package com.github.neunkasulle.chronocommand.control;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * Created by Dav on 26.01.2016.
 * Name can be changed everytime
 */
public class SchedulerHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(SchedulerHandler.class);

    private static final SchedulerHandler ourInstance = new SchedulerHandler();

    private static Scheduler sched;

    private SchedulerHandler() {

    }

    public static SchedulerHandler getInstance() {
        return ourInstance;
    }



    private static void intantiateSched() throws SchedulerException {
        SchedulerFactory schedFact = new StdSchedulerFactory();
        sched = schedFact.getScheduler();
    }

    /**
     * schedules all jobs with the triggers
     */
    public static void scheduleAll() {
        try {
            intantiateSched();
        } catch (SchedulerException e) {
            LOGGER.error("Failure in instantiating scheduler", e);
        }

        JobDetail weeklyReminderJob = newJob(WeeklyMailJob.class)
                .withIdentity("weeklyReminderJob", "weeklyReminder") //name, group
                .build();

        JobDetail monthlyReminderJob = newJob(MonthlyReminder.class)
                .withIdentity("monthlyReminderJob", "monthlyReminder")
                .build();

        Trigger lastDayOfMonth = newTrigger() //fires at 12pm every last day every month
                .withIdentity("lastDayTrigger", "monthlyreminder")
                .withSchedule(cronSchedule("0 0 12 L * ?"))
                .startNow()
                .build();

        Trigger weeklyTrigger = newTrigger() //every week at 12pm in every month
                .withIdentity("weeklyTrigger", "weeklyReminder")
                .withSchedule(cronSchedule("0 0 12 ? * SUN"))
                .startNow()
                .build();

        //trigger for tesst purpose
        JobDetail dailyReminderJob = newJob(WeeklyMailJob.class)
                .withIdentity("dailyTrigger", "testGroup") //name, group
                .build();
        Trigger dailyTrigger = newTrigger()
                .withIdentity("dailyTrigger", "testGroup")
                .withSchedule(cronSchedule("0 0 12 * * ?"))
                .startNow()
                .build();

        try {
            sched.start();
            // Tell quartz to schedule a job with a trigger
            sched.scheduleJob(monthlyReminderJob, lastDayOfMonth);
            sched.scheduleJob(weeklyReminderJob, weeklyTrigger);

            sched.scheduleJob(dailyReminderJob, dailyTrigger);
        } catch (SchedulerException e) {
            LOGGER.error("Failure in starting scheduler", e);
        }
    }

    /**
     * finish running jobs and halts the scheduler's firing of triggers
     */
    public void shutdownSched() {
        try {
            sched.shutdown(true);
        } catch (SchedulerException e) {
            LOGGER.error("Failure in shutdown", e);
        }
    }
}

@@ -2098,426 +2098,8 @@
 );%0A%0A
-        //trigger for tesst purpose%0A        JobDetail dailyReminderJob = newJob(WeeklyMailJob.class)%0A                .withIdentity(%22dailyTrigger%22, %22testGroup%22) //name, group%0A                .build();%0A        Trigger dailyTrigger = newTrigger()%0A                .withIdentity(%22dailyTrigger%22, %22testGroup%22)%0A                .withSchedule(cronSchedule(%220 0 12 * * ?%22))%0A                .startNow()%0A                .build();%0A%0A
     
@@ -2330,72 +2330,8 @@
 er);
-%0A%0A            sched.scheduleJob(dailyReminderJob, dailyTrigger);
 %0A   

remove daily reminder