package Services.Controller;
import java.awt.Color;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.springframework.boot.SpringApplication;
import org.springframework.web.client.*;


import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
@RequestMapping("/controller/")
public class ControllerApplication {
	
	private final String captMouvURI = "http://localhost:8081/CaptMouv/";
	private final String captTempURI = "http://localhost:8082/CaptTemp/";
	private final String captCO2URI = "http://localhost:8083/CaptCO2/";
	private final String captHumURI = "http://localhost:8084/CaptHum/";
	private final String automPorteURI = "http://localhost:8085/AutomPorteApplication/";
	private final String automFenetreURI = "http://localhost:8086/AutomFenetreApplication/";
	private final String automLumiereURI = "http://localhost:8087/AutomLumiereApplication/";
	private final String automChauffageURI = "http://localhost:8088/AutomChauffageApplication/";
	private final String simulationURI = "http://localhost:8089/Simulation/";
	
	
	boolean ControlLumiereAutomatique = false;
	boolean PorteFenetreAutomatique_TemperatureCO2 = false;
	boolean PorteFenetreAutomatique_CO2 = false;
	boolean MouvementAlarmActivated = false;
	boolean TemperatureAlarmActivated = false;
	boolean CO2AlarmActivated = false;
	
	
	//Controle de la lumiere automatique
	//Controle en fonction du mouvement
	@GetMapping("isControlLumiereAutomatiqueActivated/")
	private final boolean isControlLumiereAutomatique() {
		return ControlLumiereAutomatique;
	}
	@GetMapping("setControlLumiereAutomatique/")
	private final void setControlLumiereAutomatique(boolean controlLumiereAutomatique) {
		ControlLumiereAutomatique = controlLumiereAutomatique;
	}
	
	//Control de l'ouverture de la porte et fenetre
	//Control en fonction de la temperature et CO2
	@GetMapping("isPorteFenetreAutomatique_TemperatureCO2Activated/")
	private final boolean isPorteFenetreAutomatique_TemperatureCO2() {
		return PorteFenetreAutomatique_TemperatureCO2;
	}


	@GetMapping("setPorteFenetreAutomatique_TemperatureCO2/")
	private final void setPorteFenetreAutomatique_TemperatureCO2(boolean porteFenetreAutomatique_TemperatureCO2) {
		PorteFenetreAutomatique_TemperatureCO2 = porteFenetreAutomatique_TemperatureCO2;
	}
	//Controle en fonction du CO2
	@GetMapping("isPorteFenetreAutomatique_CO2Activated/")
	private final boolean isPorteFenetreAutomatique_CO2() {
		return PorteFenetreAutomatique_CO2;
	}


	@GetMapping("setPorteFenetreAutomatique_CO2/")
	private final void setPorteFenetreAutomatique_CO2(boolean porteFenetreAutomatique_CO2) {
		PorteFenetreAutomatique_CO2 = porteFenetreAutomatique_CO2;
	}




	public void ControlLumiereAutomatique() {
		String url_getDetection = captMouvURI + "getDetection/";
		String url_allumerLumiere = automLumiereURI + "setON/?oN=true";
		String url_eteindreLumiere = automLumiereURI + "setON/?oN=false";
		RestTemplate restTemplate = new RestTemplate();
		
		
		//return restTemplate.getForObject(test, String.class);
		
		
		if (restTemplate.getForObject(url_getDetection,boolean.class)) {
			restTemplate.getForObject(url_allumerLumiere,boolean.class);			
		}
		else {
			restTemplate.getForObject(url_eteindreLumiere,boolean.class);
		}
	}
	
	
	public void PorteFenetreAutomatique_TemperatureCO2() {
		String url_getTemp = captTempURI + "getTemp/";
		String url_getCO2 = captCO2URI + "getCO2/";
		String url_setOuvertureFenetre = automFenetreURI + "setOpen/?open=true";
		String url_setFermetureFenetre = automFenetreURI + "setOpen/?open=false";
		String url_setOuverturePorte = automPorteURI + "setOpen/?open=true";
		String url_setFermeturePorte = automPorteURI + "setOpen/?open=false";
		RestTemplate restTemplate = new RestTemplate();
		
		if (((restTemplate.getForObject(url_getTemp,double.class)) > 30) | ((restTemplate.getForObject(url_getCO2,int.class)) > 500)) {
			restTemplate.getForObject(url_setOuvertureFenetre,double.class);
			restTemplate.getForObject(url_setOuverturePorte,double.class);
		}
		else {
			restTemplate.getForObject(url_setFermetureFenetre,double.class);
			restTemplate.getForObject(url_setFermeturePorte,double.class);
		}
		
	}
	
	
	public void PorteFenetreAutomatique_CO2() {
		
		String url_getCO2 = captCO2URI + "getCO2/";
		String url_setOuvertureFenetre = automFenetreURI + "setOpen/?open=true";
		String url_setFermetureFenetre = automFenetreURI + "setOpen/?open=false";
		String url_setOuverturePorte = automPorteURI + "setOpen/?open=true";
		String url_setFermeturePorte = automPorteURI + "setOpen/?open=false";
		RestTemplate restTemplate = new RestTemplate();
		
		if (((restTemplate.getForObject(url_getCO2,int.class)) > 500)) {
			restTemplate.getForObject(url_setOuvertureFenetre,double.class);
			restTemplate.getForObject(url_setOuverturePorte,double.class);
		}
		else {
			restTemplate.getForObject(url_setFermetureFenetre,double.class);
			restTemplate.getForObject(url_setFermeturePorte,double.class);
		}
		
	}
	@GetMapping("getSalleOccupee/")
	public String SalleOccupe() {
		String url_getDetection = captMouvURI + "getDetection/";
		RestTemplate restTemplate = new RestTemplate();
		boolean salle_occupe = restTemplate.getForObject(url_getDetection,boolean.class);
		
		
		return ("La salle est:" + String.valueOf(salle_occupe)) ;
		
	}
	
