package com.jasamedika.medifirst2000.asynctask;

import java.util.Calendar;

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.KontrakVerificationTimer;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.dao.SettingDataFixedDao;
import com.jasamedika.medifirst2000.service.LogbookKinerjaService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.LogbookKinerjaVO;

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

	@Autowired
	private LogbookKinerjaService logbookKinerjaService;

	@Autowired
	private SettingDataFixedDao settingDataFixedDao;

	public AutoKontrakVerification() {
//		int day = 21;
//		int hour = 0;
//		int minute = 00;
//		KontrakVerificationTimer.schedule(new Runnable() {
//			@Override
//			public void run() {
//				try {
//					LOGGER.info("Task Kontrak Verification : Running Task Kontrak Verification");
//					autoKontrakVerification();
//				} catch (Exception ex) {
//					LOGGER.error("Task Kontrak Verification : Task Kontrak Verification " + ex.getMessage());
//				} finally {
//					LOGGER.info("Task Kontrak Verification : Finishing Task Kontrak Verification");
//				}
//			}
//		}, day, hour, minute);
	}

//	@Async
	public void autoKontrakVerification() {
		try {
			if (CommonUtil.isNotNullOrEmpty(logbookKinerjaService)) {
				LOGGER.info("Task Kontrak Verification : Running Automatic Kontrak Verification");

				Calendar cal = Calendar.getInstance();
				cal.add(Calendar.MONTH, +1);
				if (Boolean.parseBoolean(
						settingDataFixedDao.getSettingDataFixed("otomatisVerifikasiKontrakKinerjaDokter"))) {
					logbookKinerjaService.autoVerifKontrakJamKerjaDokter(cal.getTime());
				}
			} else {
				LOGGER.warn("Task Kontrak Verification : Undefined service");
			}
		} catch (Exception ex) {
			LOGGER.error("Task Kontrak Verification : Automatic Kontrak Verification " + ex.getMessage());
		} finally {
			LOGGER.info("Task Kontrak Verification : Finishing Automatic Kontrak Verification");
		}
	}

}