package controller;

import org.sig.fw.layout.common.LayoutConfig;
import org.sig.fw.layout.pane.SOptionPane;
import org.sig.fw.sig.db.dao.Software_DBAO;
import org.sig.fw.sig.model.Software;
import org.sig.fw.utility.date.DateTools;
import org.sig.fw.utility.date.SDate;

import controller.history.Application_Controller;
import main.Main;

public class Check_Maintenance_Controller
{
	private Application_Controller ctrl_application;
	
	private Thread check_thread = null;
	private Thread timer_thread = null;
	
	private boolean show15 = false;
	private boolean show10 = false;
	private boolean show5 = false;
	
	public Check_Maintenance_Controller(Application_Controller ctrl_application) 
	{
		super();
		this.ctrl_application = ctrl_application;
	}
	
	public void startCheckThread()
	{
		if(this.check_thread == null)
		{
			this.check_thread = new Thread(new Runnable()
			{
				@Override
				public void run() 
				{
					Software sw = new Software_DBAO("soft").getWithName(Main.APP_NAME);
					while(true)
					{
						ctrl_application.getView().setMaintenanceText("");
						SDate date = sw.getMaintenanceDate();
						if(sw.isMaintenance() && date != null)
						{
							startTimerThread(date);
							break;
						}
	
						
						try {
							Thread.sleep(300000);
						} catch (InterruptedException e) {
							break;
						}
					}
				}
			});
			this.check_thread.start();
		}
		else
		{
			this.check_thread.interrupt();
			this.check_thread = null;
			startCheckThread();
		}
	}

	public void startTimerThread(SDate date)
	{
		this.timer_thread = new Thread(new Runnable()
		{
			@Override
			public void run() 
			{
				while(true)
				{
					long diff = date.getDate().getTime() - new SDate().getDate().getTime();
					
					if(diff < 0)
					{
						LayoutConfig.frame = null;
						SOptionPane.showInformationMessage("Corrida", "Fermeture de l'application pour cause de Maintenance !");
						ctrl_application.closeApplication();
						break;
					}
					else
					{
						String text = "/!\\ Maintenance planifiée dans " + DateTools.longToHours(diff) + " /!\\";
						ctrl_application.getView().setMaintenanceText(text);
						
						
						if(diff < 5*60*1000 && !show5)
						{
							showInformationPane("Maintenance dans moins de 5 Minutes.");
							show5 = true;
						}
						
						if(diff < 10*60*1000 && !show10)
						{
							showInformationPane("Maintenance dans moins de 10 Minutes.");
							show10 = true;
						}
						
						if(diff < 15*60*1000 && !show15)
						{
							showInformationPane("Maintenance dans moins de 15 Minutes.");
							show15 = true;
						}
					}
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						break;
					}
				}
			}
		});
		this.timer_thread.start();
	}
	
	public void showInformationPane(String information)
	{
		new Thread(new Runnable() 
		{
			@Override
			public void run() {
				SOptionPane.showInformationMessage("MATP - Gestion", information);
			}
		}).start();
	}
	
	public void stopThread()
	{
		if(this.check_thread != null)
			this.check_thread.interrupt();
		if(this.timer_thread != null)
			this.timer_thread.interrupt();
	}
}
