package com.jasamedika.medifirst2000.asynctask.timer;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class KontrakVerificationTimer {
	private final static Logger LOGGER = LoggerFactory.getLogger(KontrakVerificationTimer.class);

	// What to do
	private final Runnable whatToDo;

	// when
	private final int dayOfMonth;
	private final int hourOfDay;
	private final int minuteOfHour;

	// The current timer
	private Timer current = new Timer();

	public void cancelCurrent() {
		LOGGER.info("KontrakVerificationTimer : Cancel current execution");
		current.cancel();

		LOGGER.info("KontrakVerificationTimer : Removes the timertask so it can be garbage collected");
		current.purge();
	}

	public static KontrakVerificationTimer schedule(Runnable runnable, int dayOfMonth, int hourOfDay, int minuteOfHour) {
		LOGGER.info("KontrakVerificationTimer : Create a new instance");
		return new KontrakVerificationTimer(runnable, dayOfMonth, hourOfDay, minuteOfHour);
	}

	private KontrakVerificationTimer(Runnable runnable, int day, int hour, int minute) {
		this.whatToDo = runnable;
		this.dayOfMonth = day;
		this.hourOfDay = hour;
		this.minuteOfHour = minute;

		schedule();
	}

	private void schedule() {
		cancelCurrent();

		LOGGER.info(
				"KontrakVerificationTimer : Assigning a new instance of Timer, allow the previous Timer to be garbage collected");
		current = new Timer();
		current.schedule(new TimerTask() {
			public void run() {
				try {
					LOGGER.info("KontrakVerificationTimer : Running schedule");
					whatToDo.run();
				} finally {
					LOGGER.info("KontrakVerificationTimer : Schedule for the next month");
					schedule();
				}
			}
		}, nextDate());
	}

	private Date nextDate() {
		Calendar curDate = Calendar.getInstance();
		Calendar runDate = Calendar.getInstance();

		// set_for_compare
		runDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
		runDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
		runDate.set(Calendar.MINUTE, minuteOfHour);
		runDate.set(Calendar.SECOND, 0);
		runDate.set(Calendar.MILLISECOND, 0);

		if (curDate.getTime().after(runDate.getTime())) {
			runDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			runDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
			runDate.set(Calendar.MINUTE, minuteOfHour);
			runDate.set(Calendar.SECOND, 0);
			runDate.set(Calendar.MILLISECOND, 0);
			runDate.add(Calendar.MONTH, 1);
		}

		LOGGER.info("KontrakVerificationTimer : Set to next month " + runDate.getTime());

		return runDate.getTime();
	}
}
