package com.example.mynotepad.MenuFeatures.AllArchivedNotes.MyArchivedNotesRecyclerView;

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.MenuFeatures.AllNotes.MyNoteRecyclerView.MyNote;
import com.example.mynotepad.MenuFeatures.Calender.MyCalenderRecyclerView.TimePicker.MyDate;
import com.example.mynotepad.R;
import java.util.ArrayList;
import java.util.List;

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

    private List<MyNote> notes;
    private List<MyDate> dates;
    private ArrayList<DateOrNote> dateOrNoteList;
    private OnArchivedItemClickListener onArchivedItemClickListener;

    public MyArchivedAdaptor(List<MyNote> notes, List<MyDate> dates) {
        this.notes = notes;
        this.dates = dates;
        dateOrNoteList = new ArrayList<>();
        setarchivedNotesAndDates();
    }

    private void setarchivedNotesAndDates() {
        setNotes();
        setDates();
    }

    private void setDates() {
        for (MyDate myDate : dates) {
            if (myDate.isArchived()) {
                DateOrNote date = new DateOrNote(myDate.getId(), "date", myDate.getDate(), myDate.getDescription());
                dateOrNoteList.add(date);
            }
        }
    }

    private void setNotes() {
        for (MyNote myNote : notes) {
            if (myNote.isArchived()) {
                DateOrNote note = new DateOrNote(myNote.getId(), "note", myNote.getTitle(), myNote.getDescription());
                dateOrNoteList.add(note);
            }
        }
    }

    public void setOnArchivedItemClickListener(OnArchivedItemClickListener onArchivedItemClickListener) {
        this.onArchivedItemClickListener = onArchivedItemClickListener;
    }

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

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

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


    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 (onArchivedItemClickListener != null) {
                onArchivedItemClickListener.onItemClicked(dateOrNoteList.get(getAdapterPosition()));
            }
        }

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

    public ArrayList<DateOrNote> getDateOrNoteList() {
        return dateOrNoteList;
    }
}
