package com.zztrapp.zztrapp.ui.currency.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.entity.LocalMedia;
import com.zztrapp.zztrapp.R;
import com.zztrapp.zztrapp.utilcode.mUtil.GlideMyUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * @author：luck
 * @date：2016-7-27 23:02
 * @describe：GridImageAdapter
 */
public class GridImageAdapter extends RecyclerView.Adapter<GridImageAdapter.ViewHolder> {
    public static final int TYPE_CAMERA = 1;
    public static final int TYPE_PICTURE = 2;
    private List<LocalMedia> list = new ArrayList<>();
    private int selectMax = 8;
    private Context context;
    public int addImageType = 1;//1必传1张 2必传2张 22可选2张 44可选4张 8可选8张
    private int jumpType;// 0采样、1修改、2查看  用来控制是否显示删除按钮


    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onAddPicClick(int position);

        void onDelPicClick(int position);

        void onPicClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    public GridImageAdapter(Context context, int addImageType, int jumpType) {
        this.context = context;
        this.addImageType = addImageType;
        this.jumpType = jumpType;
    }

    public void setSelectMax(int selectMax) {
        this.selectMax = selectMax;
    }

    public void setList(List<LocalMedia> list) {
        this.list = list;
    }

    @Override
    public int getItemCount() {
        if (jumpType == 2) {
            return list.size();
        } else {
            if (list.size() < selectMax) {
                return list.size() + 1;
            } else {
                return list.size();
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (jumpType == 2) {
            return TYPE_PICTURE;
        } else {
            if (isShowAddItem(position)) {
                return TYPE_CAMERA;
            } else {
                return TYPE_PICTURE;
            }
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.gv_filter_image, parent, false);
        return new ViewHolder(view);
    }

    private boolean isShowAddItem(int position) {
        int size = list.size();
        return position == size;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, @SuppressLint("RecyclerView") int position) {
        //少于MaxSize张，显示继续添加的图标
        if (getItemViewType(position) == TYPE_CAMERA) {
            viewHolder.mImg.setImageResource(R.mipmap.img_ic);
            viewHolder.mImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onAddPicClick(position);
                    }
                }
            });
            viewHolder.mIvDel.setVisibility(View.INVISIBLE);
        } else {
            if (jumpType == 2) {//查看
                viewHolder.mIvDel.setVisibility(View.GONE);
            } else {
                viewHolder.mIvDel.setVisibility(View.VISIBLE);
            }
            //itemView 的点击事件
            if (onItemClickListener != null) {
                viewHolder.mIvDel.setOnClickListener(view -> onItemClickListener.onDelPicClick(position));
            }
            //itemView 的点击事件
            if (onItemClickListener != null) {
                viewHolder.itemView.setOnClickListener(v -> {
                    onItemClickListener.onPicClick(position);
                });
            }
            Glide.with(context).load(GlideMyUtil.getGlideUrl(list.get(position).getPath())).error(R.drawable.picture_icon_data_error).centerCrop().into(viewHolder.mImg);

        }
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView mImg;
        ImageView mIvDel;

        public ViewHolder(View view) {
            super(view);
            mImg = view.findViewById(R.id.fiv);
            mIvDel = view.findViewById(R.id.iv_del);
        }
    }

}
