package com.paint.sweet.drawing.sweetadapter;

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.recyclerview.widget.RecyclerView;

import com.paint.sweet.drawing.R;

import java.util.List;

// 泛型使用Pair替代Kotlin的Pair，这里用自定义的简单数据类封装图片资源和类型文字
public class DrawingImagePagerAdapter extends RecyclerView.Adapter<DrawingImagePagerAdapter.ViewHolder> {

    // 自定义数据类，替代Kotlin的Pair
    public static class ImageTypeBean {
        private int imageRes; // 图片资源ID
        private String typeText; // 图片类型文字

        public ImageTypeBean(int imageRes, String typeText) {
            this.imageRes = imageRes;
            this.typeText = typeText;
        }

        public int getImageRes() {
            return imageRes;
        }

        public String getTypeText() {
            return typeText;
        }
    }

    // ViewHolder内部类
    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivImage;
        TextView tvType;

        public ViewHolder(View itemView) {
            super(itemView);
            // 绑定控件
            ivImage = itemView.findViewById(R.id.iv_image);
            tvType = itemView.findViewById(R.id.tv_image_type);
        }
    }

    private List<ImageTypeBean> imageList; // 数据源

    // 构造方法，传入数据源
    public DrawingImagePagerAdapter(List<ImageTypeBean> imageList) {
        this.imageList = imageList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 加载子项布局
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_image_label, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 获取当前位置的数据源
        ImageTypeBean bean = imageList.get(position);
        // 设置图片和文字
        holder.ivImage.setImageResource(bean.getImageRes());
        holder.tvType.setText(bean.getTypeText());
    }

    @Override
    public int getItemCount() {
        // 返回数据源长度，空值保护避免空指针
        return imageList == null ? 0 : imageList.size();
    }
}