package org.motechproject.appointments.reminder.impl;

import org.joda.time.Period;
import org.motechproject.appointments.domain.Appointment;
import org.motechproject.appointments.reminder.AppointmentReminderService;
import org.motechproject.event.MotechEvent;
import org.motechproject.scheduler.service.MotechSchedulerService;
import org.motechproject.scheduler.contract.RepeatingSchedulableJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Implementation for the appointment reminder connector service
 */
@Component
public class AppointmentReminderServiceImpl implements AppointmentReminderService {

    private static final int MILLISECONDS = 1000;
    private static final int SECONDS = 60;
    private static final int MINUTES = 60;
    private static final int HOURS = 24;
    private static final int DAYS = 7;

    private static final String SUBJECT = "Appointment.Reminder.%s.%s";

    @Autowired
    private MotechSchedulerService schedulerService;

    /**
     * Add reminders for a specific appointment
     * @param appointment Appointment object with the necessary fields populated
     */
    @Override
    public void addReminders(Appointment appointment) {
        if (appointment.getReminderInterval() != null) {
            RepeatingSchedulableJob reminder = buildJob(appointment);
            this.schedulerService.safeScheduleRepeatingJob(reminder);
        }
    }

    /**
     * Remove reminders for a specific appointment
     * @param appointment Appointment object with the necessary fields populated
     */
    @Override
    public void removeReminders(Appointment appointment) {
        this.schedulerService.safeUnscheduleAllJobs(String.format(SUBJECT, appointment.getExternalId(), appointment.getApptId()));
    }

    /**
     * Helper to create a repeating schedulable job from an appointment
     * @param appointment The appointment object to create the schedulable job for
     * @return a repeating job for the scheduler
     */
    private RepeatingSchedulableJob buildJob(Appointment appointment) {
        String eventTitle = String.format(SUBJECT, appointment.getExternalId(), appointment.getApptId());
        MotechEvent event = new MotechEvent(eventTitle);

        // temp workaround for lack of period support in scheduler, ignoring anything bigger than weeks for now. Tracking MOTECH-1121
        Period interval = appointment.getReminderInterval();
        long reminderInterval = interval.getWeeks() * DAYS * HOURS * MINUTES * SECONDS * MILLISECONDS + interval.getDays() * HOURS * MINUTES * SECONDS * MILLISECONDS +
                interval.getHours() * MINUTES * SECONDS * MILLISECONDS + interval.getMinutes() * SECONDS * MILLISECONDS + interval.getSeconds() * MILLISECONDS + interval.getMillis();

        return new RepeatingSchedulableJob(event, appointment.getReminderStartTime().toDate(), appointment.getAppointmentDate().toDate(), reminderInterval, true);
    }
}

@@ -52,37 +52,8 @@
 l;%0A%0A
-import org.joda.time.Period;%0A
 impo
@@ -264,38 +264,46 @@
 ler.
-service.MotechSchedulerService
+contract.RepeatingPeriodSchedulableJob
 ;%0Aim
@@ -339,40 +339,38 @@
 ler.
-contract.RepeatingSchedulableJob
+service.MotechSchedulerService
 ;%0Aim
@@ -653,225 +653,8 @@
  %7B%0A%0A
-    private static final int MILLISECONDS = 1000;%0A    private static final int SECONDS = 60;%0A    private static final int MINUTES = 60;%0A    private static final int HOURS = 24;%0A    private static final int DAYS = 7;%0A%0A
     
@@ -1076,32 +1076,38 @@
        Repeating
+Period
 SchedulableJob r
@@ -1194,16 +1194,22 @@
 epeating
+Period
 Job(remi
@@ -1781,16 +1781,23 @@
 peating 
+period 
 job for 
@@ -1831,32 +1831,38 @@
 rivate Repeating
+Period
 SchedulableJob b
@@ -2072,555 +2072,8 @@
     
-// temp workaround for lack of period support in scheduler, ignoring anything bigger than weeks for now. Tracking MOTECH-1121%0A        Period interval = appointment.getReminderInterval();%0A        long reminderInterval = interval.getWeeks() * DAYS * HOURS * MINUTES * SECONDS * MILLISECONDS + interval.getDays() * HOURS * MINUTES * SECONDS * MILLISECONDS +%0A                interval.getHours() * MINUTES * SECONDS * MILLISECONDS + interval.getMinutes() * SECONDS * MILLISECONDS + interval.getSeconds() * MILLISECONDS + interval.getMillis();%0A%0A        
 retu
@@ -2088,16 +2088,22 @@
 epeating
+Period
 Schedula
@@ -2204,17 +2204,32 @@
 Date(), 
-r
+appointment.getR
 eminderI
@@ -2235,16 +2235,18 @@
 Interval
+()
 , true);

Add support for period in Appointment scheduler