package com.jasamedika.medifirst2000.asynctask;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.service.IpsrsMaintenanceService;
import com.jasamedika.medifirst2000.service.PegawaiMutasiSanksiResignService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.IpsrsJadwalPemeliharaanVO;

@Component
public class IpsrsAlertMaintenance extends LocaleController<IpsrsJadwalPemeliharaanVO>{
/*
	private Boolean flag =false;
	private Integer pending = 0;
	private String time = new SimpleDateFormat("HH:mm").format(Calendar.getInstance().getTime());
	private SimpleDateFormat format = new SimpleDateFormat("HH:mm");
	
	public IpsrsAlertMaintenance() {
		if(!Thread.currentThread().isInterrupted()){
			try {
//				new Thread(new RunningTask()).start();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	class RunningTask implements Runnable{
		public void run() {
			new Timer().schedule(new TimerTask(){
				@Override
				public void run() {
					try{
						Date timeNow = format.parse(time);
						Date timeEnd = format.parse("18:00");
						Date timeReset = format.parse("21:00");

						if(pending==60*3){
							if(timeNow.before(timeEnd)){
								if(flag==false){
//									getAlertlMaintenance();
//									getDataPlanningByDate();
							        flag=true;
								}
							}
							pending=0;
						} else{
							pending++;
							if(timeNow.equals(timeReset)){
								flag=false;
							}
						}
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			}, 1000L, 1000L);
		}
	}
	
	@Autowired
	private IpsrsMaintenanceService ipsrsMaintenanceService;
	
	public void getAlertlMaintenance(){
		try{
			if (ipsrsMaintenanceService != null){
				Map<String, Object> jadwalMaintenance = ipsrsMaintenanceService.getAlertMaintenance();
				if(CommonUtil.isNotNullOrEmpty(jadwalMaintenance)){
					BroadcastMessageOther("jadwalMaintenance", jadwalMaintenance);
					System.out.println(DateUtil.now()+" task scheduler :");
					System.out.println(new Gson().toJson(jadwalMaintenance));
				} else{
					System.out.println(DateUtil.now()+" task scheduler : tidak ada jadwal maintenance");
				}
			}
		}catch (Exception ex){
			ex.printStackTrace();
		}
	}
	
	@Autowired
	private PegawaiMutasiSanksiResignService pegawaiMutasiSanksiResignService;
	
	@SuppressWarnings("unchecked")
	public void getDataPlanningByDate(){
		try{
			if (pegawaiMutasiSanksiResignService != null) {
				Map<String, Object> mapData = pegawaiMutasiSanksiResignService.getDataPlanningByDate(DateUtil.now());
				if(CommonUtil.isNotNullOrEmpty(mapData)){
					for(Map<String, Object> data : (List<Map<String, Object>>) mapData.get("listData")){
						Map<String, Object> dataResult = pegawaiMutasiSanksiResignService.updateExecPegawai(data);
						System.out.println(DateUtil.now()+" task scheduler : update pegawai "+new Gson().toJson(dataResult));
					}
				} else{
					System.out.println(DateUtil.now()+" task scheduler : tidak ada update data pegawai musare");
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
*/	
}