	@GetMapping("setTemperatureHeatingSystem/")
	public void setTemperatureHeatingSystem(int temp) {
		String url_setTemp = automChauffageURI + "setTemperatureSet/?temperatureSet=" + String.valueOf(temp);
		
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getForObject(url_setTemp,void.class);
	}
	
	@GetMapping("getHeatingSystemONOFF/")
	public boolean getHeatingSystemONOFF() {
		String url_getONOFF = automChauffageURI + "isActive/";
		
		RestTemplate restTemplate = new RestTemplate();
		return (restTemplate.getForObject(url_getONOFF,boolean.class));
	}
	
	public boolean getMovementDetection() {
		String url_getDetection = captMouvURI + "getDetection/";
		boolean resultat = false;
		RestTemplate restTemplate = new RestTemplate();
		
		return (restTemplate.getForObject(url_getDetection, boolean.class));
		
	}
	
	@GetMapping("getTemperatureAlarm/")
	public boolean getTemperatureAlarm() {
		return (TemperatureAlarmActivated);
	}
	@GetMapping("setTemperatureAlarm/")
	public void setTemperatureAlarm(boolean active) {
		TemperatureAlarmActivated = active;
	}
	
	@GetMapping("setMouvementAlarm/")
	public void setMouvementAlarm(boolean active) {
		MouvementAlarmActivated = active;
	}
	@GetMapping("getMouvementAlarm/")
	public boolean getMouvementAlarm() {
		return (MouvementAlarmActivated);
	}
	
