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.JadwalKerjaTimer;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.service.PegawaiJadwalKerjaService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.PegawaiJadwalKerjaVO;

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

	@Autowired
	private PegawaiJadwalKerjaService pegawaiJadwalKerjaService;

	public PegawaiJadwalKerjaGenerating() {
		int the1st = 1;
		int at0hrs = 0;
		JadwalKerjaTimer.schedule(new Runnable() {
			@Override
			public void run() {
				try {
					LOGGER.info("Task Jadwal Kerja : Running Task Jadwal Kerja");
					genJadwalKerja();
				} catch (Exception ex) {
					LOGGER.error("Task Jadwal Kerja : Task Jadwal Kerja " + ex.getMessage());
				} finally {
					LOGGER.info("Task Jadwal Kerja : Finishing Task Jadwal Kerja");
				}
			}
		}, the1st, at0hrs);
	}

	@Async
	public void genJadwalKerja() {
		try {
			if (CommonUtil.isNotNullOrEmpty(pegawaiJadwalKerjaService)) {
				LOGGER.info("Task Jadwal Kerja : Running Generate Jadwal Kerja");
				pegawaiJadwalKerjaService.scheduledSaveJadwalKerjaNonShift();
			} else {
				LOGGER.warn("Task Jadwal Kerja : Undefined service");
			}
		} catch (Exception ex) {
			LOGGER.error("Task Jadwal Kerja : Generate Jadwal Kerja " + ex.getMessage());
		} finally {
			LOGGER.info("Task Jadwal Kerja : Finishing Generate Jadwal Kerja");
		}
	}

}
