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

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.example.mynotepad.Dialogs.CustomDialog;
import com.example.mynotepad.Dialogs.onCustomDialogClickListener;
import com.example.mynotepad.MenuFeatures.AllArchivedNotes.ArchivedNoesListFragment;
import com.example.mynotepad.MenuFeatures.AllNotes.AllNotesListFragment;
import com.example.mynotepad.MenuFeatures.AllNotes.DataBase.DateEntity;
import com.example.mynotepad.MenuFeatures.AllNotes.DataBase.Note;
import com.example.mynotepad.MenuFeatures.Calender.CalenderNotesListFragment;
import com.example.mynotepad.MenuFeatures.Utils;
import com.example.mynotepad.R;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

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

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class NoteFragment extends Fragment {
    private Note note;
    DateEntity dateEntity;
    private EditText titleTxt, bodyTxt;
    private FloatingActionButton back;
    private boolean isAnewNote;
    private String type, noteDateType;
    private ConstraintLayout  noteLayout ;

    public NoteFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View v = inflater.inflate(R.layout.fragment_note, container, false);
        init(v);
        setBg(Utils.sTheme);
        isAnewNote = true;
        return v;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setTitleAndBodyTxt();
    }


    private void setBg(String sTheme) {
        switch (sTheme) {
            case "dark":
                noteLayout.setBackgroundResource(R.drawable.main_bg_dark);
                break;
            case "bright":
                noteLayout.setBackgroundResource(R.drawable.main_bg_bright);
                break;
        }
    }
    private void init(View view) {
        back = view.findViewById(R.id.backBtn);
        titleTxt = view.findViewById(R.id.myTitle);
        bodyTxt = view.findViewById(R.id.textBody);
        noteLayout = view.findViewById(R.id.noteLayout);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (titleTxt.getText().length() != 0 && bodyTxt.getText().length() != 0)
                    showAlert();
                else getActivity().onBackPressed();
            }
        });
    }


    private void showAlert() {
        CustomDialog customDialog = new CustomDialog();
        customDialog.showAddTxtDialog(getActivity(), "Do you want to save the note ?");
        customDialog.setOnCustomDialogClickListener(new onCustomDialogClickListener() {
            @Override
            public void yesClicked() {
                if (type.equals("note"))
                    saveNote();
                else if (type.equals("date"))
                    saveCalenderNote();
                else if (type.equals("noteDate")) {
                    System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++");
                    if (noteDateType.equals("note")) {
                        noteDataBase.noteDao().updateNote(titleTxt.getText().toString(), bodyTxt.getText().toString(), true, ArchivedNoesListFragment.chosenDateOrNote.getId());
                    } else if (noteDateType.equals("date")) {
                        noteDataBase.dateNoreDAO().updateDate(titleTxt.getText().toString(), bodyTxt.getText().toString(), true, ArchivedNoesListFragment.chosenDateOrNote.getId());
                    }
                }
                if (getActivity() != null)
                    getActivity().onBackPressed();
            }

            @Override
            public void noClicked() {
                getActivity().onBackPressed();
            }
        });
    }


    private void saveNote() {
        if (isAnewNote) {
            note = new Note(titleTxt.getText().toString(), bodyTxt.getText().toString(), false);
            noteDataBase.noteDao().insertNote(note);
            Toast.makeText(getActivity(), "saved", Toast.LENGTH_SHORT).show();
        } else {
            noteDataBase.noteDao().updateNote(titleTxt.getText().toString(), bodyTxt.getText().toString(), AllNotesListFragment.myChosenNote.isArchived(), AllNotesListFragment.myChosenNote.getId());
            Toast.makeText(getActivity(), "updated", Toast.LENGTH_SHORT).show();
        }
    }


    private void saveCalenderNote() {
        if (isAnewNote) {
            dateEntity = new DateEntity(titleTxt.getText().toString(), bodyTxt.getText().toString(), false);
            noteDataBase.dateNoreDAO().insertDateNote(dateEntity);
            Toast.makeText(getActivity(), "saved", Toast.LENGTH_SHORT).show();
        } else {
            noteDataBase.dateNoreDAO().updateDate(titleTxt.getText().toString(), bodyTxt.getText().toString(), CalenderNotesListFragment.myChosenDateNote.isArchived(), CalenderNotesListFragment.myChosenDateNote.getId());
            Toast.makeText(getActivity(), "updated", Toast.LENGTH_SHORT).show();
        }
    }


    private void setTitleAndBodyTxt() {
        Bundle bundle = getArguments();

        if (bundle != null)
            if (bundle.getString("title").equals("") && bundle.getString("bodyTxt").equals("")) {
                isAnewNote = true;
                type = bundle.getString("type");
            } else {
                isAnewNote = false;
                titleTxt.setText(bundle.getString("title"));
                bodyTxt.setText(bundle.getString("bodyTxt"));
                type = bundle.getString("type");
                noteDateType = bundle.getString("noteDateType");
            }
    }
}