package com.utopiaindustries.qualitychecker.ui.adapter;

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

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.utopiaindustries.qualitychecker.R;
import com.utopiaindustries.qualitychecker.utils.FullScreenImageDialog;

import java.util.List;

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {

    //private byte[] file;
    //private List<String> imageList;
    List<byte[]> imageList;
    private Context context;

    public ImageAdapter(List<byte[]> imageList, Context context) {
        this.imageList = imageList;
        this.context = context;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
        //holder.imageView.setImageBitmap(null);
        holder.imageView.setImageResource(R.drawable.img_load);
        //Bitmap bitmap = BitmapFactory.decodeByteArray( imageList.get(position), 0 , imageList.get(position).length );
        //holder.imageView.setImageBitmap( bitmap );

        /*Glide.with(context)
                .load(bitmap) // Load the Bitmap directly
                .placeholder(R.drawable.img_load)
                .apply(new RequestOptions().centerCrop()) // Optional: apply transformations like center crop
                .into(holder.imageView);*/

        Glide.with(context)
                .load(imageList.get(position)) // Glide will handle the decoding and placeholder
                .placeholder(R.drawable.img_load)
                .apply(new RequestOptions().centerCrop())
                .into(holder.imageView);

        Bitmap bitmap = BitmapFactory.decodeByteArray(imageList.get(position), 0, imageList.get(position).length);

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FullScreenImageDialog dialog = new FullScreenImageDialog(bitmap);
                dialog.show(((AppCompatActivity) v.getContext()).getSupportFragmentManager(), "FullScreenDialog");
            }
        });
    }

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

    public class ImageViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public ImageViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
        }
    }
}
