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

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/";
	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;
	
	
	//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("setTemperatureAlarm/")
	public void setTemperatureAlarm(boolean active) {
		TemperatureAlarmActivated = active;
	}
	
	@GetMapping("setMouvementAlarm/")
	public void setMouvementAlarm(boolean active) {
		MouvementAlarmActivated = active;
	}
	
	
	@GetMapping("run")
	public int run() throws Exception {
		
		if (ControlLumiereAutomatique) {
			ControlLumiereAutomatique();
		}
		
		if (PorteFenetreAutomatique_TemperatureCO2) {
			PorteFenetreAutomatique_TemperatureCO2();
		}
		
		if (PorteFenetreAutomatique_CO2) {
			PorteFenetreAutomatique_CO2();
		}
		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);
			
			return 3;
			
		}
		
		
		
		return 0;
		//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;presence&quot; val=&quot;1&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/Capteur_Mouvement/DATA/";
		
		ResponseEntity respEntity = restTemplate.exchange(url,HttpMethod.POST,entity,String.class);
		String resp = (String) respEntity.getBody();
		return resp;
		*/
		
		
	}
	
	

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

}
