package com.wd.workoffice.ui.adapter;


import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
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.DeviceBean;
import com.wd.workoffice.bean.ProductBean;
import com.wd.workoffice.bean.event.ModifyCarEvent;
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 DeviceCarAdapter extends BaseQuickAdapter<DeviceBean.RecordsBean, BaseViewHolder> {

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


    @Override
    protected void convert(BaseViewHolder helper, DeviceBean.RecordsBean item) {
        helper.setText(R.id.tv_name, item.getTitle());
        helper.setText(R.id.tv_spec, item.getSpecification());
        helper.setText(R.id.tv_all_price, MathUtils.multiply(item.getCarNum() , item.getCarPrice(), 3));
        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 , item.getCarPrice(), 3));
                EventBus.getDefault().post(new ModifyCarEvent(JSON.toJSONString(getData())));
            }

        });
        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;
                }
                item.setCarPrice(s.toString());
                helper.setText(R.id.tv_all_price, MathUtils.multiply(item.getCarNum() , item.getCarPrice(), 3));
                EventBus.getDefault().post(new ModifyCarEvent(JSON.toJSONString(getData())));
            }
        });
        num.setNumber(item.getCarNum());
    }
}

