package com.example.mynotepad.MenuFeatures.Pics.Gallery;

import android.content.Context;
import android.graphics.BitmapFactory;
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.example.mynotepad.R;
import java.util.List;

public class GalleryAdaptor extends RecyclerView.Adapter<GalleryAdaptor.GalleryViewHolder> {
    private List<MyGalleryPic> picsUrlAndTxt;
    private OnGalleryItemClickListener onItemClickListener;

    public GalleryAdaptor(List<MyGalleryPic> picsUrlAndTxt) {
        this.picsUrlAndTxt = picsUrlAndTxt;
    }

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

    @Override
    public void onBindViewHolder(@NonNull GalleryViewHolder holder, int position) {
        holder.textView.setText(picsUrlAndTxt.get(position).getText());
        System.out.println(picsUrlAndTxt.get(position).getUrl() + "                                         !!!!!");
        holder.imageView.setImageBitmap(BitmapFactory.decodeFile(picsUrlAndTxt.get(position).getUrl()));
    }

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

    public OnGalleryItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnGalleryItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public class GalleryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener , View.OnLongClickListener {
        TextView textView;
        ImageView imageView;

        public GalleryViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_gallery);
            imageView = itemView.findViewById(R.id.img_gallery);
        }

        @Override
        public void onClick(View view) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClicked(picsUrlAndTxt.get(getAdapterPosition()));
            }
        }

        @Override
        public boolean onLongClick(View view) {
            if (onItemClickListener != null)
                onItemClickListener.onItemLongClicked(picsUrlAndTxt.get(getAdapterPosition()));
            return true;
        }
    }
}
