package ru.ulstu.paper.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class PaperScheduler {
    private final static boolean IS_DEADLINE_NOTIFICATION_BEFORE_WEEK = true;

    private final Logger log = LoggerFactory.getLogger(PaperScheduler.class);

    private final PaperNotificationService paperNotificationService;
    private final PaperService paperService;

    public PaperScheduler(PaperNotificationService paperNotificationService,
                          PaperService paperService) {
        this.paperNotificationService = paperNotificationService;
        this.paperService = paperService;
    }


    @Scheduled(cron = "0 0 8 * * MON", zone = "Europe/Samara")
    public void checkDeadlineBeforeWeek() {
        log.debug("PaperScheduler.checkDeadlineBeforeWeek started");
        paperNotificationService.sendDeadlineNotifications(paperService.findAll(), IS_DEADLINE_NOTIFICATION_BEFORE_WEEK);
        log.debug("PaperScheduler.checkDeadlineBeforeWeek finished");
    }

    @Scheduled(cron = "0 0 8 * * ?", zone = "Europe/Samara")
    public void checkDeadlineAfterWeek() {
        log.debug("PaperScheduler.checkDeadlineAfterWeek started");
        paperNotificationService.sendDeadlineNotifications(paperService.findAll(), !IS_DEADLINE_NOTIFICATION_BEFORE_WEEK);
        log.debug("PaperScheduler.checkDeadlineAfterWeek finished");
    }

    @Scheduled(cron = "0 0 6 * * ?", zone = "Europe/Samara")
    public void closeFailedPapers() {
        log.debug("PaperScheduler.closeFailedPapers started");
        paperService.closeFailedPapers();
        log.debug("PaperScheduler.closeFailedPapers finished");
    }
}
