package com.unis.sxzcl.ui.template;

import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.unis.sxzcl.R;
import com.unis.sxzcl.databinding.TemplateTvBinding;

import org.jetbrains.annotations.NotNull;

/**
 * 包名: com.unis.sxzcl.ui.template
 * 类名: DetailTemplateAdapter
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/11/18 15:03
 * 更新人:
 * 更新时间: 2020/11/18 15:03
 * 更新说明:
 * 版本号: 1.0
 */
public class DetailTemplateAdapter extends BaseQuickAdapter<DetailTemplateBean, BaseViewHolder> {

    private Callback mCallback;

    public DetailTemplateAdapter() {
        super(R.layout.item_detail_template);
    }

    public void setCallback(Callback callback) {
        mCallback = callback;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, DetailTemplateBean item) {
        // 标题
        holder.setText(R.id.tv_title, (getItemPosition(item) + 1) + "." + item.title);

        // 选项创建
        LinearLayout layoutOption = holder.getView(R.id.layout_option);
        layoutOption.removeAllViews();
        for (int i = 0; i < item.option.length; i++) {
            layoutOption.addView(createdTextView(item, i));
        }

        if (item.flowNow % 2 == 0) {
            holder.itemView.setBackgroundResource(R.color.colorBackground);
        } else {
            holder.itemView.setBackgroundResource(R.color.colorWhite);
        }
    }

    /**
     * 创建单选按钮
     */
    private TextView createdTextView(DetailTemplateBean item, int optionIndex) {
        TextView textView = TemplateTvBinding.inflate(LayoutInflater.from(getContext())).getRoot();
        textView.setText(item.option[optionIndex]);
        textView.setGravity(Gravity.CENTER_VERTICAL);
        textView.setEnabled(item.enable);
        if (item.selectIndex == optionIndex) {
            Drawable dra = getContext().getResources().getDrawable(R.drawable.template_tv_left_sel_enable);
            textView.setCompoundDrawablesWithIntrinsicBounds(dra, null, null, null);
        } else {
            Drawable dra = getContext().getResources().getDrawable(R.drawable.template_tv_left_enable);
            textView.setCompoundDrawablesWithIntrinsicBounds(dra, null, null, null);
        }
        textView.setTag(item);
        textView.setOnClickListener(v -> {
            TextView tv = (TextView) v;
            DetailTemplateBean bean = (DetailTemplateBean) v.getTag();
            int index = -1;
            for (int i = 0; i < bean.option.length; i++) {
                if (tv.getText().toString().equals(bean.option[i])) {
                    index = i;
                }
            }
            notifyItemChanged(getItemPosition(item));
            if (mCallback != null)
                mCallback.onChangesListener(bean, index);
        });
        return textView;
    }

    public interface Callback {
        void onChangesListener(DetailTemplateBean item, int changeIndex);
    }

}
