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

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

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.ItemApplyAuditRecordBinding;
import com.ycgis.macall.personalcenter.m.adapterbean.ApplyAuditBean;
import com.ycgis.macall.personalcenter.m.adapterbean.BaseBean;
import com.ycgis.macall.personalcenter.p.callback.ImageCallBack;
import com.ycgis.macall.personalcenter.p.request.AsyncBitmapLoader;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * created by:   Macall
 * create time:  2023/11/4 15:29
 * copyright:    @ruansee.com
 * Describe:
 */
public class ApplyAuditRecordAdapter extends BaseRecyclerAdapter<BaseBean, BaseRecyclerAdapter.ViewHolder> {

    public ApplyAuditRecordAdapter(Context context, List<BaseBean> 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 == 1){
            View view = bindView(parent, R.layout.item_apply_audit_record);
            ItemApplyAuditRecordBinding bind = ItemApplyAuditRecordBinding.bind(view);
            return new ViewDataBindingHolder<>(bind);
        }
        if (viewType == 0||viewType == -1){
            View view = bindView(parent, R.layout.item_error_empty_xiao);
            return new ErrorViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull BaseRecyclerAdapter.ViewHolder viewHolder, int position) {
        BaseBean dataItem = getDataItem(position);
        if (viewHolder instanceof BaseRecyclerAdapter.ViewDataBindingHolder){
            ViewDataBindingHolder<ItemApplyAuditRecordBinding> holder = (ViewDataBindingHolder<ItemApplyAuditRecordBinding>) viewHolder;
            ApplyAuditBean bean = (ApplyAuditBean) dataItem;
            holder.viewBind.setData(bean);
            holder.viewBind.tvResult.setTextColor(ContextCompat.getColor(context,bean.stateColor()));
            holder.viewBind.tvResult.setBackground(ContextCompat.getDrawable(context,bean.stateBG()));
            if ("1".equals(bean.getSealStatus())){
                loadSeal(bean,holder.viewBind.ivYz);
            }
        }
        if (viewHolder instanceof BaseRecyclerAdapter.ErrorViewHolder){{
            ErrorViewHolder holder =(ErrorViewHolder) viewHolder;
            if (dataItem.getBaseType()== 0){
                holder.setEmpty(dataItem.getBaseMessage());
            }else {
                holder.setError(dataItem.getBaseMessage());
            }
        }}
    }

    private void loadSeal(ApplyAuditBean bean, ImageView iv){
        AsyncBitmapLoader asyncBitmapLoader = new AsyncBitmapLoader();
        iv.setTag(bean.getSealDeptId());
        Bitmap bitmap = asyncBitmapLoader.loadSeal( bean.getId(), bean.getSealDeptId(), iv,new ImageCallBack() {
            @Override
            public void imageLoad(ImageView imageView, Bitmap bitmap) {
                if (bean.getSealDeptId().equals(imageView.getTag())){
                    imageView.setImageBitmap(bitmap);
                }
            }

            @Override
            public void imageLoadError(ImageView imageView, String message) {

            }
        });
        if (bitmap!=null){
            iv.setImageBitmap(bitmap);
        }
    }
}
