package com.wd.workoffice.ui.adapter;


import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.wd.workoffice.R;
import com.wd.workoffice.bean.OrderDetail;
import com.wd.workoffice.bean.event.SendWeightEvent;
import com.wd.workoffice.util.MathUtils;
import com.wd.workoffice.widget.AddAndReduceView;

import org.greenrobot.eventbus.EventBus;

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

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

public class SendApplyAddCrucibleAdapter extends BaseQuickAdapter<OrderDetail.OrderItemsBean, BaseViewHolder> {

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

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

    @Override
    protected void convert(BaseViewHolder helper, OrderDetail.OrderItemsBean item) {
        if (!TextUtils.isEmpty(item.getCount())) {
            helper.setText(R.id.tv_from, item.getCount());
        }
        helper.setText(R.id.tv_name, item.getProductSimpleCode());
        helper.setText(R.id.tv_card, item.getProductSoleCode());
        helper.setText(R.id.tv_card_name, item.getProductName());
        helper.setText(R.id.tv_spec, item.getProductSpec());
        helper.setText(R.id.tv_price, "￥".concat(MathUtils.converData(item.getInnerPrice(), 3)));
//        helper.setText(R.id.tv_all_price, "￥".concat(String.valueOf(item.getInnerPrice())));
        helper.setText(R.id.tv_unit, item.getProductUnit());
        helper.setText(R.id.et_lot, item.getLot());
//        helper.setText(R.id.tv_from, WorkUtils.getOrderFrom(item.getType()));
        helper.setText(R.id.tv_num, MathUtils.converData(item.getProductCount(), 3));
        helper.setText(R.id.tv_apply_send_num, MathUtils.converData(item.getCanOutStoreProductSum(), 3));
        helper.setText(R.id.tv_send_num, MathUtils.converData(item.getShipSum(), 3));
        EditText etLot = helper.getView(R.id.et_lot);
        etLot.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setLot(s.toString());
            }
        });
        CheckBox cbChoose = helper.getView(R.id.cb_check);
        cbChoose.setChecked(isChoose.get(helper.getAdapterPosition()) == null ? false : isChoose.get(helper.getAdapterPosition()));
        cbChoose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isChoose.put(helper.getAdapterPosition(), cbChoose.isChecked());
                EventBus.getDefault().post(new SendWeightEvent());
            }
        });

        helper.addOnClickListener(R.id.rl_from);
    }

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

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

