package com.gingersoft.gsa.cloud.ui.adapter.multi;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.gingersoft.gsa.cloud.ui.R;
import com.gingersoft.gsa.cloud.ui.adapter.bean.InfoMultiBean;
import com.gingersoft.gsa.cloud.ui.adapter.bean.MultiInputBean;

import org.jetbrains.annotations.NotNull;

import butterknife.ButterKnife;

/**
 * @author 宇航.
 * User: admin
 * Date: 2020/10/29
 * Time: 15:58
 * Use:
 */
public class InputProvider<T extends InfoMultiBean> extends MyBaseItemProvider<T> {

    @Override
    public int getItemViewType() {
        return InfoMultiBean.ITEM_TYPE_INPUT;
    }

    @Override
    public int getLayoutId() {
        return R.layout.multi_input;
    }

    @Override
    public @NotNull InputViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        return new InputViewHolder(LayoutInflater.from(context).inflate(getLayoutId(), parent, false));
    }

    @Override
    public void convert(@NotNull BaseViewHolder baseViewHolder, T t) {
        super.convert(baseViewHolder, t);
        InputViewHolder viewHolder = (InputViewHolder) baseViewHolder;
        MultiInputBean infoMultiBean = (MultiInputBean) t;

        viewHolder.removeTextWatcher();

        if (!TextUtils.isEmpty(infoMultiBean.getHint())) {
            baseViewHolder.setVisible(R.id.tv_expect,true);
            baseViewHolder.setText(R.id.tv_expect,infoMultiBean.getHint());
        }else {
            baseViewHolder.setGone(R.id.tv_expect,true);
        }


        EditText editText = baseViewHolder.getView(R.id.ed_multi_value);
        if (infoMultiBean.getTextWatcher() != null) {
            editText.removeTextChangedListener(infoMultiBean.getTextWatcher());
            editText.addTextChangedListener(infoMultiBean.getTextWatcher());
        }
        editText.setError(null);
        initEditFocusable(infoMultiBean, editText);
        //設置輸入限制
        setFilters(editText, infoMultiBean);
        //刷新數據
        viewHolder.updateView();
        //設置輸入框的值
        initEditText(editText, infoMultiBean.getShowValue(), infoMultiBean.getHintText());
        //添加焦點監聽
        editText.setOnFocusChangeListener((v, hasFocus) -> {
            if (getAdapter() != null) {
                T infoBean = getAdapter().getData().get(baseViewHolder.getAdapterPosition());
                if (infoBean instanceof MultiInputBean) {
                    MultiInputBean currentInputBean = (MultiInputBean) infoBean;
                    if (currentInputBean.getOnFocusChangeListener() != null) {
                        currentInputBean.getOnFocusChangeListener().onFocusChange(v, hasFocus);
                    }
                }
            }
        });
    }

    private void initEditFocusable(MultiInputBean infoMultiBean, EditText editText) {
        //是否可編輯
        editText.setFocusable(infoMultiBean.isEdit());
        if (infoMultiBean.isEdit()) {
            editText.setTextColor(ContextCompat.getColor(context, R.color.color_3c));
        } else {
            editText.setTextColor(ContextCompat.getColor(context, R.color.color_86));
        }
    }

    private void initEditText(EditText tvThird, String showValue, String hintText) {
        tvThird.setText(showValue);
        tvThird.setHint(hintText);
    }

    private void setFilters(EditText editText, MultiInputBean multiSelectBean) {
        //先清空值.清空值之前需要去掉輸入監聽
        editText.setText("");
        //輸入攔截器
        editText.setFilters(multiSelectBean.getInputFilters());
        //輸入類型限制
        editText.setInputType(multiSelectBean.getInputType());
    }


    public class InputViewHolder extends BaseViewHolder {
        public InputTextWatcher inputTextWatcher = new InputTextWatcher();
        private EditText editText;

        public InputViewHolder(@NotNull View view) {
            super(view);
            ButterKnife.bind(this, view);
            editText = view.findViewById(R.id.ed_multi_value);
        }

        public void removeTextWatcher() {
            editText.removeTextChangedListener(inputTextWatcher);
        }

        public void updateView() {
            editText.addTextChangedListener(inputTextWatcher);
            inputTextWatcher.inputViewHolder = this;
        }
    }

    class InputTextWatcher implements TextWatcher {
        InputViewHolder inputViewHolder;

        @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 (inputViewHolder != null) {
                MultiInputBean infoMultiBean = (MultiInputBean) getAdapter().getItem(inputViewHolder.getAdapterPosition());
                infoMultiBean.setShowValue(s + "");
            }
        }
    }
}
