package com.fxy.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.fxy.R;
import com.fxy.detection.bean.OptionsBean;
import com.luck.picture.lib.entity.LocalMedia;

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


public class ShowPictureAdapter extends RecyclerView.Adapter<ShowPictureAdapter.ViewHolder> {

    private LayoutInflater mInflater;
    //已上传
    private List<String> pictureList =new ArrayList<>();
    private int selectMax = 9;
    private  Context context;
    private  Activity mActivity;


    /**
     * 点击添加图片跳转
     */
    private final onAddPicClickListener mOnAddPicClickListener;

    public interface onAddPicClickListener {
        void onAddPicClick();
    }


    public ShowPictureAdapter(Context context, Activity activity, onAddPicClickListener mOnAddPicClickListener) {
        this.context = context;
        this.mActivity = activity;
        mInflater = LayoutInflater.from(context);
        this.mOnAddPicClickListener = mOnAddPicClickListener;
    }

    public ShowPictureAdapter(Context context, Activity activity, onAddPicClickListener mOnAddPicClickListener, List<String> list) {
        this.context = context;
        this.mActivity = activity;
        mInflater = LayoutInflater.from(context);
        this.mOnAddPicClickListener = mOnAddPicClickListener;
        pictureList= list;
    }

    public void setList( List<String> list) {
        pictureList.clear();
        this.pictureList = list;

    }

    public void setBeanList( List<OptionsBean> list) {

        pictureList.clear();
        for (int i = 0; i < list.size(); i++) {
//            pictureList.add(list.get(i).getUrl());
        }
    }

    /**
     *
     * 支持当前上传
     */
    public void setLocalMediaList(List<LocalMedia> list) {
        pictureList.clear();
        for (int i = 0; i < list.size(); i++) {
            String path;
            LocalMedia media= list.get(i);
            if (media.isCut() && !media.isCompressed()) {
                // 裁剪过
                path = media.getCutPath();
            } else if (media.isCompressed() || (media.isCut() && media.isCompressed())) {
                // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
                path = media.getCompressPath();
            } else {
                // 原图
                path = media.getPath();
            }
            pictureList.add(path);
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView mImg;
        CheckBox cbSelect;
        View selectedOverlay;

        ViewHolder(View view) {
            super(view);
            mImg = view.findViewById(R.id.ivp_img);
            cbSelect = view.findViewById(R.id.cb_select);
        }
    }

    @Override
    public int getItemCount() {
        if (pictureList.size() < selectMax) {
            return pictureList.size();
        } else {
            return pictureList.size();
        }
    }


    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.item_preview_image, viewGroup, false);
        return new ViewHolder(view);
    }
    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        //少于8张，显示继续添加的图标
        viewHolder.cbSelect.setVisibility(View.VISIBLE);
        viewHolder.cbSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // 当checkbox1的选中状态改变时执行
                if (isChecked) {
                    // checkbox1被选中了
                    viewHolder.selectedOverlay.setVisibility(View.VISIBLE);
                } else {
                    // checkbox1被取消了
                    viewHolder.selectedOverlay.setVisibility(View.GONE);
                }
            }
        });

        if (pictureList.size()>0){
            String path = pictureList.get(position);
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.color.color_f6)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
            Glide.with(viewHolder.itemView.getContext())
                    .load(path)
                    .apply(options)
                    .into(viewHolder.mImg);
        }


        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int adapterPosition = viewHolder.getAdapterPosition();
                //外部定义了
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(adapterPosition, v);
                }
            }
        });
    }

    private OnItemClickListener mItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }

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