package com.example.calendaryapp;

import android.app.Application;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import java.util.List;

public class NoteRepository {
    private NoteDao noteDao;
    private LiveData<List<Note>> allNotes;
    private LiveData<List<Note>> allNotesMar;
    private LiveData<List<Note>> allNotesMie;
    private LiveData<List<Note>> allNotesJue;
    private LiveData<List<Note>> allNotesVie;


    public  NoteRepository(Application application){

        NoteDatabase database = NoteDatabase.getInstance(application);

        noteDao = database.noteDao();

        allNotes = noteDao.getAllNotes();
        allNotesMar = noteDao.getAllNotesMar();
        allNotesMie = noteDao.getAllNotesMie();
        allNotesJue = noteDao.getAllNotesJue();
        allNotesVie = noteDao.getAllNotesVie();


    }
    public void insert(Note note) {

        new InsertNoteAsyncTask(noteDao).execute(note);



    }

    public void update(Note note) {

         new UpdateNoteAsyncTask(noteDao).execute(note);

    }

    public void delete(Note note) {

        new DeleteNoteAsyncTask(noteDao).execute(note);


    }

    public void deleteAllNotes() {

           new DeleteAllNotesAsyncTask(noteDao).execute();


    }

    public LiveData<List<Note>> getAllNotes() {
        return allNotes;

    }
    public LiveData<List<Note>> getAllNotesMar() {
        return allNotesMar;

    }
    public LiveData<List<Note>> getAllNotesMie() {
        return allNotesMie;

    }
    public LiveData<List<Note>> getAllNotesJue() {
        return allNotesJue;

    }
    public LiveData<List<Note>> getAllNotesVie() {
        return allNotesVie;

    }


    private static class InsertNoteAsyncTask extends AsyncTask<Note, Void, Void>{
        private NoteDao noteDao;


        private InsertNoteAsyncTask(NoteDao noteDao) {
            this.noteDao = noteDao;
        }

        @Override
        protected Void doInBackground(Note... notes) {

            noteDao.insert(notes[0]);
            return null;
        }
    }
    private static class UpdateNoteAsyncTask extends AsyncTask<Note, Void, Void> {
        private NoteDao noteDao;

        private UpdateNoteAsyncTask(NoteDao noteDao) {

            this.noteDao = noteDao;
        }

        @Override
        protected Void doInBackground(Note... notes) {
            noteDao.update(notes[0]);
            return null;
        }
    }

    private static class DeleteNoteAsyncTask extends AsyncTask<Note, Void, Void> {
        private NoteDao noteDao;


        private DeleteNoteAsyncTask(NoteDao noteDao) {

            this.noteDao = noteDao;
        }

        @Override
        protected Void doInBackground(Note... notes) {
            noteDao.delete(notes[0]);
            return null;
        }
    }

    private static class DeleteAllNotesAsyncTask extends AsyncTask<Void, Void, Void> {
        private NoteDao noteDao;

        private DeleteAllNotesAsyncTask(NoteDao noteDao) {
            this.noteDao = noteDao;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            noteDao.deleteAllNotes();
            return null;
        }
    }


}
