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.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.wd.workoffice.R;
import com.wd.workoffice.bean.FinancialTypeBean;
import com.wd.workoffice.bean.event.AddFinancialNumEvent;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.greendao.annotation.Id;

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

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

public class FinancialAddAdapter extends BaseQuickAdapter<FinancialTypeBean, BaseViewHolder> {
    Map<Integer, String> num = new HashMap<>();

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

    public Map<Integer, String> getNum() {
        return num;
    }

    public void setNum(Map<Integer, String> num) {
        this.num = num;
    }

    @Override
    protected void convert(BaseViewHolder helper, FinancialTypeBean item) {
        helper.setText(R.id.tv_name, item.getName());
        EditText etNum = helper.getView(R.id.et_num);
        etNum.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) {
                WorkUtils.keepDecimals(etNum);
            }

            @Override
            public void afterTextChanged(Editable s) {
//                if (Double.valueOf(s.toString())==0d){
//                    etNum.setText("");
//                    return;
//                }
                num.put(helper.getAdapterPosition(), s.toString());
                EventBus.getDefault().post(new AddFinancialNumEvent());
            }
        });
        etNum.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    if (TextUtils.equals("0", etNum.getText().toString())) {
                        etNum.setText("");
                    }
                }else {
                    if (TextUtils.isEmpty( etNum.getText().toString())) {
                        etNum.setText("0");
                    }
                }
            }
        });
    }
}

