<?php
// Setze den Content-Type-Header für SVG
header('Content-Type: image/svg+xml');
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<svg xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     width="500" height="100">

    <defs>
        <style>
            @font-face {
                font-family: LexendDecaBold;
                src: url(http://192.168.37.103/vendor/fonts/LexendDeca-Bold.ttf);
            }

            * {
                font-family: LexendDecaBold;
                font-size: 14px;
            }
        </style>
    </defs>

    <text x="30" y="5" fill="black" text-anchor="middle" transform="translate(50,50) rotate(-90)">30</text>
    <text x="-30" y="5" fill="black" text-anchor="middle" transform="translate(50,50) rotate(-90)">10</text>
    <text x="30" y="405" fill="black" text-anchor="middle" transform="translate(50,50) rotate(-90)">31</text>
    <text x="-30" y="405" fill="black" text-anchor="middle" transform="translate(50,50) rotate(-90)">11</text>


    <path d="M500 0 L50 0" style="stroke:black;stroke-width:5; fill:none;" />
    <path d="M50 0 L0 25" style="stroke:black;stroke-width:3; fill:none;" />
    <path d="M0 25 L0 75" style="stroke:black;stroke-width:5; fill:none;" />
    <path d="M0 75 L50 100" style="stroke:black;stroke-width:3; fill:none;" />
    <path d="M50 100 L500 100" style="stroke:black;stroke-width:5; fill:none;" />
    <path d="M500 100 L500 0" style="stroke:black;stroke-width:5; fill:none;" />


    <rect width="35" height="35" x="452" y="32" rx="5" ry="5" style="stroke:black;stroke-width:3;fill:none" />
    <circle r="8" cx="470" cy="50" fill="none" style="stroke:black;stroke-width:3;" />
    <rect width="35" height="35" x="13" y="32" rx="5" ry="5" style="stroke:black;stroke-width:3;fill:none" />
    <circle r="8" cx="30" cy="50" fill="none" style="stroke:black;stroke-width:3;" />


    <rect width="380" height="80" x="60" y="10" rx="5" ry="5" style="stroke:black;stroke-width:3;fill:none" />

    <?php
    $pin_coordinates = [
        10 => [70, 68], 20 => [70, 44], 30 => [70, 20],
        11 => [405, 68], 12 => [370, 68], 13 => [335, 68], 14 => [300, 68],
        15 => [265, 68], 16 => [230, 68], 17 => [195, 68], 18 => [160, 68], 19 => [125, 68],
        21 => [405, 44], 22 => [370, 44], 23 => [335, 44], 24 => [300, 44],
        25 => [265, 44], 26 => [220, 44], 27 => [185, 44], 28 => [150, 44], 29 => [115, 44],
        31 => [405, 20], 32 => [370, 20], 33 => [335, 20], 34 => [300, 20],
        35 => [265, 20], 36 => [230, 20], 37 => [195, 20], 38 => [160, 20], 39 => [125, 20],
    ];

    // Rechtecke einfärben, wenn "fill"-Modus aktiv ist
    if ($_GET["mode"] == "fill") {
        $data = $_GET["data"];
        $data = json_decode(urldecode($data), true);

        foreach ($pin_coordinates as $key => $coordinates) {
            $fill = in_array($key, $data) ? "green" : "red";
            echo '<rect width="25" height="10" x="' . $coordinates[0] . '" y="' . $coordinates[1] . '" rx="1" ry="1" style="stroke:black;stroke-width:3;fill:' . $fill . '" />';
        }
    } else {
        // Nur Umrisse
        foreach ($pin_coordinates as $coordinates) {
            echo '<rect width="25" height="10" x="' . $coordinates[0] . '" y="' . $coordinates[1] . '" rx="1" ry="1" style="stroke:black;stroke-width:3;fill:none" />';
        }
    }

    // SVG frühzeitig beenden, wenn kein Data vorhanden oder im fill-Modus
    if (empty($_GET['data']) || $_GET["mode"] == "fill") {
        print "</svg>";
        die();
    }

    // Brückenverbindungen übersetzen, falls aktiviert
    if ($_GET["translate"] == "true") {
        $db = new SQLite3('db-test/test.db');
        $data = json_decode($_GET["data"], true);
        $plug = json_decode(file_get_contents("settings.json"), true)["plug"];
        $nodes_raw = [];

        foreach ($data as $bridge) {
            $nodes = $db->query("SELECT node_from, node_to FROM nodes WHERE required_by = " . $bridge);
            while ($node = $nodes->fetchArray(SQLITE3_ASSOC)) {
                $nodes_raw[] = [$node['node_from'], $node['node_to']];
            }
        }
        $inputArray = $nodes_raw;
    } else {
        $inputArray = json_decode($_GET['data'], true);
    }

    // Koordinaten übersetzen
    $translatedArray = array_map(function ($pair) use ($pin_coordinates) {
        return [
            $pin_coordinates[$pair[0]],
            $pin_coordinates[$pair[1]],
        ];
    }, $inputArray);

    $colors = ["#1a5fb4", "#26a269", "#e5a50a", "#c64600", "#a51d2d", "#613583", "#63452c", "#3d3846"];

    // Linien ausgeben
    $colorNumber = 0;
    foreach ($translatedArray as $line) {
        $point1 = $line[0];
        $point2 = $line[1];

        $point1x = $point1[0] + 10;
        $point1y = $point1[1] + 5;
        $point2x = $point2[0] + 10;
        $point2y = $point2[1] + 5;

        echo '<path d="M' . $point1x . ' ' . $point1y . ' L' . $point2x . ' ' . $point2y .
            '" style="stroke:' . $colors[$colorNumber] . ';stroke-width:3; fill:none;" />';

        $colorNumber = ($colorNumber + 1) % count($colors);
    }
    ?>
</svg>
