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.ItemHomeAppBinding;
import com.ycgis.macall.personalcenter.databinding.ItemSelectAppBinding;
import com.ycgis.macall.personalcenter.m.adapterbean.AppInfoBean;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * created by:   Macall
 * create time:  2023/11/24 16:00
 * copyright:    @ruansee.com
 * Describe:
 */
public class WorkbenchCenterAdapter extends BaseRecyclerAdapter<AppInfoBean, BaseRecyclerAdapter.ViewHolder>{

    public WorkbenchCenterAdapter(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_empty_xiao, parent, false);
            return new ErrorViewHolder(view);
        }
        if (viewType == 1){
            View view = bindView(parent, R.layout.item_home_app);
            ItemHomeAppBinding bind = ItemHomeAppBinding.bind(view);
            return new ViewBindingHolder<>(bind);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull BaseRecyclerAdapter.ViewHolder viewHolder, int position) {

        if (viewHolder instanceof BaseRecyclerAdapter.ViewBindingHolder) {
            ViewBindingHolder<ItemHomeAppBinding> holder = (ViewBindingHolder<ItemHomeAppBinding>) viewHolder;
            AppInfoBean dataItem = getDataItem(position);
            holder.viewBind.homeAppName.setText(dataItem.getName());
            setImg(holder.viewBind.homeAppLogo, dataItem.getIconUrl());
            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());
            }
        }
    }
    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);
    }
}
