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.bean.workEnum.PermissionType;
import com.wd.workoffice.util.MathUtils;
import com.wd.workoffice.util.WorkUtils;
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;// 0 购物车外采购  1 确认订单页面  2 购物车除了外采购
    private int curFocusPosition = -1;
    private String orderType;

    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(MathUtils.converData(item.getInternalPrice(), 3)));
        helper.setText(R.id.tv_unit, MathUtils.converData(item.getWeight(), 3).concat("吨"));
        helper.setText(R.id.tv_all_price, MathUtils.multiply(item.getCarNum(), MathUtils.converData(item.getInternalPrice(), 3), 3));
        if (type == 1) {
            helper.addOnClickListener(R.id.rl_choose_dep);
            helper.addOnClickListener(R.id.rl_upload);
            helper.addOnClickListener(R.id.rl_mark);
            helper.addOnClickListener(R.id.rl_receive_order);
            helper.setText(R.id.tv_sale_price, item.getCarPrice());
            helper.setText(R.id.tv_buy_count, item.getCarNum());
            if (!TextUtils.isEmpty(item.getProvideMethodName())) {
                helper.setText(R.id.tv_receive_order, item.getProvideMethodName());
            }
            if (!TextUtils.isEmpty(item.getFactoryName())) {
                helper.setText(R.id.tv_choose_dep, item.getFactoryName());
            }
            if (null != item.getMarkOrderRel()) {
                helper.setText(R.id.tv_choose_mark, "已选择");
            }
            if (!TextUtils.isEmpty(item.getUrl())) {
                helper.setText(R.id.tv_upload, item.getUrl().substring(1));
            }
            if (TextUtils.equals(orderType,
                    PermissionType.PROCESS_CRUCIBLE.getCode())) {
                helper.setGone(R.id.rl_pro_from, false);
                helper.setGone(R.id.rl_choose_dep, false);
            }
            if (TextUtils.equals(orderType,
                    PermissionType.PROCESS_SUPPLY.getCode())) {
                helper.setGone(R.id.rl_choose_dep, false);
                helper.setGone(R.id.rl_sale_price, false);
            }
            //供应和销售显示接单方式  (修改为全部隐藏)
            if (TextUtils.equals(orderType,
                    PermissionType.PROCESS_SALE.getCode()) || TextUtils.equals(orderType,
                    PermissionType.PROCESS_SUPPLY.getCode())) {
                helper.setGone(R.id.rl_receive_order, false);
            } else {
                helper.setGone(R.id.rl_receive_order, false);
            }
            helper.setGone(R.id.rl_mark, TextUtils.equals(orderType, PermissionType.PROCESS_SALE.getCode()));
        } else if (type == 2) {
            helper.setGone(R.id.ll_inner_price, false);
            helper.setGone(R.id.tv_desc, item.getFlag() == 1);
            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) {
                    if (!TextUtils.equals(vs, item.getCarNum())) {
                        item.setCarNum(vs);
                        helper.setText(R.id.tv_all_price, MathUtils.multiply(vs, MathUtils.converData(item.getInternalPrice(), 3), 3));
                        EventBus.getDefault().post(new ModifyCarEvent(JSON.toJSONString(getData()), true));
                    }
                }
            });
            EditText etPrice = helper.getView(R.id.et_price);
            etPrice.setText(item.getCarPrice());
            WorkUtils.addDecimalsListener(etPrice);
            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;
                    }
                    if (!TextUtils.equals(s.toString(), item.getCarPrice())) {
                        item.setCarPrice(s.toString());
                        helper.setText(R.id.tv_all_price, MathUtils.multiply(item.getCarNum(), MathUtils.converData(item.getInternalPrice(), 3), 3));
                        EventBus.getDefault().post(new ModifyCarEvent(JSON.toJSONString(getData())));
                    }
                }
            });
            num.setNumber(item.getCarNum());
            num.setNumFocusListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        curFocusPosition = helper.getAdapterPosition();
                    }
                }
            });
        } else {
            helper.setGone(R.id.tv_desc, item.getFlag() == 1);
            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) {
                    if (!TextUtils.equals(vs, item.getCarNum())) {
                        item.setCarNum(vs);
                        helper.setText(R.id.tv_all_price, MathUtils.multiply(vs, MathUtils.converData(item.getInternalPrice(), 3), 3));
                        EventBus.getDefault().post(new ModifyCarEvent(JSON.toJSONString(getData()), true));
                    }
                }
            });
            EditText etPrice = helper.getView(R.id.et_price);
            etPrice.setText(item.getCarPrice());
            WorkUtils.addDecimalsListener(etPrice);
            EditText etInnerPrice = helper.getView(R.id.et_inner_price);
            etInnerPrice.setText(item.getCarInnerPrice());
            WorkUtils.addDecimalsListener(etInnerPrice);
            etInnerPrice.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;
                    }
                    if (!TextUtils.equals(s.toString(), item.getCarInnerPrice())) {
                        item.setCarInnerPrice(s.toString());
                        item.setInternalPrice(Double.parseDouble(s.toString()));
                        EventBus.getDefault().post(new ModifyCarEvent(JSON.toJSONString(getData())));
                    }
                }
            });
            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;
                    }
                    if (!TextUtils.equals(s.toString(), item.getCarPrice())) {
                        item.setCarPrice(s.toString());
                        helper.setText(R.id.tv_all_price, MathUtils.multiply(item.getCarNum(), MathUtils.converData(item.getInternalPrice(), 3), 3));
                        EventBus.getDefault().post(new ModifyCarEvent(JSON.toJSONString(getData())));
                    }
                }
            });
            num.setNumber(item.getCarNum());
            num.setNumFocusListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        curFocusPosition = helper.getAdapterPosition();
                    }
                }
            });
        }
    }

    public void notifyData() {
        notifyDataSetChanged();
        if (curFocusPosition != -1) {
            AddAndReduceView num = (AddAndReduceView) getViewByPosition(curFocusPosition, R.id.ar_num);
            num.setNumFouce();
        }
    }

    public String getOrderType() {
        return orderType;
    }

    public void setOrderType(String orderType) {
        this.orderType = orderType;
    }
}

