package com.example.mynotepad.MenuFeatures.Calender;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
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.AllNotes.DataBase.DateEntity;
import com.example.mynotepad.MenuFeatures.Calender.MyCalenderRecyclerView.MyCalenderAdaptor;
import com.example.mynotepad.MenuFeatures.Calender.MyCalenderRecyclerView.OnCalenderItemClickListener;
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 CalenderNotesListFragment extends Fragment {

    private FloatingActionButton addDateBtn;
    private CustomToolbarOption customToolbarOption;
    private FrameLayout calenderToolbar;
    private RecyclerView recyclerView;
    private MyCalenderAdaptor myAdaptor;
    private List<MyDate> myDates;
    private List<DateEntity> entityDates;
    public static MyDate myChosenDateNote;
    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 onResume() {
        super.onResume();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        myAdaptor = new MyCalenderAdaptor(myDates);
        recyclerView.setAdapter(myAdaptor);
        recyclerView.setLayoutManager(linearLayoutManager);
        addListeners();
    }

    @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) {

        setAllDateNoteListAndTitles();
        addDateBtn = view.findViewById(R.id.addButton);

        customToolbarOption = view.findViewById(R.id.customToolbarOption);
        customToolbarOption.setVisibility(View.GONE);
        calenderToolbar = view.findViewById(R.id.toolbar);
        calenderToolbar.setEnabled(false);
        addDateBtn = view.findViewById(R.id.addButton);
        navController = Navigation.findNavController(view);
        allNotesListLayout = view.findViewById(R.id.allNoteListLayout);

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


    private void addListeners() {
        addDateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                navController.navigate(R.id.action_calenderNotesListFragment_to_timePickerFragment);
            }
        });

        // add listener to recyclerView
        myAdaptor.setOnCalenderItemClickListener(new OnCalenderItemClickListener() {
            @Override
            public void onItemClicked(MyDate myDate) {
                myChosenDateNote = myDate;
                //set noteFragment data
                final Bundle bundle = new Bundle();
                bundle.putString("title", myChosenDateNote.getDate());
                bundle.putString("bodyTxt", myChosenDateNote.getDescription());
                bundle.putString("type", "date");
                //change the fragment
                navController.navigate(R.id.action_calenderNotesListFragment_to_noteFragment2, bundle);
            }

            @Override
            public void onItemLongClicked(MyDate myDate) {
                customToolbarOption.setVisibility(View.VISIBLE);
                calenderToolbar.setVisibility(View.GONE);
                myChosenDateNote = myDate;
                if (myChosenDateNote.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() {
                deleteClicked();
            }

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


    private void closeClicked() {
        customToolbarOption.setVisibility(View.GONE);
        calenderToolbar.setVisibility(View.VISIBLE);
    }

    private void deleteClicked() {
        AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
        alert.setMessage("are you sure you wanna delete it ??? ");
        alert.setPositiveButton("yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                noteDataBase.dateNoreDAO().deleteDateNote(entityDates.get(myDates.indexOf(myChosenDateNote)));
                myDates.remove(myChosenDateNote);
                myAdaptor.notifyDataSetChanged();
            }
        });
        alert.setNegativeButton("no", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.cancel();
            }
        });
        alert.show();
        calenderToolbar.setVisibility(View.VISIBLE);
        customToolbarOption.setVisibility(View.GONE);
    }

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

    private void updatearchivedDatesInDataBase() {
        noteDataBase.dateNoreDAO().updateDate(myChosenDateNote.getDate(), myChosenDateNote.getDescription(), myChosenDateNote.isArchived(), myChosenDateNote.getId());
    }


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

}