package code;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

public class Antifurto implements HttpHandler {
	
	private boolean statoAntifurto = true;
	private boolean statoAllarme = false;

	@Override
	public void handle(HttpExchange exchange) throws IOException {
		URI requestURI = exchange.getRequestURI();
		String path[] = requestURI.toString().split("/");
		String lastfile = path[path.length - 1];

		String requestMethod = exchange.getRequestMethod();
		List<String> strlist = new ArrayList<>(); //serve List<String> a causa di .put("content-type", strlist);
		strlist.add("text/json");
		exchange.getResponseHeaders().put("content-type", strlist);//opzionale, per dire cosa c'e' nel body
		
		if(Helper.compareText(lastfile,"antifurto")) {//get {stato, allarme, attenzione, soglia, sensori}
			if(Helper.compareText(requestMethod,"GET")) {
				String response = "{\"stato\":true,\"soglia\":43,\"attenzione\":21,\"allarme\":false,\"sensori\":[{\"in0\":8},{\"in2\":3}]}";
				statoAntifurto = true;
				statoAllarme = false;
				Helper.sendResponse(response,exchange);
			} else {
				Helper.methodNotAllowed(exchange);
			}
			return;
		}
		
		if(Helper.compareText(lastfile,"stato")) {//get, put {se l'antifurto e' attivo + aggiornamento}
			if(Helper.compareText(requestMethod,"GET")) {
				String response = "{\"stato\":true}";
				Helper.sendResponse(response,exchange);
			} else if(Helper.compareText(requestMethod,"PUT")) {
				String body = Helper.readBody(exchange.getRequestBody());
				if(!Helper.checkJSON(body) || !body.contains("\"stato\"")) {
					Helper.badRequest(exchange);
					return;
				}
				String stato = body.split("\"stato\"",2)[1].split(":",2)[1].split(",",2)[0].split("}",2)[0].trim();

				try	{
					boolean exStato = statoAntifurto;
					statoAntifurto = Boolean.parseBoolean(stato);
					if(exStato == statoAntifurto)
						System.out.println("\tRicevuto stato antifurto gia' assegnato");
					if(statoAllarme && !statoAntifurto) {
						Helper.badRequest(exchange);
						return;
					}
					
					String response = "{\"antifurto\":\"ok\",\"stato\":"+statoAntifurto+"}";
					Helper.sendResponse(response,exchange);
				} catch (Exception e) {
					System.out.println("Conversion error.\t"+stato);
					e.printStackTrace();
					Helper.badRequest(exchange);
				}
			} else {
				Helper.methodNotAllowed(exchange);
			}
			return;
		}
		
		if(Helper.compareText(lastfile,"allarme")) {//get, put {se l'allarme sta suonando + aggiornamento}
			if(Helper.compareText(requestMethod,"GET")) {
				String response = "{\"dati\":\"ecco l'allarme\"}";
				Helper.sendResponse(response,exchange);
			} else if(Helper.compareText(requestMethod,"PUT")) {
				String body = Helper.readBody(exchange.getRequestBody());
				if(!Helper.checkJSON(body) || !body.contains("\"stato\"")) {
					Helper.badRequest(exchange);
					return;
				}
				String stato = body.split("\"stato\"",2)[1].split(":",2)[1].split(",",2)[0].split("}",2)[0].trim();

				try	{
					boolean exStato = statoAllarme;
					statoAllarme = Boolean.parseBoolean(stato);
					if(exStato == statoAllarme)
						System.out.println("\tRicevuto stato allarme gia' assegnato");
					if(statoAllarme && !statoAntifurto) {
						Helper.badRequest(exchange);
						return;
					}
					String response = "{\"allarme\":\"ok\",\"stato\":"+statoAllarme+"}";
					Helper.sendResponse(response,exchange);
				} catch (Exception e) {
					System.out.println("Conversion error.\t"+stato);
					e.printStackTrace();
					Helper.badRequest(exchange);
				}
			} else {
				Helper.methodNotAllowed(exchange);
			}
			return;
		}
		
		if(Helper.compareText(lastfile,"attenzione")) {//put {valore della progress bar}
			if(Helper.compareText(requestMethod,"PUT")) {
				String body = Helper.readBody(exchange.getRequestBody());
				if(!Helper.checkJSON(body) || !body.contains("\"attenzione\"")) {
					Helper.badRequest(exchange);
					return;
				}
				String valore = body.split("\"attenzione\"",2)[1].split(":",2)[1].split(",",2)[0].split("}",2)[0].trim();
				try {
					int val = Integer.parseInt(valore);

					String response = "{\"attenzione\":"+val+"}";
					Helper.sendResponse(response,exchange);
				} catch (Exception e) {
					System.out.println("Conversion error.\t"+valore);
					e.printStackTrace();
					Helper.badRequest(exchange);
				}
			} else {
				Helper.methodNotAllowed(exchange);
			}
			return;
		}
		
		if(Helper.compareText(lastfile,"soglia")) {//put {valore scelto dall'utente per la soglia}
			if(Helper.compareText(requestMethod,"PUT")) {
				System.out.println(Helper.readBody(exchange.getRequestBody()));
				String response = "{\"dati\":\"soglia aggiornata\"}";
				Helper.sendResponse(response,exchange);
			} else {
				Helper.methodNotAllowed(exchange);
			}
			return;
		}
		
		Helper.pageNotFound(exchange);
	}

}