package com.sheep.gamegroup.view.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sheep.gamegroup.model.entity.PictureInvitationEntity;
import com.sheep.jiuyan.samllsheep.R;

import java.util.List;

/**
 * created on：2018/9/4 on 10:57
 * created by: YSL
 * 描述:
 */
public class InvitationPictureAdapter extends RecyclerView.Adapter<InvitationPictureAdapter.MyHolder> {
    private Context mContet;
    private List<PictureInvitationEntity> mList;
    private ItemOnClickListener itemOnClickListener;
    private int position;

    public InvitationPictureAdapter(Context mContet, List<PictureInvitationEntity> mList) {
        this.mContet = mContet;
        this.mList = mList;

    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContet).inflate(R.layout.adapter_invitationpictureadapter, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder viewHolder, int i) {
        position = i;
        viewHolder.tvPosition.setText("邀请" + (i + 1));
        // GlideImageLoader.centerImage(viewHolder.ivPicture, mList.get(i).getPicture());
        viewHolder.ivPicture.setImageResource(R.drawable.kaipin);
        viewHolder.ivPicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemOnClickListener.itemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return 5;
    }


    private Bitmap mergeBitmap(ImageView imageFirst, ImageView imageSecound) {
        Bitmap firstBitmap = ((BitmapDrawable) imageFirst.getDrawable()).getBitmap();
        Bitmap secondBitmap = ((BitmapDrawable) imageFirst.getDrawable()).getBitmap();
        Bitmap bitmap = Bitmap.createBitmap(firstBitmap.getWidth(), firstBitmap.getHeight(), firstBitmap.getConfig());
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(firstBitmap, new Matrix(), null);
        canvas.drawBitmap(secondBitmap, 0, 0, null);
        return bitmap;
    }


    class MyHolder extends RecyclerView.ViewHolder {
        private ImageView ivPicture;
        private TextView tvPosition;

        public MyHolder(View itemView) {
            super(itemView);
            ivPicture = itemView.findViewById(R.id.iv_picture);
            tvPosition = itemView.findViewById(R.id.tv_position);
        }
    }

    /**
     * 设置recycleview item间距辅助类
     */
    public static class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        /**
         * 设置间距
         */
        private int space;

        public SpacesItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.left = space;
            outRect.right = space;
            //outRect.bottom = space;
            /*if (parent.getChildPosition(view) == 0)
                outRect.top = space;*/
        }
    }

    /**
     * 图片点击事件回调
     */
    public interface ItemOnClickListener {
        void itemClick(int position);
    }


}
