<?php global $toplevel; global $system; global $isLoggedIn; global $lang; global $pages; $pages = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/pages.json"), true); global $lang; global $pages; ?>
<span>
    <?php

$membersNames = array_map(function ($i) {
    return $i["name"];
}, scoreOrderGlobal());
$parts = explode("/", $_GET['_']);
$name = $toplevel === "" ? "home" : $toplevel;

$actionsProfile = "default";
$part1 = "<page>";
$part2 = null;
$part3 = null;

if (isset($pages[$name])) {
    if (($name === "docs" || $name === "toys" || $name === "actions" || $name === "computers") && isset($parts[2])) {
        $part3 = "<a class='title-bar-backlink' href='/-/dashboard'>Dashboard</a>";
        $part2 = "<a class='title-bar-backlink' href='/-/$name'>" . $pages[$name]['name'][$lang["_name"]] . "</a>";

        if ($name === "docs") {
            $actionsProfile = "docs-item";
            $part1 = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/docs/" . $parts[2] . ".json"), true)["name"];
        } elseif ($name === "computers") {
            $part1 = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/computers/metadata/" . $parts[2] . ".json"), true)["host"];
        } else {
            $actionsProfile = "$name-item";
            $list = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/" . ($name === "toys" ? "toys/toys" : "actions/actions") . ".json"), true);
            $part1 = array_values(array_filter($list, function ($i) use ($parts) {
                return $i["id"] === $parts[2];
            }))[0]["name"];
        }
    } else {
        if ($pages[$name]["admin"]) {
            $part1 = $pages[$name]['name'][$lang["_name"]];
            $part2 = "<a class='title-bar-backlink' href='/-/dashboard'>Dashboard</a>";
        } else {
            $part1 = $pages[$name]['name'][$lang["_name"]];
        }
    }
} elseif ($name === "cloudburst") {
    if (isset($parts[2])) {
        $part2 = "<a class='title-bar-backlink' href='/cloudburst'>Cloudburst System</a>";

        if ($parts[2] === "subsystem") {
            $actionsProfile = "subsystem";
            $part1 = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/subsystems/ynmuc-$parts[3].json"), true)["name"];
        } else {
            $part1 = $pages["s:" . $parts[2]]['name'][$lang["_name"]] ?? $parts[2];
        }
    } else {
        $actionsProfile = "system";
        $part1 = "Cloudburst System";
    }
} elseif ($name === "raindrops") {
    if (isset($parts[2])) {
        $part2 = "<a class='title-bar-backlink' href='/raindrops'>Raindrops System</a>";

        if ($parts[2] === "subsystem") {
            $actionsProfile = "subsystem";
            $part1 = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/subsystems/gdapd-$parts[3].json"), true)["name"];
        } else {
            $part1 = $pages["s:" . $parts[2]]['name'][$lang["_name"]] ?? $parts[2];
        }
    } else {
        $actionsProfile = "system";
        $part1 = "Raindrops System";
    }
} else {
    $member = getMemberFromName($name);
    $actionsProfile = "member";

    if (isset($member)) {
        $memberName = $member["display_name"] ?? $member["name"];
        $part1 = "$memberName";

        if ($member["_system"] === "gdapd") {
            $part2 = "<a class='title-bar-backlink' href='/raindrops'>Raindrops System</a>";
        } else {
            $part2 = "<a class='title-bar-backlink' href='/cloudburst'>Cloudburst System</a>";
        }
    } else {
        $part1 = "$name";
    }
}

if ($actionsProfile === "default") {
    switch ($name) {
        case "home":
            $actionsProfile = "home";
            break;

        case "emergency":
        case "wakeup":
        case "pleasure":
            $actionsProfile = "alert";
            break;

        case "fronting":
            $actionsProfile = "planner";
            break;

        case "actions":
        case "toys":
            $actionsProfile = "actions-or-toys";
            break;

        case "docs":
            $actionsProfile = "docs";
            break;

        case "edit":
        case "edit-private":
            if (count($parts) === 3) $actionsProfile = "editor-system";
            if (count($parts) > 3 && in_array($parts[3], $membersNames)) $actionsProfile = "editor-member";
            if (count($parts) > 3 && !in_array($parts[3], $membersNames)) $actionsProfile = "editor-subsystem";
            break;

        case "debug":
            $actionsProfile = "debug";
            break;

        case "bitset":
            $actionsProfile = "bitset";
            break;

        case "computers":
            if (count($parts) === 3) $actionsProfile = "computers";
            if (count($parts) === 4) $actionsProfile = "computers-remote";
            break;
    }
}

echo("<span id='title-bar-parts-desktop''>");
if (isset($part3)) {
    echo "$part3 > $part2 > <b>$part1</b>";
} elseif (isset($part2)) {
    echo "$part2 > <b>$part1</b>";
} else {
    echo "<b>$part1</b>";
}
echo("</span>");
echo("<span id='title-bar-parts-mobile' style='display:none;'>");
echo "<b>$part1</b>";
echo("</span>");

