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 android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.wd.workoffice.R;
import com.wd.workoffice.bean.FinancailDetailBean;
import com.wd.workoffice.util.MathUtils;
import com.wd.workoffice.util.WorkUtils;

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

import androidx.annotation.Nullable;

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

public class FinancialDetailAdapter extends BaseQuickAdapter<FinancailDetailBean, BaseViewHolder> {
    Map<Integer, String> errorMap = new HashMap<>();
    String status;

    public Map<Integer, String> getErrorMap() {
        return errorMap;
    }

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

    public FinancialDetailAdapter(int layoutResId, @Nullable List<FinancailDetailBean> data, String status) {
        super(layoutResId, data);
        this.status = status;
    }

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

    @Override
    protected void convert(BaseViewHolder helper, FinancailDetailBean item) {
        helper.setText(R.id.tv_type, item.getTaxTypeName());
        helper.setText(R.id.tv_price, MathUtils.converData(item.getAmount(),3));

        helper.setText(R.id.et_remark, item.getAuditRemark());
        if (TextUtils.equals("0", status) || TextUtils.equals("3", status)) {
            helper.setText(R.id.tv_status, WorkUtils.getFinancialTypeStatus("1"));
        } else {
            helper.setText(R.id.tv_status, WorkUtils.getFinancialTypeStatus(item.getStatus()));
        }
        switch (item.getStatus()) {
            case "0":
//                helper.setText(R.id.tv_status, WorkUtils.getFinancialTypeStatus("1"));
                break;
            case "1":
                helper.setGone(R.id.rl_status, false);
                helper.setGone(R.id.rl_remark, false);
                helper.setGone(R.id.tv_error, false);
                break;
            case "2":
                helper.setGone(R.id.tv_error, false);
                break;
        }
        EditText remark = helper.getView(R.id.et_remark);
        remark.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.setAuditRemark(s.toString());
            }
        });
        TextView error = helper.getView(R.id.tv_error);
        error.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.equals("标记错误",error.getText().toString())){
                    helper.setText(R.id.tv_status, WorkUtils.getFinancialTypeStatus("2"));
                    errorMap.put(helper.getAdapterPosition(), "");
                    error.setText("标记正确");
                }else {
                    helper.setText(R.id.tv_status, WorkUtils.getFinancialTypeStatus("1"));
                    if (errorMap.containsKey(helper.getAdapterPosition()))
                        errorMap.remove(helper.getAdapterPosition());
                    error.setText("标记错误");
                }
            }
        });


    }
}

