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 android.widget.TextView;

import androidx.annotation.NonNull;

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.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:  2022/11/10 19:21
 * copyright:    @ruansee.com
 * Describe:
 */
public class ApplicationGuideAdapter extends BaseRecyclerAdapter<AppInfoBean,BaseRecyclerAdapter.ViewHolder> {
    private LoadAppCenterImage loadAppCenterImage;

    public ApplicationGuideAdapter(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);
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_app_operation,parent,false);
//        ItemAppInfoBinding inflate = ItemAppInfoBinding.inflate(LayoutInflater.from(parent.getContext()));
        return new AppHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull BaseRecyclerAdapter.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof AppHolder) {
            AppHolder holder = (AppHolder) viewHolder;
            AppInfoBean dataItem = getDataItem(position);
//            if (1 == dataItem.getAttribution()) {
//                if (loadAppCenterImage == null) {
//                    loadAppCenterImage = new LoadAppCenterImage();
//                }
//                Bitmap bitmap = loadAppCenterImage.loadBitmapText(context, holder.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.ic_load_imag_eerror)
//                                .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
//                                .into(imageView);
//                    }
//                });
//                if (bitmap!= null){
//                    holder.ivAppIcon.setImageBitmap(bitmap);
//                }
//            } else {
                setImg(holder.ivAppIcon, dataItem.getIconUrl());
//            }
//            holder.ivAppIcon.setImageDrawable(ContextCompat.getDrawable(context,dataItem.getIcon()));
            holder.tvAppName.setText(dataItem.getName());
            holder.tvAppCompany.setText(dataItem.getServiceProvider());
        }
        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());
            }
        }
    }

    private void setImg(ImageView img,String url){
        //图片加载自己实现
        Glide.with(img)
                .load(url)
                .placeholder(R.drawable.icon_error2)
                .error(R.drawable.ic_load_imag_eerror)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                .into(img);
    }

    private class AppHolder extends ViewHolder {
        private ImageView ivAppIcon;
        private TextView tvAppCompany;
        private TextView tvAppName;

        public AppHolder(View itemView) {
            super(itemView);
            ivAppIcon = itemView.findViewById(R.id.iv_app_icon);
            tvAppCompany = itemView.findViewById(R.id.tv_app_company);
            tvAppName = itemView.findViewById(R.id.tv_app_name);
            if (listClickListener!=null){
                itemView.findViewById(R.id.linearLayout3).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listClickListener.onItemClickListener(v,getAdapterPosition());
                    }
                });
            }
        }
    }
}
