package com.example.mynotepad.MenuFeatures.AllNotes;

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.Dialogs.CustomDialog;
import com.example.mynotepad.Dialogs.onCustomDialogClickListener;
import com.example.mynotepad.MenuFeatures.AllNotes.DataBase.Note;
import com.example.mynotepad.MenuFeatures.AllNotes.MyNoteRecyclerView.MyAllNotesAdaptor;
import com.example.mynotepad.MenuFeatures.AllNotes.MyNoteRecyclerView.MyNote;
import com.example.mynotepad.MenuFeatures.AllNotes.MyNoteRecyclerView.OnAllNotesItemClickListener;
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 AllNotesListFragment extends Fragment {
    private FloatingActionButton addNoteButton;

    private CustomToolbarOption customToolbarOption;
    private FrameLayout allNoteToolBar;
    private RecyclerView recyclerView;
    private MyAllNotesAdaptor myAdaptor;
    private List<Note> entityNotes;
    private List<MyNote> myNotes;
    public static MyNote myChosenNote;
    private NavController navController;
    private LinearLayout allNotesListLayout;
    private CustomDialog customDialog;

    @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);
        addListeners();
    }


    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();

        customToolbarOption = view.findViewById(R.id.customToolbarOption);
        customToolbarOption.setVisibility(View.GONE);
        allNoteToolBar = view.findViewById(R.id.toolbar);
        allNoteToolBar.setEnabled(false);
        addNoteButton = view.findViewById(R.id.addButton);
        navController = Navigation.findNavController(view);
        allNotesListLayout = view.findViewById(R.id.allNoteListLayout);
        customDialog = new CustomDialog();

        // show list with recycler view
        recyclerView = view.findViewById(R.id.recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        myAdaptor = new MyAllNotesAdaptor(myNotes);

        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(myAdaptor);

    }

    private void addListeners() {
        addNoteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showAddAlert();
            }
        });
        // add listener to recyclerView
        myAdaptor.setOnItemClickListener(new OnAllNotesItemClickListener() {
            @Override
            public void onItemClicked(MyNote myNote) {
                myChosenNote = myNote;
                //set noteFragment data
                final Bundle bundle = new Bundle();
                bundle.putString("title", myNote.getTitle());
                bundle.putString("bodyTxt", myNote.getDescription());
                bundle.putString("type", "note");

                //change the fragment
                navController.navigate(R.id.action_allNotesListFragment_to_noteFragment, bundle);
            }

            @Override
            public void onItemLongClicked(MyNote myNote) {
                customToolbarOption.setVisibility(View.VISIBLE);
                allNoteToolBar.setVisibility(View.GONE);
                myChosenNote = myNote;
                if (myChosenNote.isArchived()) {
                    customToolbarOption.setYellowStar();
                } else {
                    customToolbarOption.setBlackStar();
                }
            }
        });

        //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() {
                starClicked();
            }

            @Override
            public void onDeleteClicked() {
                Note note = entityNotes.get(myNotes.indexOf(myChosenNote));
                deleteClicked(note);
            }

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


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

    protected void deleteClicked(final Note note) {
        customDialog.showAddTxtDialog(getActivity(), "are you sure you wanna delete it ??? ");
        customDialog.setOnCustomDialogClickListener(new onCustomDialogClickListener() {
            @Override
            public void yesClicked() {
                noteDataBase.noteDao().deleteNote(note);
                myNotes.remove(myChosenNote);
                myAdaptor.notifyDataSetChanged();
            }

            @Override
            public void noClicked() {
            }
        });
        allNoteToolBar.setVisibility(View.VISIBLE);
        customToolbarOption.setVisibility(View.GONE);
        allNoteToolBar.setVisibility(View.VISIBLE);
    }

    protected void starClicked() {
        if (myChosenNote.isArchived()) {
            customToolbarOption.setBlackStar();
            myChosenNote.setArchived(false);
            updatearchivedNotesInDataBase();
            Toast.makeText(getContext(), "unarchived", Toast.LENGTH_SHORT).show();
        } else {
            customToolbarOption.setYellowStar();
            myChosenNote.setArchived(true);
            updatearchivedNotesInDataBase();
            Toast.makeText(getContext(), "archived", Toast.LENGTH_SHORT).show();
        }
    }

    private void updatearchivedNotesInDataBase() {
        noteDataBase.noteDao().updateNote(myChosenNote.getTitle(), myChosenNote.getDescription()
                , myChosenNote.isArchived(), myChosenNote.getId());
    }


    private void showAddAlert() {
        customDialog.showAddTxtDialog(getActivity(), "Do you want to create a new note ?");
        customDialog.setOnCustomDialogClickListener(new onCustomDialogClickListener() {
            @Override
            public void yesClicked() {
                showABlankNote();
            }
            @Override
            public void noClicked() {
            }
        });

    }

    private void showABlankNote() {
        //set noteFragment data
        final Bundle bundle = new Bundle();
        bundle.putString("title", "");
        bundle.putString("bodyTxt", "");
        bundle.putString("type", "note");

        //change the fragment
        navController.navigate(R.id.action_allNotesListFragment_to_noteFragment, bundle);
    }


    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);
        }
    }

}