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

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

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.rs.macall.androidx.basemodel.base.BaseRecyclerAdapter;
import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.databinding.ItemSelectAppBinding;
import com.ycgis.macall.personalcenter.m.adapterbean.AppInfoBean;
import com.ycgis.macall.personalcenter.p.request.LoadAppCenterImage;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * created by:   Macall
 * create time:  2023/11/14 17:56
 * copyright:    @ruansee.com
 * Describe:
 */
public class SelectAppAdapter extends BaseRecyclerAdapter<AppInfoBean, BaseRecyclerAdapter.ViewHolder> {
    private LoadAppCenterImage loadAppCenterImage;

    public SelectAppAdapter(Context context, List<AppInfoBean> 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_app);
            ItemSelectAppBinding viewBind = ItemSelectAppBinding.bind(view);
            return new ViewBindingHolder<>(viewBind);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull BaseRecyclerAdapter.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof BaseRecyclerAdapter.ViewBindingHolder) {
            ViewBindingHolder<ItemSelectAppBinding> holder = (ViewBindingHolder<ItemSelectAppBinding>) viewHolder;
            AppInfoBean dataItem = getDataItem(position);
            holder.viewBind.tvAppName.setText(dataItem.getName());
            holder.viewBind.imageView16.setVisibility(View.GONE);
//            if (1 == dataItem.getAttribution()) {
//                if (loadAppCenterImage == null) {
//                    loadAppCenterImage = new LoadAppCenterImage();
//                }
//                Bitmap bitmap = loadAppCenterImage.loadBitmapText(context, holder.viewBind.ivAppIcon, dataItem.getIcon(), new ImageCallBack() {
//                    @Override
//                    public void imageLoad(ImageView imageView, Bitmap bitmap) {
//                        imageView.setImageBitmap(bitmap);
//                    }
//
//                    @Override
//                    public void imageLoadError(ImageView imageView, String message) {
//                        if (context == null||((Activity) context).isDestroyed()) return;
//                        Glide.with(context)
//                                .load(R.drawable.icon_error2)
//                                .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
//                                .into(imageView);
//                    }
//                });
//                if (bitmap!= null){
//                    holder.viewBind.ivAppIcon.setImageBitmap(bitmap);
//                }
//            } else {
                setImg(holder.viewBind.ivAppIcon, dataItem.getIconUrl());
//            }
            if (dataItem.isSelect()) {
                holder.viewBind.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_select));
                holder.viewBind.tvAppName.setTextColor(ContextCompat.getColor(context, R.color.blue));
            } else {
                holder.viewBind.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_not_select));
                holder.viewBind.tvAppName.setTextColor(ContextCompat.getColor(context, R.color.colorGrey8));
            }
            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());
            }
        }
    }

    @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 a = (int) o;
                if (a == 5) {
                    if (viewHolder instanceof BaseRecyclerAdapter.ViewBindingHolder) {
                        AppInfoBean dataItem = getDataItem(position);
                        ViewBindingHolder<ItemSelectAppBinding> holder = (ViewBindingHolder<ItemSelectAppBinding>) viewHolder;
                        if (dataItem.isSelect()) {
                            holder.viewBind.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_select));
//                            holder.viewBind.imageView16.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_arrow_bottom));
                            holder.viewBind.tvAppName.setTextColor(ContextCompat.getColor(context, R.color.blue));
                        } else {
                            holder.viewBind.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_not_select));
                            holder.viewBind.tvAppName.setTextColor(ContextCompat.getColor(context, R.color.colorGrey8));
//                            holder.viewBind.imageView16.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_arrow_right));
                        }
                    }
                }
            }
        }
    }

    private void setImg(ImageView img, String url) {
//        String imgUrl = url;
//        String imgUrl = "http://" + RuanseeApplication.getAppCache().getIp() + ":8080" + url;
        //图片加载自己实现
        Glide.with(img)
                .load(url)
                .placeholder(R.drawable.icon_error2)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                .error(R.drawable.icon_error2)
                .into(img);
    }
}
