package com.sipai.jspatrol.Fault.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sipai.jspatrol.Bean.User;
import com.sipai.jspatrol.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lvliming on 2019/3/24.
 */

public class PersonSelectorAdapter extends RecyclerView.Adapter<PersonSelectorAdapter.ViewHolder> implements View.OnClickListener{
    //数据源
    private List<User> list;
    private Context context;
    //是否显示单选框
    private boolean isshowBox = true;
    //存储勾选框状态的map集合
    private Map<Integer,Boolean> map = new HashMap<>();
    //接口实例
    private RecyclerViewOnItemClickListener onItemClickListener;

    public PersonSelectorAdapter(List<User> list, Context context) {
        this.list = list;
        this.context = context;
        initMap();
    }
    //初始化map集合，默认不选中
    private void initMap() {
        for (int i=0;i<list.size();i++){
            map.put(i,false);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_selector_person, parent, false);
        ViewHolder vh = new ViewHolder(root);
        //为Item设置点击事件
        root.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        User item = list.get(position);
        holder.tv_name.setText(item.getCaption());
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.list_anim);
        //设置Tag
        holder.root.setTag(position);
        //设置checkBox改变监听
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //用map集合保存
                map.put(position, isChecked);
                if (holder.checkBox.isChecked()){
                    holder.ll_card.setBackgroundColor(context.getResources().getColor(R.color.little_blue));
                }else {
                    holder.ll_card.setBackgroundColor(context.getResources().getColor(R.color.green));
                }
            }
        });
        // 设置CheckBox的状态
        if (map.get(position) == null) {
            map.put(position, false);
        }
        holder.checkBox.setChecked(map.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        if (onItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            onItemClickListener.onItemClickListener(v, (Integer) v.getTag());
        }
    }

    //视图管理
    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tv_name;
        private CheckBox checkBox;
        private LinearLayout ll_card;
        private View root;
        public ViewHolder(View itemView) {
            super(itemView);
            this.root = itemView;
            tv_name = root.findViewById(R.id.item_selector_person_tv_name);
            checkBox = (CheckBox) root.findViewById(R.id.item_selector_person_cb);
            ll_card = root.findViewById(R.id.item_selector_person_ll_card);
        }
    }

    //设置点击事件
    public void setRecyclerViewOnItemClickListener(RecyclerViewOnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    //设置是否显示CheckBox
    public void setShowBox() {
        //取反
        isshowBox = !isshowBox;
    }

    //点击item选中CheckBox
    public void setSelectItem(int position) {
        //对当前状态取反
        if (map.get(position)) {
            map.put(position, false);
        } else {
            map.put(position, true);
        }
        notifyItemChanged(position);
    }
    //返回集合给MainActivity
    public Map<Integer, Boolean> getMap() {
        return map;
    }

    //接口回调设置点击事件
    public interface RecyclerViewOnItemClickListener {
        //点击事件
        void onItemClickListener(View view, int position);

    }
}
