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

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.rs.macall.androidx.basemodel.base.BaseRecyclerAdapter;
import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.databinding.ItemApplyAuditBinding;
import com.ycgis.macall.personalcenter.databinding.ItemApplyInitiateRecordBinding;
import com.ycgis.macall.personalcenter.databinding.ItemApplyMyAuditRecordBinding;
import com.ycgis.macall.personalcenter.m.adapterbean.ApplyBean;
import com.ycgis.macall.personalcenter.m.adapterbean.BaseBean;
import com.ycgis.macall.personalcenter.p.callback.OnAdapterLongClickListener;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * created by:   Macall
 * create time:  2023/11/4 15:22
 * copyright:    @ruansee.com
 * Describe:
 */
public class InitiateRecordAdapter extends BaseRecyclerAdapter<BaseBean, BaseRecyclerAdapter.ViewHolder> {
    private OnAdapterLongClickListener<BaseBean> longClickListener;

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

    }

    @Override
    public int getItemViewType(int position) {
        return getDataItem(position).getBaseType();
    }

    public void setLongClickListener(OnAdapterLongClickListener<BaseBean> longClickListener) {
        this.longClickListener = longClickListener;
    }

    @NonNull
    @NotNull
    @Override
    public BaseRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        if (viewType == 1) {
            View view = bindView(parent, R.layout.item_apply_initiate_record);
            ItemApplyInitiateRecordBinding binding = ItemApplyInitiateRecordBinding.bind(view);
            return new ViewDataBindingHolder<>(binding);
        }
        if (viewType == 2) {
            View view = bindView(parent, R.layout.item_apply_audit);
            ItemApplyAuditBinding binding = ItemApplyAuditBinding.bind(view);
            return new ViewDataBindingHolder<>(binding);
        }
        if (viewType == 3) {
            View view = bindView(parent, R.layout.item_apply_my_audit_record);
            ItemApplyMyAuditRecordBinding binding = ItemApplyMyAuditRecordBinding.bind(view);
            return new ViewDataBindingHolder<>(binding);
        }
        if (viewType == 0 || viewType == -1) {
            View view = bindView(parent, R.layout.item_error);
            return new ErrorViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull BaseRecyclerAdapter.ViewHolder viewHolder, int position, @NonNull @NotNull List<Object> payloads) {
        if (payloads.isEmpty()) {
            super.onBindViewHolder(viewHolder, position, payloads);
            return;
        }
        Object o = payloads.get(0);
        if (o instanceof Integer) {
            if (viewHolder instanceof ViewDataBindingHolder) {
                ViewDataBindingHolder holder = (ViewDataBindingHolder) viewHolder;
                if (holder.viewBind instanceof ItemApplyAuditBinding) {
                    ApplyBean dataItem = (ApplyBean) getDataItem(position);
                    ItemApplyAuditBinding binding = (ItemApplyAuditBinding) holder.viewBind;
                    int s = (int) o;
                    if (s == 5) {       //修改是可被修改
                        if (dataItem.isMore()) {
                            binding.btnAuditTitle.setVisibility(View.GONE);
                            binding.btnSel.setVisibility(View.VISIBLE);
                            binding.btnSel.setClickable(false);
                            binding.btnSel.setFocusable(false);
                            binding.btnSel.setChecked(dataItem.isSel());
//                            binding.btnSel.setOnCheckedChangeListener((buttonView, isChecked) -> {
//                                dataItem.setSel(isChecked);
//                                if (btnClickListener != null) {
//                                    btnClickListener.onClickView(null, 0, 1, null);
//                                }
//                            });
                        } else {
                            binding.btnAuditTitle.setVisibility(View.VISIBLE);
                            binding.btnSel.setVisibility(View.GONE);
                        }
                    } else if (s == 3) {       //修改是可被修改
                        binding.btnSel.setChecked(dataItem.isSel());
                    }
                }
            }
        }
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull BaseRecyclerAdapter.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof ViewDataBindingHolder) {
            ViewDataBindingHolder holder = (ViewDataBindingHolder) viewHolder;
            if (holder.viewBind instanceof ItemApplyInitiateRecordBinding) {
                ApplyBean dataItem = (ApplyBean) getDataItem(position);
                ItemApplyInitiateRecordBinding binding = (ItemApplyInitiateRecordBinding) holder.viewBind;
                binding.setData(dataItem);
                binding.tvState.setTextColor(ContextCompat.getColor(context, dataItem.stateColor()));
                binding.tvState.setBackground(ContextCompat.getDrawable(context, dataItem.stateBG()));
            }
            if (holder.viewBind instanceof ItemApplyMyAuditRecordBinding) {
                ApplyBean dataItem = (ApplyBean) getDataItem(position);
                ItemApplyMyAuditRecordBinding binding = (ItemApplyMyAuditRecordBinding) holder.viewBind;
                binding.setData(dataItem);
                binding.tvState.setTextColor(ContextCompat.getColor(context, dataItem.stateColor()));
                binding.tvState.setBackground(ContextCompat.getDrawable(context, dataItem.stateBG()));
            }
            if (holder.viewBind instanceof ItemApplyAuditBinding) {
                ApplyBean dataItem = (ApplyBean) getDataItem(position);
                ItemApplyAuditBinding binding = (ItemApplyAuditBinding) holder.viewBind;
                if (dataItem.isMore()) {
                    binding.btnAuditTitle.setVisibility(View.GONE);
                    binding.btnSel.setVisibility(View.VISIBLE);
                } else {
                    binding.btnAuditTitle.setVisibility(View.VISIBLE);
                    binding.btnSel.setVisibility(View.GONE);
                    binding.btnSel.setOnCheckedChangeListener((buttonView, isChecked) -> {
                        dataItem.setSel(isChecked);
                    });
                }
                binding.setData(dataItem);
                binding.tvSystemName.setText(Html.fromHtml(dataItem.getAuthorityStr()));

                if (longClickListener != null) {
                    binding.getRoot().setOnLongClickListener(new OnAdapterLongClickListener<BaseBean>(dataItem, position) {
                        @Override
                        protected void onLongClick(View v, BaseBean data, int position) {
                            longClickListener.setParam(data, position);
                            longClickListener.onLongClick(v);
                        }
                    });
                }
            }
        }
        if (viewHolder instanceof BaseRecyclerAdapter.ErrorViewHolder) {
            ErrorViewHolder holder = (ErrorViewHolder) viewHolder;
            holder.setEmpty(getDataItem(position).getBaseMessage());
        }
    }


}
