<?php

session_start();

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

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

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


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


switch ($action) {
	case "add":

		$meta = json_decode($meta, true);
		$bridges = $meta["bridges"];
		$programID = $meta["programID"];
		$programDescription = $meta["programDescription"];

		if(empty($bridges) || empty($programID)) {
			$message[0] = false;
			$message[1] = "Es wurden keine Änderungen gespeichert.";
			$message = json_encode($message);
			print($message);
			die();
		}


		$result = $db->query("SELECT COUNT(*) as anzahl FROM program_id_bits WHERE
		position = 0 AND value = '" . $programID[0] . "' AND
		position = 1 AND value = '" . $programID[1] . "' AND 
		position = 2 AND value = '" . $programID[2] . "' AND 
		position = 3 AND value = '" . $programID[3] . "' AND 
		position = 4 AND value = '" . $programID[4] . "' 
		;");
		
		$row = $result->fetchArray(SQLITE3_ASSOC);
		$number = $row['anzahl'];

		if($number > 0) {
			$message[0] = false;
			$message[1] = "Diese Programm-ID existiert unter dem aktuell gewählten Stecker bereits. Bitte anderen Namen wählen oder den Stecker wechseln.";
			$message = json_encode($message);
			print($message);
			die();
		}

		$db->exec("  INSERT INTO programs (plug_id, program_description) VALUES ('" . $plug  . "', '" . $programDescription  . "');   ");

		$program_id = $db->lastInsertRowID();
		foreach($bridges as $bridge) {
			$db->exec("  INSERT INTO program_bridge_relation (program_id, bridge_id, plug_id) VALUES ('" . $program_id  . "', '" . $bridge  . "', '" . $plug  . "');   ");
		}

		for($i=0; $i<=4; $i++) {
			$db->exec("  INSERT INTO program_id_bits (value, position, program_id) VALUES ('" . $programID[$i]  . "', '" . $i  . "', '" . $program_id  . "');   ");
		}

		$message[0] = true;
		$message[1] = 'Das Programm wurde als <div class="label" style="width: 160px; display: inline-block; float: unset; height: unset;margin-bottom: -10px;">
				 <span style="margin: unset;"><span class="label-char">' .  $programID[0] . '</span><span class="label-char">' .  $programID[1] . '</span><span class="label-char">' .  $programID[2] . '</span><span class="label-char">' .  $programID[3] . '</span><span class="label-char">' .  $programID[4] . '</span>
				 </span>
			  </div> gespeichert!';
		$message = json_encode($message);
		print($message);
		die();

	break;
	case "edit":

		$meta = json_decode(urldecode($meta),true);
		$bridges = $meta["bridges"];
		$programDescription = $meta["programDescription"];

		if(empty($bridges) || empty($programDescription)) {
			$message[0] = false;
			$message[1] = "Es wurden keine Änderungen gespeichert.";
			$message = json_encode($message);
			print($message);
			die();
		}

		$db->exec("  UPDATE programs set program_description = '" . $programDescription  . "' WHERE id = '" . $programID  . "' AND plug_id = '" . $plug  . "';  ");
		$db->exec("  DELETE FROM program_bridge_relation WHERE program_id =  '" . $programID  . "' and plug_id =  '" . $plug  . "'; ");

		foreach($bridges as $bridge) {
			$db->exec("  INSERT INTO program_bridge_relation (program_id, bridge_id, plug_id) VALUES ('" . $programID  . "', '" . $bridge  . "', '" . $plug  . "');   ");
		}

		$message[0] = true;
		$message[1] = 'Die Änderungen an <div class="label" style="width: 160px; display: inline-block; float: unset; height: unset;margin-bottom: -10px;"><span style="margin: unset;">';
		
		$program_identifiers = $db->query("SELECT * FROM program_id_bits WHERE program_id = '" . $programID . "';");

		while ($program_identifier = $program_identifiers->fetchArray(SQLITE3_ASSOC)) {
			$message[1] .= '<span class="label-char">' . $program_identifier["value"] . '</span>';
		}
		
		$message[1] .= '</span></div> wurden gespeichert!';
		 
		$message = json_encode($message);
		print($message);
		die();

	break;
	case "remove":
		$programID = $meta;

		// Ü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 program_bridge_relation WHERE program_id =  '" . $meta  . "' and plug_id =  '" . $plug  . "'; ");
		$db->exec("  DELETE FROM program_id_bits WHERE program_id =  '" . $meta  . "'; ");
		$db->exec("  DELETE FROM programs WHERE id =  '" . $meta  . "' and plug_id = '" . $plug  . "'; ");
		
		
		$message[0] = true;
		$message[1] = "Das Programm wurde nun gelöscht.";
		$message = json_encode($message);
		print ($message);
		die();
		

	break;


}
