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.EditText;

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.ProductBean;
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.List;

import flexible.xd.android_base.utils.ToastUtil;

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

public class StoreCarAdapter extends BaseQuickAdapter<ProductBean.RecordsBean, BaseViewHolder> {
    int type = 0;//1 确认订单页面  2 单数量无价格

    public StoreCarAdapter(int layoutResId, List data) {
        super(layoutResId, data);
    }

    public StoreCarAdapter(int layoutResId, List data, int i) {
        super(layoutResId, data);
        this.type = i;
    }

    @Override
    protected void convert(BaseViewHolder helper, ProductBean.RecordsBean item) {
        helper.setText(R.id.tv_name, item.getSimpleCode());
        helper.setText(R.id.tv_card, item.getSoleCode());
        helper.setText(R.id.tv_card_name, item.getName());
        helper.setText(R.id.tv_spec, item.getSpec());
        helper.setText(R.id.tv_price, "￥".concat(String.valueOf(item.getInternalPrice())));
        helper.setText(R.id.tv_unit, String.valueOf(item.getWeight()).concat("吨"));
        helper.setText(R.id.tv_all_price, MathUtils.multiply(item.getCarNum(), item.getInternalPrice() + "", 3));
        if (type == 1) {
            helper.addOnClickListener(R.id.rl_choose_dep);
            helper.addOnClickListener(R.id.rl_upload);
            if (!TextUtils.isEmpty(item.getFactoryName())) {
                helper.setText(R.id.tv_choose_dep, item.getFactoryName());
            }
            if (!TextUtils.isEmpty(item.getUrl())) {
                helper.setText(R.id.tv_upload, "已上传，点击替换");
            }
        } else if (type == 2) {
            helper.addOnClickListener(R.id.tv_delete);
            AddAndReduceView num = helper.getView(R.id.ar_num);
            num.setOnNumberChangedListener(new AddAndReduceView.OnNumberChangedListener() {
                @Override
                public void OnNumberChanged(String vs) {
                    item.setCarNum(vs);
                    helper.setText(R.id.tv_all_price, MathUtils.multiply(vs , String.valueOf(item.getInternalPrice()), 3));
                    EventBus.getDefault().post(new ModifyCarEvent(JSON.toJSONString(getData())));
                }
            });
            EditText etPrice = helper.getView(R.id.et_price);
            helper.setGone(R.id.ll_price, false);
            num.setNumber(item.getCarNum());
        } else {
            helper.addOnClickListener(R.id.tv_delete);
            AddAndReduceView num = helper.getView(R.id.ar_num);
            num.setOnNumberChangedListener(new AddAndReduceView.OnNumberChangedListener() {
                @Override
                public void OnNumberChanged(String vs) {
                    item.setCarNum(vs);
                    helper.setText(R.id.tv_all_price, MathUtils.multiply(vs, String.valueOf(item.getInternalPrice()), 3));
                    EventBus.getDefault().post(new ModifyCarEvent(JSON.toJSONString(getData())));
                }
            });
            EditText etPrice = helper.getView(R.id.et_price);
            etPrice.setText(item.getCarPrice());
            etPrice.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) {
                    if (TextUtils.isEmpty(s)) {
                        ToastUtil.showShort("请输入外部单价");
                        return;
                    }
                    item.setCarPrice(s.toString());
                    helper.setText(R.id.tv_all_price, MathUtils.multiply(item.getCarNum(), String.valueOf(item.getInternalPrice()), 3));
                    EventBus.getDefault().post(new ModifyCarEvent(JSON.toJSONString(getData())));
                }
            });
            num.setNumber(item.getCarNum());
        }
    }
}

