<?php


session_start();



$db = new SQLite3('../db-test/test.db');

$page = $_GET["page"];
$action = $_GET["action"];
$meta = $_GET["meta"];

$settings = json_decode(file_get_contents("../settings.json"), true);
$plug = $settings["plug"]; //$plug ist die plug-ID!

$bridgeName = $_SESSION["meta"];
$programID = json_decode(urldecode($_SESSION["meta"]), true);



switch($action) {

	case "remove":

		$settings = json_decode(file_get_contents("../settings.json"), true);

		$plug = $settings["plug"]; //$plug ist die plug-ID!
		
		// Überprüfen, ob die Struktur korrekt ist
		if (!isset($meta)) {
			$message[0] = false;
			$message[1] = "Es gab einen Fehler bei der Datenübermittlung.";
			$message = json_encode($message);
			print($message);
			die();
		}

		// Hier wichtig! Beim Löschvorgang muss die referenzielle Integrität gewahrt werden! Erst die Fremdschlüssel löschen, dann die Tabelle mit dem Primärschlüssel!
		
		$db->exec("  DELETE FROM measurement_nodes WHERE required_by =  '" . $meta  . "'; ");
		$db->exec("  DELETE FROM measurement_program_id_bits WHERE measurement_id =  '" . $meta  . "'; ");
		$db->exec("  DELETE FROM measurements WHERE id =  '" . $meta  . "'; ");


		$message[0] = true;
		$message[1] = "Die Messung wurde gelöscht.";
		$message = json_encode($message);
		print($message);
		die();



	break;


	case "add":

		$meta = json_decode($meta, true);

		if(empty($meta["programCache"]) || empty($_SESSION["bridges"]) || empty($meta['name'])) {
			$message[0] = false;
			$message[1] = "Es gab einen Fehler beim Speichern!";
			$message = json_encode($message);
			print($message);
			die();
		}


		$db->exec("INSERT INTO places (plug_id, name) VALUES (" . $plug  . ", '" .  $meta["name"]. "'); ");

		$newdata["plugID"] = $plug;
		$newdata["place"] = $placeID;
		$newdata["programID"] = $meta["programCache"];
		$bridges_original = $_SESSION["bridges"];
		$newdata["special"] = $meta["special"];
		$bridges_hidden = json_decode(urldecode($meta["bridges-hidden"]));
		$newdata["timestamp"] = time();
		
		
		
		// $bridges_result = $bridges_original - $bridges_hidden
		$bridges_original;
		$bridges_hidden;
		
		
		
		// Hilfsfunktion: vergleicht zwei Arrays inhaltlich, unabhängig von String/Int
		function arrays_are_equal($a, $b) {
		    return ((string)$a[0] === (string)$b[0]) && ((string)$a[1] === (string)$b[1]);
		}

		// Ergebnisarray initialisieren
		$bridges_result = [];

		// Für jedes Originalelement prüfen, ob es in hidden vorkommt
		foreach ($bridges_original as $orig) {
		    $found = false;
		    foreach ($bridges_hidden as $hidden) {
			if (arrays_are_equal($orig, $hidden)) {
			    $found = true;
			    break;
			}
		    }
		    if (!$found) {
			$bridges_result[] = $orig;
		    }
		}

		
		$newdata["bridges"] = $bridges_result;
		
		
		

		$db->exec("INSERT INTO measurements (place_name, comment, timestamp) VALUES ('" .  $meta["name"]  . "', '" .  $meta["special"] . "', " .  time() . ");");

		$measurement_id = $db->lastInsertRowID();

		$i = 0;
		foreach($meta["programCache"] as $program_id_char) {
			$db->exec("INSERT INTO measurement_program_id_bits (value, position, measurement_id) VALUES ('" .  $program_id_char  . "', '" .  $i. "', '" .  $measurement_id . "');  ");
			$i++;
		}

		foreach($newdata["bridges"] as $bridge) {
			$db->exec("INSERT INTO measurement_nodes (required_by, node_from, node_to) VALUES (" .  $measurement_id  . ", '" .  $bridge[0]. "', '" .  $bridge[1] . "');  ");
		}



		$message[0] = true;
		$message[1] = "Die Messung wurde am Ort '" . $meta['name'] . "' gespeichert!";
		$message = json_encode($message);
		print($message);
		die();
	break;
}
