package com.example.mynotepad.MenuFeatures.AllArchivedNotes;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.mynotepad.MenuFeatures.AllArchivedNotes.MyArchivedNotesRecyclerView.DateOrNote;
import com.example.mynotepad.MenuFeatures.AllArchivedNotes.MyArchivedNotesRecyclerView.MyArchivedAdaptor;
import com.example.mynotepad.MenuFeatures.AllArchivedNotes.MyArchivedNotesRecyclerView.OnArchivedItemClickListener;
import com.example.mynotepad.MenuFeatures.AllNotes.DataBase.DateEntity;
import com.example.mynotepad.MenuFeatures.AllNotes.DataBase.Note;
import com.example.mynotepad.MenuFeatures.AllNotes.MyNoteRecyclerView.MyNote;
import com.example.mynotepad.MenuFeatures.Calender.MyCalenderRecyclerView.TimePicker.MyDate;
import com.example.mynotepad.MenuFeatures.CustomToolbarOption;
import com.example.mynotepad.MenuFeatures.CustomToolbarOptionListener;
import com.example.mynotepad.MenuFeatures.Utils;
import com.example.mynotepad.R;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;

import static com.example.mynotepad.MainActivity.noteDataBase;


public class ArchivedNoesListFragment extends Fragment {
    private FloatingActionButton addNoteButton;
    private CustomToolbarOption customToolbarOption;
    private FrameLayout allNoteToolBar;
    private RecyclerView recyclerView;
    private MyArchivedAdaptor myarchivedAdaptor;
    private List<Note> entityNotes;
    private List<DateEntity> entityDates;
    private List<MyNote> myNotes;
    private List<MyDate> myDates;
    public static DateOrNote chosenDateOrNote;
    private NavController navController;
    private LinearLayout allNotesListLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_all_notes_list, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
        setBg(Utils.sTheme);
    }

    private void setBg(String sTheme) {
        switch (sTheme) {
            case "dark":
                allNotesListLayout.setBackgroundResource(R.drawable.main_bg_dark);
                break;
            case "bright":
                allNotesListLayout.setBackgroundResource(R.drawable.main_bg_bright);
                break;
        }
    }

    private void init(View view) {
        // get data from database
        setAllNoteListAndTitles();
        setAllDateNoteListAndTitles();
        customToolbarOption = view.findViewById(R.id.customToolbarOption);
        customToolbarOption.setVisibility(View.GONE);
        allNoteToolBar = view.findViewById(R.id.toolbar);
        allNoteToolBar.setEnabled(false);
        navController = Navigation.findNavController(view);
        allNotesListLayout = view.findViewById(R.id.allNoteListLayout);

        //hide add button in favourite list
        addNoteButton = view.findViewById(R.id.addButton);
        addNoteButton.setVisibility(View.GONE);

        // show list with recycler view
        recyclerView = view.findViewById(R.id.recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        myarchivedAdaptor = new MyArchivedAdaptor(myNotes, myDates);
        recyclerView.setAdapter(myarchivedAdaptor);
        recyclerView.setLayoutManager(linearLayoutManager);

        addListeners();
    }

    private void addListeners() {

        // add listener to recyclerView
        myarchivedAdaptor.setOnArchivedItemClickListener(new OnArchivedItemClickListener() {
            @Override
            public void onItemClicked(DateOrNote item) {
                chosenDateOrNote = item;
                if (item.getType().equals("note")) {
                    //set noteFragment data
                    final Bundle bundle = new Bundle();
                    bundle.putString("title", item.getTitle());
                    bundle.putString("bodyTxt", item.getDescription());
                    bundle.putString("type", "noteDate");
                    bundle.putString("noteDateType", item.getType());
                    //change the fragment
                    navController.navigate(R.id.action_archivedNoesListFragment_to_noteFragment3, bundle);
                } else if (item.getType().equals("date")) {
                    //set noteFragment data
                    final Bundle bundle = new Bundle();
                    bundle.putString("title", item.getTitle());
                    bundle.putString("bodyTxt", item.getDescription());
                    bundle.putString("type", "noteDate");
                    bundle.putString("noteDateType", item.getType());
                    //change the fragment
                    navController.navigate(R.id.action_archivedNoesListFragment_to_noteFragment3, bundle);
                }

            }

            @Override
            public void onItemLongClicked(DateOrNote item) {
                chosenDateOrNote = item;
                customToolbarOption.setVisibility(View.VISIBLE);
                customToolbarOption.hideRecycleBin();
                allNoteToolBar.setVisibility(View.GONE);
            }
        });


        //add listener to custom toolbar option and set being archived or not to be saved in database
        customToolbarOption.setCustomToolbarOptionListener(new CustomToolbarOptionListener() {
            @Override
            public void onStarClicked() {
                customToolbarOption.setBlackStar();
                myarchivedAdaptor.getDateOrNoteList().remove(chosenDateOrNote);
                updateToUnarchiveInDataBase();
                Toast.makeText(getContext(), "unarchived", Toast.LENGTH_SHORT).show();
            }


            @Override
            public void onDeleteClicked() {
            }

            @Override
            public void onCloseClicked() {
                closeClicked(customToolbarOption);
            }
        });
    }


    private void updateToUnarchiveInDataBase() {
        if (chosenDateOrNote.getType().equals("note")) {
            noteDataBase.noteDao().updateNote(chosenDateOrNote.getTitle(), chosenDateOrNote.getDescription(), false, chosenDateOrNote.getId());
        } else if (chosenDateOrNote.getType().equals("date")) {
            noteDataBase.dateNoreDAO().updateDate(chosenDateOrNote.getTitle(), chosenDateOrNote.getDescription(), false, chosenDateOrNote.getId());
        }
    }


    private void setAllNoteListAndTitles() {
        entityNotes = noteDataBase.noteDao().getAll();
        myNotes = new ArrayList<>();
        for (Note note : entityNotes) {
            MyNote myNote = new MyNote(note.getTitle(), note.getDescription(), note.isarchived(), note.getID());
            myNotes.add(myNote);
        }
    }

    private void setAllDateNoteListAndTitles() {
        //get data from database to set in recyclerView
        entityDates = noteDataBase.dateNoreDAO().getAllDateNotes();
        myDates = new ArrayList<>();
        for (DateEntity dateEntity : entityDates) {
            MyDate myDate = new MyDate(dateEntity.getDate(), dateEntity.getDescription(), dateEntity.isarchived(), dateEntity.getID());
            myDates.add(myDate);
        }
    }


    protected void closeClicked(CustomToolbarOption customToolbarOption) {
        customToolbarOption.setVisibility(View.GONE);
        allNoteToolBar.setVisibility(View.VISIBLE);
    }
}