	@GetMapping("setCO2Alarm/")
	public void setCO2Alarm(boolean active) {
		CO2AlarmActivated = active;
	}
	@GetMapping("getCO2Alarm/")
	public boolean getCO2Alarm() {
		return (CO2AlarmActivated);
	}
	
	
	
	
	@SuppressWarnings("deprecation")
	@GetMapping("run")
	public int run() throws Exception {
		
		if (ControlLumiereAutomatique) {
			ControlLumiereAutomatique();
		}
		
		if (PorteFenetreAutomatique_TemperatureCO2) {
			PorteFenetreAutomatique_TemperatureCO2();
		}
		
		if (PorteFenetreAutomatique_CO2) {
			PorteFenetreAutomatique_CO2();
		}
		//Gestion du nombre de declenchements
		if (getMovementDetection()) {
			RestTemplate restTemplate = new RestTemplate();
			int nbrDetection;
			nbrDetection = restTemplate.getForObject(captMouvURI +"getNbrDetection/",int.class);
			restTemplate.getForObject(captMouvURI +"setNbrDetection/?detection="+String.valueOf(nbrDetection+1),void.class);
		}
		
		//Notification de mouvement Alarm
		if (MouvementAlarmActivated & (getMovementDetection())) {
			//Communication avec OneM2M
			
			RestTemplate restTemplate = new RestTemplate();
			HttpHeaders headers = new HttpHeaders();
			
			headers.set("X-M2M-ORIGIN", "admin:admin");
			headers.set("Content-Type", "application/xml;ty=4");
			//headers.set("Accept", "application/xml");
			String parameters = "	\n"
					+ "<m2m:cin xmlns:m2m=\"http://www.onem2m.org/xml/protocols\">\n"
					+ "    <cnf>message</cnf>\n"
					+ "    <con>\n"
					+ "      &lt;obj&gt;\n"
					+ "        &lt;str name=&quot;data&quot; val=&quot;Mouvement_GEI213&quot;/&gt;\n"
					+ "        \n"
					+ "      &lt;/obj&gt;\n"
					+ "    </con>\n"
					+ "</m2m:cin>";
			
			HttpEntity <String> entity = new HttpEntity <String> (parameters,headers);
			String url = "http://127.0.0.1:8080/~/in-cse/in-name/Alarm_Notification/DATA/";
			
			restTemplate.exchange(url,HttpMethod.POST,entity,String.class);
			
		}
		
		//Notification temperature elevée
		if (TemperatureAlarmActivated) {
			String url_getTemp = captTempURI + "getTemp/";
			int temp;
			//Communication avec OneM2M
			
			RestTemplate restTemplate = new RestTemplate();
			temp = restTemplate.getForObject(url_getTemp,int.class);
			if ((temp > 35) | (temp<15)) {
			HttpHeaders headers = new HttpHeaders();
			
			headers.set("X-M2M-ORIGIN", "admin:admin");
			headers.set("Content-Type", "application/xml;ty=4");
			//headers.set("Accept", "application/xml");
			String parameters = "	\n"
					+ "<m2m:cin xmlns:m2m=\"http://www.onem2m.org/xml/protocols\">\n"
					+ "    <cnf>message</cnf>\n"
					+ "    <con>\n"
					+ "      &lt;obj&gt;\n"
					+ "        &lt;str name=&quot;location&quot; val=&quot;GEI213&quot;/&gt;\n"
					+ "			&lt;str name=&quot;data&quot; val=&quot;" + String.valueOf(temp) +"&quot;/&gt;\n"
					+ "        \n"
					+ "      &lt;/obj&gt;\n"
					+ "    </con>\n"
					+ "</m2m:cin>";
			
			HttpEntity <String> entity = new HttpEntity <String> (parameters,headers);
			String url = "http://127.0.0.1:8080/~/in-cse/in-name/Alarm_Temperature/DATA/";
			
			restTemplate.exchange(url,HttpMethod.POST,entity,String.class);
			}
		}
			
			//Notification CO2 elevée
			if (CO2AlarmActivated) {
				String url_getCO2 = captCO2URI + "getCO2/";
				double CO2;
				//Communication avec OneM2M
				
				RestTemplate restTemplate = new RestTemplate();
				CO2 = restTemplate.getForObject(url_getCO2,double.class);
				if (CO2 > 1000) {
				HttpHeaders headers = new HttpHeaders();
				
				headers.set("X-M2M-ORIGIN", "admin:admin");
				headers.set("Content-Type", "application/xml;ty=4");
				//headers.set("Accept", "application/xml");
				String parameters = "	\n"
						+ "<m2m:cin xmlns:m2m=\"http://www.onem2m.org/xml/protocols\">\n"
						+ "    <cnf>message</cnf>\n"
						+ "    <con>\n"
						+ "      &lt;obj&gt;\n"
						+ "        &lt;str name=&quot;location&quot; val=&quot;GEI213&quot;/&gt;\n"
						+ "			&lt;str name=&quot;data&quot; val=&quot;" + String.valueOf(CO2) +"&quot;/&gt;\n"
						+ "        \n"
						+ "      &lt;/obj&gt;\n"
						+ "    </con>\n"
						+ "</m2m:cin>";
				
				HttpEntity <String> entity = new HttpEntity <String> (parameters,headers);
				String url = "http://127.0.0.1:8080/~/in-cse/in-name/Alarm_CO2/DATA/";
				
				restTemplate.exchange(url,HttpMethod.POST,entity,String.class);
				}
		}
			
			//Gestion de la date:
			Date date = new Date();
			RestTemplate restTemplate = new RestTemplate();
			int day = date.getDay();
			if ((day==0)|(day==6)) {
				//On eteint le chauffage
				restTemplate.getForObject(automChauffageURI+"setActive/?active=false",double.class);
				//On eteint la lumière
				restTemplate.getForObject(automLumiereURI+"setON/?oN=false",double.class);
				//On ferme les fenetres
				restTemplate.getForObject(automFenetreURI+"setOpen/?open=false",double.class);
			}
			
		
	
		
		return 0;
		
		
		
	}
	
	

	 
     
     
	
	
	
	public static void main(String[] args) {
		SpringApplication.run(ControllerApplication.class, args);
		
	
	}

}
