package com.draw.painting.line.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
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.line.DrawingApp;
import com.draw.painting.line.R;
import com.draw.painting.line.manager.AppHelper;
import com.draw.painting.line.ui.DrawActivity;

import java.util.List;

public class ImageListAdapter extends RecyclerView.Adapter<ImageListAdapter.ImageVH> {


    private Context mCon;
    private List<String> data;


    public ImageListAdapter(Context mCon, List<String> bitmapList) {
        this.mCon = mCon;
        data = bitmapList;
    }

    @NonNull
    @Override
    public ImageVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_image, parent, false);
        return new ImageVH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageVH holder, int position) {

        String name = data.get(position);
        Log.d("---------tt","---------name="+name);
        Bitmap bitmap = AppHelper.loadImageFromAssets(mCon, name);
        holder.previewIm.setImageBitmap(bitmap);
        
        // 重置选中状态
        holder.cardView.setSelected(false);
        
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 设置选中状态，显示反馈
                holder.cardView.setSelected(true);
                
                // 延迟重置，让用户看到反馈
                holder.cardView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        holder.cardView.setSelected(false);
                    }
                }, 300);
                
                Intent intent = new Intent(mCon, DrawActivity.class);
                intent.putExtra(DrawingApp.Key_Camera,name);
                mCon.startActivity(intent);
            }
        });


    }

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

    public static class ImageVH extends RecyclerView.ViewHolder {
        private ImageView previewIm;
        private CardView cardView;

        public ImageVH(@NonNull View itemView) {
            super(itemView);
            previewIm = itemView.findViewById(R.id.imPreview);
            cardView = itemView.findViewById(R.id.card_view);
        }


    }
}

