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 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.rs.macall.androidx.basemodel.utils.StringUtil;
import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.m.adapterbean.AppBean;
import com.ycgis.macall.personalcenter.p.app.AppCache;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * created by:   Macall
 * create time:  2023/10/27 15:05
 * copyright:    @ruansee.com
 * Describe:
 */
public class HomeAppListAdapter extends BaseRecyclerAdapter<AppBean,BaseRecyclerAdapter.ViewHolder> {

    public HomeAppListAdapter(Context context, List<AppBean> 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);
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_app, parent, false);
        return new SimpleViewHolder(view).findViewById(R.id.home_app_logo).findViewById(R.id.home_app_name);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull BaseRecyclerAdapter.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof BaseRecyclerAdapter.SimpleViewHolder) {
            SimpleViewHolder holder = (SimpleViewHolder) viewHolder;
            ImageView view = holder.getView(R.id.home_app_logo, ImageView.class);
            TextView view1 = holder.getView(R.id.home_app_name, TextView.class);
            AppBean dataItem = getDataItem(position);
            view1.setText(dataItem.getAppName());
            if (StringUtil.hasContent(dataItem.getIconUrl())) {
                setImg(view, dataItem.getIconUrl());
            } else if (dataItem.getIcon() != 0) {
                view.setImageDrawable(ContextCompat.getDrawable(context, dataItem.getIcon()));
            }
        }
        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 = AppCache.BASE_IMAGE_URL + url;
        //图片加载自己实现
        Glide.with(img)
                .load(imgUrl)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                .into(img);
    }
}
