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

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

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.rs.macall.androidx.basemodel.base.BaseRecyclerAdapter;
import com.rs.macall.androidx.basemodel.utils.StringUtil;
import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.databinding.ItemWorkManageAppBinding;
import com.ycgis.macall.personalcenter.m.adapterbean.WorkAppBean;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * created by:   Macall
 * create time:  2023/11/2 17:47
 * copyright:    @ruansee.com
 * Describe:
 */
public class WorkCustomAppAdapter extends BaseRecyclerAdapter<WorkAppBean,BaseRecyclerAdapter.ViewHolder> {


    public WorkCustomAppAdapter(Context context, List<WorkAppBean> 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_work_manage_app);
            ItemWorkManageAppBinding binding = ItemWorkManageAppBinding.bind(view);
            return new ViewDataBindingHolder<>(binding);
        }
        if (viewType == 0|| viewType == -1){
            View view  = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_error,parent,false);
            return new ErrorViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull BaseRecyclerAdapter.ViewHolder viewHolder, int position) {
        WorkAppBean dataItem = getDataItem(position);
        if (viewHolder instanceof ViewDataBindingHolder){
            ViewDataBindingHolder<ItemWorkManageAppBinding> holder = (ViewDataBindingHolder<ItemWorkManageAppBinding> )viewHolder;
            holder.viewBind.setData(dataItem);
            holder.setChileViewClick(holder.viewBind.btnAddApp,position,1,"");
            holder.setChileViewClick(holder.viewBind.btnDeleteApp,position,2,"");
            if (StringUtil.isNullOrEmpty(dataItem.getIconUrl())){
                if (dataItem.getApplicationIcon() == null){
                    Glide.with(context).load(dataItem.getIcon()).into(holder.viewBind.ivAppIcon);
                }else {
                    Glide.with(context).load(dataItem.getApplicationIcon()).into(holder.viewBind.ivAppIcon);
                }
            }else {
                Glide.with(context).load(dataItem.getIconUrl()).into(holder.viewBind.ivAppIcon);
            }
        }
        if (viewHolder instanceof BaseRecyclerAdapter.ErrorViewHolder){
            ErrorViewHolder holder = (ErrorViewHolder)viewHolder;
            if (dataItem.getBaseType() == 0){
                holder.setEmpty(dataItem.getBaseMessage());
            }else {
                holder.setError(dataItem.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){
//                int s = (int)o;
//                if (s)
                WorkAppBean dataItem = getDataItem(position);
                if (viewHolder instanceof ViewDataBindingHolder) {
                    ViewDataBindingHolder<ItemWorkManageAppBinding> holder = (ViewDataBindingHolder<ItemWorkManageAppBinding>) viewHolder;
                    if (dataItem.isAdd()){
                        holder.viewBind.btnAddApp.setVisibility(View.GONE);
                        holder.viewBind.btnDeleteApp.setVisibility(View.VISIBLE);
                    }else {
                        holder.viewBind.btnDeleteApp.setVisibility(View.GONE);
                        holder.viewBind.btnAddApp.setVisibility(View.VISIBLE);
                    }
                }
            }
        }
    }
}