$links = [
    "default" => [
        [
            "name" => $lang["titlebar"]["top"],
            "link" => [
                "type" => "js",
                "text" => "document.body.scrollTop = 0; document.documentElement.scrollTop = 0;"
            ],
            "icon" => "/assets/icons/up.svg",
            "invert" => true
        ]
    ],
    "computers" => [
        [
            "name" => "Remote control",
            "link" => [
                "type" => "url",
                "text" => "/-/computers/" . ($parts[2] ?? null) . "/control"
            ],
            "icon" => "/assets/icons/computers-remote.svg",
            "invert" => true
        ],
        [
            "name" => "Screens",
            "link" => [
                "type" => "url",
                "text" => "#screens"
            ],
            "icon" => "/assets/icons/computers-screens.svg",
            "invert" => true
        ],
        [
            "name" => "Windows",
            "link" => [
                "type" => "url",
                "text" => "#windows"
            ],
            "icon" => "/assets/icons/computers-windows.svg",
            "invert" => true
        ],
        [
            "name" => $lang["titlebar"]["top"],
            "link" => [
                "type" => "js",
                "text" => "document.body.scrollTop = 0; document.documentElement.scrollTop = 0;"
            ],
            "icon" => "/assets/icons/up.svg",
            "invert" => true
        ]
    ],
    "computers-remote" => [
        [
            "name" => "Enable controlling",
            "link" => [
                "type" => "js",
                "text" => "enableControl();"
            ],
            "icon" => "/assets/icons/computers-on.svg",
            "invert" => true
        ],
        [
            "name" => "Disable controlling",
            "link" => [
                "type" => "js",
                "text" => "disableControl();"
            ],
            "icon" => "/assets/icons/computers-off.svg",
            "invert" => true
        ],
        [
            "name" => "Back to computer info",
            "link" => [
                "type" => "url",
                "text" => "/-/computers/" . ($parts[2] ?? null)
            ],
            "icon" => "/assets/icons/up.svg",
            "invert" => true
        ]
    ],
    "editor-member" => [
        [
            "name" => "View page",
            "link" => [
                "type" => "url",
                "text" => "/" . ($parts[3] ?? null)
            ],
            "icon" => "/assets/icons/view.svg",
            "invert" => true
        ],
        [
            "name" => $lang["titlebar"]["top"],
            "link" => [
                "type" => "js",
                "text" => "document.body.scrollTop = 0; document.documentElement.scrollTop = 0;"
            ],
            "icon" => "/assets/icons/up.svg",
            "invert" => true
        ]
    ],
    "editor-system" => [
        [
            "name" => "View page",
            "link" => [
                "type" => "url",
                "text" => "/" . ($parts[2] ?? null)
            ],
            "icon" => "/assets/icons/view.svg",
            "invert" => true
        ],
        [
            "name" => $lang["titlebar"]["top"],
            "link" => [
                "type" => "js",
                "text" => "document.body.scrollTop = 0; document.documentElement.scrollTop = 0;"
            ],
            "icon" => "/assets/icons/up.svg",
            "invert" => true
        ]
    ],
    "editor-subsystem" => [
        [
            "name" => "View page",
            "link" => [
                "type" => "url",
                "text" => "/" . ($parts[2] ?? null) . "/-/subsystem" . ($parts[3] ?? null)
            ],
            "icon" => "/assets/icons/view.svg",
            "invert" => true
        ],
        [
            "name" => $lang["titlebar"]["top"],
            "link" => [
                "type" => "js",
                "text" => "document.body.scrollTop = 0; document.documentElement.scrollTop = 0;"
            ],
            "icon" => "/assets/icons/up.svg",
            "invert" => true
        ]
    ],
    "member" => [
        [
            "name" => $lang["titlebar"]["member"],
            "link" => [
                "type" => "url",
                "text" => "/$system"
            ],
            "icon" => "/assets/icons/parent.svg",
            "invert" => true
        ],
        [
            "name" => $pages["edit-private"]["name"][$lang["_name"]],
            "link" => [
                "type" => "url",
                "text" => "/-/edit-private/$system/$name"
            ],
            "icon" => "/assets/icons/edit-private.svg",
            "invert" => true,
            "show" => $isLoggedIn
        ],
        [
            "name" => $pages["edit"]["name"][$lang["_name"]],
            "link" => [
                "type" => "url",
                "text" => "/-/edit/$system/$name"
            ],
            "icon" => "/assets/icons/edit.svg",
            "invert" => true,
            "show" => $isLoggedIn
        ],
        [
            "name" => $lang["titlebar"]["top"],
            "link" => [
                "type" => "js",
                "text" => "document.body.scrollTop = 0; document.documentElement.scrollTop = 0;"
            ],
            "icon" => "/assets/icons/up.svg",
            "invert" => true
        ]
    ],
    "subsystem" => [
        [
            "name" => $lang["titlebar"]["subsystem"],
            "link" => [
                "type" => "url",
                "text" => "/$parts[0]"
            ],
            "icon" => "/assets/icons/parent.svg",
            "invert" => true
        ],
        [
            "name" => $pages["edit"]["name"][$lang["_name"]],
            "link" => [
                "type" => "url",
                "text" => "/-/edit/$system/" . ($parts[3] ?? null)
            ],
            "icon" => "/assets/icons/edit.svg",
            "invert" => true,
            "show" => $isLoggedIn
        ],
        [
            "name" => $lang["titlebar"]["top"],
            "link" => [
                "type" => "js",
                "text" => "document.body.scrollTop = 0; document.documentElement.scrollTop = 0;"
            ],
            "icon" => "/assets/icons/up.svg",
            "invert" => true
        ]
    ],
    "home" => [
        [
            "name" => "Raindrops System",
            "link" => [
                "type" => "url",
                "text" => "/raindrops"
            ],
            "icon" => "/assets/avatars/7d9f543ef74240f69d0786c3f2983124.webp",
            "invert" => false
        ],
        [
            "name" => $lang["titlebar"]["top"],
            "link" => [
                "type" => "js",
                "text" => "document.body.scrollTop = 0; document.documentElement.scrollTop = 0;"
            ],
            "icon" => "/assets/icons/up.svg",
            "invert" => true
        ]
    ],
    "planner" => [
        [
            "name" => "Add in the Cloudburst System",
            "link" => [
                "type" => "js",
                "text" => "document.getElementById('planner-add-link-cloudburst-0').click()"
            ],
            "icon" => "/assets/icons/add-cloudburst.svg",
            "invert" => false
        ],
        [
            "name" => "Add in the Raindrops System",
            "link" => [
                "type" => "js",
                "text" => "document.getElementById('planner-add-link-raindrops-0').click()"
            ],
            "icon" => "/assets/icons/add-raindrops.svg",
            "invert" => false
        ]
    ],
    "actions-or-toys" => [
        [
            "name" => "Search",
            "link" => [
                "type" => "js",
                "text" => "document.body.scrollTop = 0; document.documentElement.scrollTop = 0; document.getElementById('search').focus();"
            ],
            "icon" => "/assets/icons/search.svg",
            "invert" => true
        ],
        [
            "name" => "Add",
            "link" => [
                "type" => "js",
                "text" => "(new bootstrap.Modal(document.getElementById('creator'))).show(); setInterval(() => { document.getElementById('creator-title').focus(); }, 500);"
            ],
            "icon" => "/assets/icons/add.svg",
            "invert" => true
        ]
    ],
    "docs" => [
        [
            "name" => "Marked for deletion",
            "link" => [
                "type" => "url",
                "text" => "#deletable"
            ],
            "icon" => "/assets/icons/delete.svg",
            "invert" => true
        ],
        [
            "name" => "Create new",
            "link" => [
                "type" => "url",
                "text" => "/-/docs/add"
            ],
            "icon" => "/assets/icons/add.svg",
            "invert" => true
        ],
        [
            "name" => $lang["titlebar"]["top"],
            "link" => [
                "type" => "js",
                "text" => "document.body.scrollTop = 0; document.documentElement.scrollTop = 0;"
            ],
            "icon" => "/assets/icons/up.svg",
            "invert" => true
        ]
    ],
    "docs-item" => [
        [
            "name" => "Save",
            "link" => [
                "type" => "js",
                "text" => "save();"
            ],
            "icon" => "/assets/icons/save.svg",
            "invert" => true
        ],
        [
            "name" => $lang["titlebar"]["top"],
            "link" => [
                "type" => "js",
                "text" => "document.body.scrollTop = 0; document.documentElement.scrollTop = 0;"
            ],
            "icon" => "/assets/icons/up.svg",
            "invert" => true
        ]
    ],
    "debug" => [
        [
            "name" => $lang["titlebar"]["debug"][0],
            "link" => [
                "type" => "url",
                "text" => "#failures"
            ],
            "icon" => "/assets/icons/failures.svg",
            "invert" => true
        ],
        [
            "name" => $lang["titlebar"]["debug"][1],
            "link" => [
                "type" => "url",
                "text" => "#times"
            ],
            "icon" => "/assets/icons/time.svg",
            "invert" => true
        ],
        [
            "name" => $lang["titlebar"]["top"],
            "link" => [
                "type" => "js",
                "text" => "document.body.scrollTop = 0; document.documentElement.scrollTop = 0;"
            ],
            "icon" => "/assets/icons/up.svg",
            "invert" => true
        ]
    ],
    "bitset" => [
        [
            "name" => "Copy bitset",
            "link" => [
                "type" => "js",
                "text" => "navigator.clipboard.writeText(document.getElementById('input').value);"
            ],
            "icon" => "/assets/icons/copy.svg",
            "invert" => true
        ],
        [
            "name" => "Reset",
            "link" => [
                "type" => "js",
                "text" => "document.getElementById('input').value = '2048'; calculateInput();"
            ],
            "icon" => "/assets/icons/reset.svg",
            "invert" => true
        ]
    ],
    "alert" => [
        [
            "name" => "Turn alert on",
            "link" => [
                "type" => "js",
                "text" => "if (document.getElementById('btn-on').style.display === 'block') (new bootstrap.Modal(document.getElementById('turn-on'))).show();"
            ],
            "icon" => "/assets/icons/alert-on.svg",
            "invert" => true
        ],
        [
            "name" => "Turn alert off",
            "link" => [
                "type" => "js",
                "text" => "if (document.getElementById('btn-off').style.display === 'block') (new bootstrap.Modal(document.getElementById('turn-off'))).show();"
            ],
            "icon" => "/assets/icons/alert-off.svg",
            "invert" => true
        ]
    ],
    "system" => [
        [
            "name" => $pages["s:history"]["name"][$lang["_name"]],
            "link" => [
                "type" => "url",
                "text" => "/$name/-/history"
            ],
            "icon" => "/assets/icons/history.svg",
            "invert" => true
        ],
        [
            "name" => $pages["s:compare"]["name"][$lang["_name"]],
            "link" => [
                "type" => "url",
                "text" => "/$name/-/compare"
            ],
            "icon" => "/assets/icons/compare.svg",
            "invert" => true
        ],
        [
            "name" => $pages["s:tree"]["name"][$lang["_name"]],
            "link" => [
                "type" => "url",
                "text" => "/$name/-/tree"
            ],
            "icon" => "/assets/icons/tree.svg",
            "invert" => true
        ],
        [
            "name" => $pages["s:species"]["name"][$lang["_name"]],
            "link" => [
                "type" => "url",
                "text" => "/$name/-/species"
            ],
            "icon" => "/assets/icons/species.svg",
            "invert" => true
        ],
        [
            "name" => $pages["edit"]["name"][$lang["_name"]],
            "link" => [
                "type" => "url",
                "text" => "/-/edit/$name"
            ],
            "icon" => "/assets/icons/edit.svg",
            "invert" => true,
            "show" => $isLoggedIn
        ],
        [
            "name" => $lang["titlebar"]["top"],
            "link" => [
                "type" => "js",
                "text" => "document.body.scrollTop = 0; document.documentElement.scrollTop = 0;"
            ],
            "icon" => "/assets/icons/up.svg",
            "invert" => true
        ]
    ]
];

