package com.jasamedika.medifirst2000.asynctask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.jasamedika.medifirst2000.asynctask.timer.KalenderTimer;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.service.KalenderService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.KalenderVO;

@Component
public class CalendarSystemGenerating extends LocaleController<KalenderVO> {
	private final static Logger LOGGER = LoggerFactory.getLogger(CalendarSystemGenerating.class);

	@Autowired
	private KalenderService kalenderService;

	public CalendarSystemGenerating() {
		int the1st = 1;
		int at0hrs = 0;
		KalenderTimer.schedule(new Runnable() {
			@Override
			public void run() {
				try {
					LOGGER.info("Task Sistem Kalender : Running Task Sistem Kalender");
					genSistemKalender();
				} catch (Exception ex) {
					LOGGER.error("Task Sistem Kalender : Task Sistem Kalender " + ex.getMessage());
				} finally {
					LOGGER.info("Task Sistem Kalender : Finishing Task Sistem Kalender");
				}
			}
		}, the1st, at0hrs);
	}

	@Async
	public void genSistemKalender() {
		try {
			if (CommonUtil.isNotNullOrEmpty(kalenderService)) {
				LOGGER.info("Task Sistem Kalender : Running Check/Generate Sistem Kalender");
				kalenderService.generateAndSaveKalender();
			} else {
				LOGGER.warn("Task Sistem Kalender : Undefined service");
			}
		} catch (Exception ex) {
			LOGGER.error("Task Sistem Kalender : Check/Generate Sistem Kalender " + ex.getMessage());
		} finally {
			LOGGER.info("Task Sistem Kalender : Finishing Check/Generate Sistem Kalender");
		}
	}

}
