package com.draw.painting.canvas.adapters;

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.canvas.CanvasApp;
import com.draw.painting.canvas.R;
import com.draw.painting.canvas.core.SystemHelper;
import com.draw.painting.canvas.presentation.ARCameraActivity;

import java.util.List;

public class SketchPreviewAdapter extends RecyclerView.Adapter<SketchPreviewAdapter.HomeVH> {

    private Context mCon;
    private List<String> data;

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

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

    @Override
    public void onBindViewHolder(@NonNull HomeVH holder, int position) {
        String name = data.get(position);
        Log.d("---------tt", "---------name=" + name);
        
        // 加载图片
        Bitmap bitmap = SystemHelper.loadImageFromAssets(mCon, name);
        holder.previewIm.setImageBitmap(bitmap);
        
        // 点击事件 - 直接进入AR
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mCon, ARCameraActivity.class);
                intent.putExtra(CanvasApp.Key_Camera, name);
                mCon.startActivity(intent);
            }
        });
    }

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

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

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

