package com.example.mynotepad.MenuFeatures.Calender.MyCalenderRecyclerView.TimePicker;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

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

import com.alirezaafkar.sundatepicker.DatePicker;
import com.alirezaafkar.sundatepicker.interfaces.DateSetListener;
import com.example.mynotepad.MenuFeatures.AllNotes.DataBase.DateEntity;
import com.example.mynotepad.MenuFeatures.Calender.Date;
import com.example.mynotepad.R;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import java.util.Calendar;

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

public class TimePickerFragment extends Fragment {

    private DateEntity dateEntity;
    private EditText chosenDate;
    private TextView description;
    private Date mDate;
    private FloatingActionButton back;

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

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


    private void init(View view) {
        chosenDate = view.findViewById(R.id.myTitle);
        description = view.findViewById(R.id.textBody);
        back = view.findViewById(R.id.backBtn);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showAlert(view);
            }
        });
    }


    private void showTimePicker() {
        DatePicker.Builder builder = new DatePicker
                .Builder()
                .theme(R.style.DialogTheme)
                .future(true);
        mDate = new Date();
        builder.date(mDate.getDay(), mDate.getMonth(), mDate.getYear());
        builder.build(new DateSetListener() {
            @Override
            public void onDateSet(int id, @Nullable Calendar calendar, int day, int month, int year) {
                mDate.setDate(day, month, year);
                chosenDate.setText(mDate.getDate());
            }
        }).show(getFragmentManager(), "");
    }

    private void showAlert(View view) {
        if (chosenDate.getText().length() != 0) {
            final AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
            alert.setTitle("new note :");
            alert.setMessage("Do you want to save the note ?");
            alert.setCancelable(true);
            alert.setPositiveButton("yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    saveDateNote();
                    Toast.makeText(getContext(), "saved", Toast.LENGTH_SHORT).show();
                }
            });


            alert.setNegativeButton("no", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.cancel();
                    getActivity().finish();
                }
            });
            alert.show();
        } else if (description.getText().length() != 0) {
            Snackbar.make(view, "note did not save ! you should pick a date ! ", Snackbar.LENGTH_LONG).setAction("Action", null).show();
            getActivity().finish();
        } else {
            getActivity().finish();
        }
    }

    private void saveDateNote() {
        //todo save in data base
        dateEntity = new DateEntity(chosenDate.getText().toString(), description.getText().toString(), false);
        noteDataBase.dateNoreDAO().insertDateNote(dateEntity);
    }
}