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 com.rs.macall.androidx.basemodel.base.BaseRecyclerAdapter;
import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.databinding.ItemSelectDeptBinding;
import com.ycgis.macall.personalcenter.m.adapterbean.OrgBean;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * created by:   Macall
 * create time:  2023/11/23 16:37
 * copyright:    @ruansee.com
 * Describe:
 */
public class SelectDeptAdapter extends BaseRecyclerAdapter<OrgBean,BaseRecyclerAdapter.ViewHolder> {

    public SelectDeptAdapter(Context context, List<OrgBean> dataList) {
        super(context, dataList);
    }

    @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_select_dept);
            ItemSelectDeptBinding bind = ItemSelectDeptBinding.bind(view);
            return new ViewBindingHolder<>(bind);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull BaseRecyclerAdapter.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof ViewBindingHolder){
            ViewBindingHolder<ItemSelectDeptBinding> holder =(ViewBindingHolder<ItemSelectDeptBinding>) viewHolder;
            OrgBean dataItem = getDataItem(position);
            holder.viewBind.tvOrgName.setText(dataItem.getOrgName());
            if (dataItem.isSelect()){
                holder.viewBind.ivSelect.setVisibility(View.VISIBLE);
            }else {
                holder.viewBind.ivSelect.setVisibility(View.INVISIBLE);
            }
        }
        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());
            }
        }
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull BaseRecyclerAdapter.ViewHolder viewHolder, int position, @NonNull @NotNull List<Object> payloads) {
       if (payloads.isEmpty()){
           onBindViewHolder(viewHolder, position);
       }else {
           Object o = payloads.get(0);
           if (o instanceof Integer){
               if (viewHolder instanceof ViewBindingHolder) {
                   ViewBindingHolder<ItemSelectDeptBinding> holder = (ViewBindingHolder<ItemSelectDeptBinding>) viewHolder;
                   OrgBean dataItem = getDataItem(position); if (dataItem.isSelect()){
                       holder.viewBind.ivSelect.setVisibility(View.VISIBLE);
                   }else {
                       holder.viewBind.ivSelect.setVisibility(View.INVISIBLE);
                   }
               }
           }
       }
    }
}
