package com.cappielloantonio.play.repository;

import android.app.Application;

import androidx.lifecycle.LiveData;

import com.cappielloantonio.play.database.AppDatabase;
import com.cappielloantonio.play.database.dao.ChronologyDao;
import com.cappielloantonio.play.model.Chronology;

import java.util.Calendar;
import java.util.List;

public class ChronologyRepository {
    private static final String TAG = "ChronologyRepository";

    private final ChronologyDao chronologyDao;

    public ChronologyRepository(Application application) {
        AppDatabase database = AppDatabase.getInstance(application);
        chronologyDao = database.chronologyDao();
    }

    public LiveData<List<Chronology>> getThisWeek(String server) {
        Calendar calendar = Calendar.getInstance();

        Calendar first = (Calendar) calendar.clone();
        first.add(Calendar.DAY_OF_WEEK, first.getFirstDayOfWeek() - first.get(Calendar.DAY_OF_WEEK));

        Calendar last = (Calendar) first.clone();
        last.add(Calendar.DAY_OF_YEAR, 6);

        return chronologyDao.getAllFrom(first.getTime().getTime(), last.getTime().getTime(), server);
    }

    public LiveData<List<Chronology>> getLastWeek(String server) {
        Calendar calendar = Calendar.getInstance();

        Calendar first = (Calendar) calendar.clone();
        first.add(Calendar.DAY_OF_WEEK, first.getFirstDayOfWeek() - first.get(Calendar.DAY_OF_WEEK) - 6);

        Calendar last = (Calendar) first.clone();
        last.add(Calendar.DAY_OF_YEAR, 6);

        return chronologyDao.getAllFrom(first.getTime().getTime(), last.getTime().getTime(), server);
    }

    public void insert(Chronology item) {
        InsertThreadSafe insert = new InsertThreadSafe(chronologyDao, item);
        Thread thread = new Thread(insert);
        thread.start();
    }

    private static class InsertThreadSafe implements Runnable {
        private final ChronologyDao chronologyDao;
        private final Chronology item;

        public InsertThreadSafe(ChronologyDao chronologyDao, Chronology item) {
            this.chronologyDao = chronologyDao;
            this.item = item;
        }

        @Override
        public void run() {
            chronologyDao.insert(item);
        }
    }
}
