package com.wd.workoffice.ui.adapter;


import android.text.TextUtils;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.wd.workoffice.R;
import com.wd.workoffice.bean.AddStockChooseBean;
import com.wd.workoffice.bean.event.ModifyCarEvent;
import com.wd.workoffice.util.MathUtils;
import com.wd.workoffice.widget.AddAndReduceView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by flexible on 2018/8/13.
 */

public class SendApplyCrucibleChooseAdapter extends BaseQuickAdapter<AddStockChooseBean.RecordsBean, BaseViewHolder> {
    int type;// 0 新增选择  1 新增展示 2 详情

    List<AddStockChooseBean.RecordsBean> chooseList = new ArrayList<>();

    Map<Integer, Boolean> isChoose = new LinkedHashMap<>();

    public SendApplyCrucibleChooseAdapter(int layoutResId, List data) {
        super(layoutResId, data);
        for (int i = 0; i < getData().size(); i++) {
            isChoose.put(i, false);
        }
    }

    public SendApplyCrucibleChooseAdapter(int layoutResId, List data, int type) {
        super(layoutResId, data);
        this.type = type;
        for (int i = 0; i < getData().size(); i++) {
            isChoose.put(i, false);
        }
    }


    @Override
    protected void convert(BaseViewHolder helper, AddStockChooseBean.RecordsBean item) {
        if (type == 1) {
            helper.setGone(R.id.ll_bottom, false);
            helper.setGone(R.id.cb_check, false);
            helper.setText(R.id.tv_stock_num_desc, "可发货数量");
            helper.setText(R.id.tv_stock_num, MathUtils.converData(item.getChooseNum(), 3));
        } else if (type == 2) {
            helper.setGone(R.id.ll_bottom, false);
            helper.setGone(R.id.cb_check, false);
            helper.setText(R.id.tv_stock_num_desc, "申请发货数量");
            helper.setText(R.id.tv_stock_num, MathUtils.converData(item.getQuantity(), 3));
        } else {
            helper.setText(R.id.tv_stock_num_desc, "可发货数量");
            helper.setText(R.id.tv_add_desc, "申请发货数量");
            helper.setText(R.id.tv_stock_num, MathUtils.converData(item.getQuantity(), 3));
        }
        helper.setText(R.id.tv_name, item.getCrucibleId());
        helper.setText(R.id.tv_stock, item.getStockName());
        CheckBox cbChoose = helper.getView(R.id.cb_check);
        cbChoose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isChoose.put(helper.getAdapterPosition(), cbChoose.isChecked());
                if (isChecked) {
                    addChooseList(item);
                } else {
                    removeChooseList(item);
                }
                EventBus.getDefault().post(new ModifyCarEvent(JSON.toJSONString(getData())));
            }
        });
        cbChoose.setChecked(isChoose.get(helper.getAdapterPosition()) == null ? false : isChoose.get(helper.getAdapterPosition()));
        AddAndReduceView num = helper.getView(R.id.ar_num);
        num.setMaxNum(item.getQuantity());
        if (!TextUtils.isEmpty(item.getChooseNum()))
            num.setNumber(item.getChooseNum());
        num.setOnNumberChangedListener(new AddAndReduceView.OnNumberChangedListener() {
            @Override
            public void OnNumberChanged(String vs) {
                item.setChooseNum(vs);
                changeChooseNum(item);
                EventBus.getDefault().post(new ModifyCarEvent(JSON.toJSONString(getData())));
            }
        });

    }

    public Map<Integer, Boolean> getIsChoose() {
        return isChoose;
    }

    public void setIsChoose(Map<Integer, Boolean> isChoose) {
        this.isChoose = isChoose;
    }


    public void notifyData() {
        for (int i = 0; i < isChoose.entrySet().size(); i++) {
            isChoose.put(i, false);
        }
        for (AddStockChooseBean.RecordsBean recordsBean : chooseList) {
            for (int i = 0; i < getData().size(); i++) {
                if (getData().get(i).getId() == recordsBean.getId()) {
                    isChoose.put(i, true);
                }
            }
        }
        notifyDataSetChanged();
    }


    public List<AddStockChooseBean.RecordsBean> getChooseList() {
        return chooseList;
    }

    public void setChooseList(List<AddStockChooseBean.RecordsBean> chooseList) {
        this.chooseList = chooseList;
    }

    private void addChooseList(AddStockChooseBean.RecordsBean data) {
        for (AddStockChooseBean.RecordsBean chooseData : chooseList) {
            if (chooseData.getId() == data.getId()) {
                chooseList.remove(chooseData);
            }
        }
        chooseList.add(data);
    }

    private void removeChooseList(AddStockChooseBean.RecordsBean data) {
        chooseList.remove(data);
    }

    private void changeChooseNum(AddStockChooseBean.RecordsBean data) {
        for (AddStockChooseBean.RecordsBean chooseData : chooseList) {
            if (chooseData.getId() == data.getId()) {
                chooseData.setChooseNum(data.getChooseNum());
            }
        }
    }
}