if ($_SERVER['HTTP_HOST'] !== "fr.equestria.horse") {
    array_unshift($links["home"], [
        "name" => "Cloudburst System",
        "link" => [
            "type" => "url",
            "text" => "/cloudburst"
        ],
        "icon" => "/assets/avatars/ade46823206b4b0cad3ccaae934a5f3b.webp",
        "invert" => false
    ]);
}

$linksList = $links[isset($links[$actionsProfile]) ? $actionsProfile : "default"];

?><!-- (<code><?= $actionsProfile ?></code>)--><!--<pre><?php var_dump($parts); ?></pre>-->
    <span id="title-bar-actions">
        <?php foreach ($linksList as $index => $link): if (!(isset($link["show"]) && !$link["show"])): ?><a style="display: inline-block; padding: 4px;" id="title-bar-action-<?= $index ?>" <?php if ($link["link"]["type"] === "url"): ?>href<?php else: ?>onclick<?php endif; ?>="<?= $link["link"]["text"] ?>" title="<?= $link["name"] ?>" data-bs-toggle="tooltip" class="title-bar-action tooltip-nohelp"><img src="<?= $link["icon"] ?>" <?php if ($link["invert"]): ?>class="dropdown-icon"<?php endif; ?> alt="" style="width:24px;vertical-align: middle;"></a><?php endif; endforeach; ?>
    </span>
</span>

<style>
    .title-bar-backlink {
        color: white !important;
        text-decoration: none;
    }

    .title-bar-backlink:hover {
        opacity: .75;
    }

    #title-bar-actions {
        position: fixed;
        right: 0;
        height: 34px;
        top: 0;
        display: block;
        border-left: 1px solid rgba(255, 255, 255, .25);
    }

    .title-bar-action {
        cursor: pointer;
    }

    .title-bar-action:hover {
        background-color: rgba(255, 255, 255, .1);
    }
</style>