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

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;

import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.gingersoft.gsa.cloud.common.utils.other.TextUtil;
import com.gingersoft.gsa.cloud.ui.R;
import com.gingersoft.gsa.cloud.ui.bean.InfoMultiBean;
import com.gingersoft.gsa.cloud.ui.bean.MultiInputBean;

import org.jetbrains.annotations.NotNull;

import java.util.List;

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

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

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

    @Override
    public void convert(@NotNull BaseViewHolder baseViewHolder, T t) {
        MultiInputBean infoMultiBean = (MultiInputBean) t;
        //是否顯示必選
        baseViewHolder.setGone(R.id.multi_required, !infoMultiBean.isRequired());
        //設置標題
        if (TextUtil.isNotEmptyOrNullOrUndefined(infoMultiBean.getShowTitle())) {
            baseViewHolder.setText(R.id.tv_multi_title, infoMultiBean.getShowTitle());
        } else {
            baseViewHolder.setText(R.id.tv_multi_title, "");
        }

        EditText editText = baseViewHolder.getView(R.id.ed_multi_value);
        if (TextUtil.isNotEmptyOrNullOrUndefined(infoMultiBean.getShowValue())) {
            editText.setText(infoMultiBean.getShowValue());
        } else {
            editText.setHint(infoMultiBean.getHintText());
            editText.setText("");
        }
        ImageView ivClear = baseViewHolder.getView(R.id.iv_clear_multi_content);
        ivClear.setOnClickListener(v -> editText.setText(""));
        editText.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) {
                List<T> data = getAdapter().getData();
                data.get(baseViewHolder.getAdapterPosition()).setShowValue(s + "");
                if (s != null && TextUtil.isNotEmptyOrNullOrUndefined(s.toString())) {
                } else {
                    editText.setHint(data.get(baseViewHolder.getAdapterPosition()).getHintText());
                }
                baseViewHolder.setGone(R.id.iv_clear_multi_content, s.length() <= 0);
            }
        });

        if (infoMultiBean.getInputFilters() != null) {
            editText.setFilters(infoMultiBean.getInputFilters());
        } else {
            editText.setFilters(editText.getFilters());
        }

        if (infoMultiBean.getInputType() != -1) {
            editText.setInputType(infoMultiBean.getInputType());
        } else {
            editText.setInputType(editText.getInputType());
        }
    }
}
