package com.example.mynotepad.MenuFeatures.AllNotes.MyNoteRecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import com.example.mynotepad.R;

import java.util.List;

public class MyAllNotesAdaptor extends RecyclerView.Adapter<MyAllNotesAdaptor.ViewHolder> {

    private List<MyNote> notes;
    private OnAllNotesItemClickListener onItemClickListener;

    public MyAllNotesAdaptor(List<MyNote> notes) {
        this.notes = notes;
    }

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

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.titleTV.setText(notes.get(position).getTitle());
        holder.descriptionTV.setText(notes.get(position).getDescription());
        holder.isArchived = notes.get(position).isArchived();
    }

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

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

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        protected TextView titleTV, descriptionTV;
        protected boolean isArchived;

        public ViewHolder(View itemView) {
            super(itemView);
            titleTV = itemView.findViewById(R.id.titleTv);
            descriptionTV = itemView.findViewById(R.id.descriptionTv);
            isArchived = false;
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

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

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


}
