package com.draw.painting.canvas.adapters;

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

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.draw.painting.canvas.R;
import com.draw.painting.canvas.core.SystemHelper;
import com.draw.painting.canvas.onClickListener;

import java.util.List;

public class FeaturedSketchAdapter extends RecyclerView.Adapter<FeaturedSketchAdapter.FeaturedVH> {

    private Context mContext;
    private List<String> featuredList;
    private onClickListener listener;

    public FeaturedSketchAdapter(Context context, List<String> featuredList) {
        this.mContext = context;
        this.featuredList = featuredList;
    }

    public void setListener(onClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public FeaturedVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_featured, parent, false);
        
        // 动态设置卡片宽度为屏幕宽度的 85%
        int screenWidth = SystemHelper.getScreen(mContext).x;
        int cardWidth = (int) (screenWidth * 0.85);
        
        CardView cardView = view.findViewById(R.id.card_featured);
        ViewGroup.LayoutParams params = cardView.getLayoutParams();
        params.width = cardWidth;
        cardView.setLayoutParams(params);
        
        return new FeaturedVH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FeaturedVH holder, int position) {
        String filePath = featuredList.get(position);
        Bitmap bitmap = SystemHelper.loadImageFromAssets(mContext, filePath);
        holder.imFeatured.setImageBitmap(bitmap);

        holder.cardFeatured.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClickItem(false, filePath);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return featuredList.size();
    }

    public static class FeaturedVH extends RecyclerView.ViewHolder {
        private ImageView imFeatured;
        private CardView cardFeatured;

        public FeaturedVH(@NonNull View itemView) {
            super(itemView);
            imFeatured = itemView.findViewById(R.id.im_featured);
            cardFeatured = itemView.findViewById(R.id.card_featured);
        }
    }
}

