package com.ycgis.macall.personalcenter.v.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.rs.macall.androidx.basemodel.base.BaseRecyclerAdapter;
import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.databinding.ItemFrequentlyQuestionsBinding;
import com.ycgis.macall.personalcenter.databinding.ItemFrequentlyTitleBinding;
import com.ycgis.macall.personalcenter.m.adapterbean.BaseBean;
import com.ycgis.macall.personalcenter.m.adapterbean.FrequentlyItemBean;
import com.ycgis.macall.personalcenter.m.requestbean.FrequentlyBean;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * created by:   Macall
 * create time:  2023/11/7 14:58
 * copyright:    @ruansee.com
 * Describe:
 */
public class FrequentlyQuestionsAdapter extends BaseRecyclerAdapter<BaseBean,BaseRecyclerAdapter.ViewHolder> {

    public FrequentlyQuestionsAdapter(Context context, List<BaseBean> dataList) {
        super(context, dataList);
    }

    //1 大标题   2 问题标题    3 结尾标题
    @Override
    public int getItemViewType(int position) {
        return getDataItem(position).getBaseType();
    }

    @NonNull
    @NotNull
    @Override
    public BaseRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        if (viewType == 0 || viewType == -1) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_error, parent, false);
            return new ErrorViewHolder(view);
        }
        if (viewType == 1){
            View view = bindView(parent,R.layout.item_frequently_title);
            ItemFrequentlyTitleBinding binding = ItemFrequentlyTitleBinding.bind(view);
            return new ViewBindingHolder<>(binding);
        }
        if (viewType == 2||viewType == 3){
            View view = bindView(parent,R.layout.item_frequently_questions);
            ItemFrequentlyQuestionsBinding binding = ItemFrequentlyQuestionsBinding.bind(view);
            return new ViewBindingHolder<>(binding);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull BaseRecyclerAdapter.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof BaseRecyclerAdapter.ViewBindingHolder){
            ViewBindingHolder holder = (ViewBindingHolder)viewHolder;
            if (holder.viewBind instanceof ItemFrequentlyTitleBinding){
                ItemFrequentlyTitleBinding binding =(ItemFrequentlyTitleBinding) holder.viewBind;
                FrequentlyBean dataItem = (FrequentlyBean)getDataItem(position);
                binding.frequentlyTvClassification.setText(dataItem.getTitle());
                return;
            }
            if (holder.viewBind instanceof ItemFrequentlyQuestionsBinding){
                ItemFrequentlyQuestionsBinding binding =(ItemFrequentlyQuestionsBinding) holder.viewBind;
                BaseBean dataItem1 = getDataItem(position);
                if (dataItem1 instanceof FrequentlyItemBean){
                    FrequentlyItemBean dataItem = (FrequentlyItemBean)getDataItem(position);
                    binding.frequentlyTvTitle.setText(dataItem.getProblem());
                    if (dataItem.getBaseType() == 3){
                        binding.getRoot().setBackground(ContextCompat.getDrawable(context,R.drawable.round_bottom_white_8));
                    }else {
                        binding.getRoot().setBackground(ContextCompat.getDrawable(context,R.color.colorWhite));
                    }
                }
                return;
            }
        }
        if (viewHolder instanceof BaseRecyclerAdapter.ErrorViewHolder) {
            ErrorViewHolder holder = (ErrorViewHolder) viewHolder;
            if (getDataItem(position).getBaseType() == 0) {
                holder.setEmpty(getDataItem(position).getBaseMessage());
            } else {
                holder.setError(getDataItem(position).getBaseMessage());
            }
        }
    }
}
