<?php

$pages = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/pages.json"), true);

$subsystems1 = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/subsystems/ynmuc.json"), true);
$cloudburst_subsystemsNotMember = array_values(array_filter($subsystems1, function ($i) {
    return $i["source_type"] !== "member";
}));

$subsystems2 = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/subsystems/gdapd.json"), true);
$raindrops_subsystemsNotMember = array_values(array_filter($subsystems2, function ($i) {
    return $i["source_type"] !== "member";
}));

global $navigation;
global $toplevel;
global $lang; global $pages;

$navigation_admin = [
    "admin" => true,
    "name" => $lang["navigation"]["admin"],
    "icon" => "/assets/icons/admin.svg",
    "invert" => true,
    "items" => [
        "alerts" => [
            "name" => null,
            "minimal" => true,
            "items" => [
                [
                    "name" => $pages["emergency"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/emergency.svg",
                    "invert" => false,
                    "link" => "/-/emergency",
                    "stepped" => "rgb(220,53,69)"
                ],
                [
                    "name" => $pages["wakeup"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/wakeup.svg",
                    "invert" => false,
                    "link" => "/-/wakeup",
                    "stepped" => "rgb(13,202,240)"
                ]
            ]
        ],
        "apps" => [
            "name" => $lang["navigation"]["apps"],
            "minimal" => false,
            "items" => [
                [
                    "name" => $pages["about"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/about.svg",
                    "invert" => true,
                    "link" => "/-/about",
                    "stepped" => null
                ],
                [
                    "name" => $pages["fronting"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/fronting.svg",
                    "invert" => true,
                    "link" => "/-/fronting",
                    "stepped" => null
                ],
                [
                    "name" => $pages["profiles"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/profiles.svg",
                    "invert" => true,
                    "link" => "/-/profiles",
                    "stepped" => null
                ],
                [
                    "name" => $pages["money"]["name"][$lang["_name"]] . " <span class='badge bg-success rounded-pill'>Beta</span>",
                    "icon" => "/assets/icons/money.svg",
                    "invert" => true,
                    "link" => "/-/money",
                    "stepped" => null
                ],
                [
                    "name" => $pages["rules"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/rules.svg",
                    "invert" => true,
                    "link" => "/-/rules",
                    "stepped" => null
                ],
                [
                    "name" => $pages["docs"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/docs.svg",
                    "invert" => true,
                    "link" => "/-/docs",
                    "stepped" => null
                ],
                [
                    "name" => $pages["computers"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/computers.svg",
                    "invert" => true,
                    "link" => "/-/computers",
                    "stepped" => null
                ],
                [
                    "name" => $pages["travelling"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/travelling.svg",
                    "invert" => true,
                    "link" => "/-/travelling",
                    "stepped" => null
                ],
                [
                    "name" => $pages["stats"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/stats.svg",
                    "invert" => true,
                    "link" => "/-/stats",
                    "stepped" => null
                ]
            ]
        ],
        "sort" => [
            "name" => "Sorted members lists",
            "minimal" => false,
            "items" => [
                [
                    "name" => $pages["splitting"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/splitting.svg",
                    "invert" => true,
                    "link" => "/-/splitting",
                    "stepped" => null
                ],
                [
                    "name" => $pages["byfront"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/byfront.svg",
                    "invert" => true,
                    "link" => "/-/byfront",
                    "stepped" => null
                ],
                [
                    "name" => $pages["alphabet"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/alphabet.svg",
                    "invert" => true,
                    "link" => "/-/alphabet",
                    "stepped" => null
                ],
                [
                    "name" => $pages["s:species"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/species.svg",
                    "invert" => true,
                    "link" => "/-/byspecies",
                    "stepped" => null
                ],
            ]
        ],
        /*"debug" => [
            "name" => $lang["navigation"]["debug"],
            "minimal" => false,
            "items" => [
                [
                    "name" => $pages["debug"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/debug.svg",
                    "invert" => true,
                    "link" => "/-/debug",
                    "stepped" => null
                ],
                [
                    "name" => $pages["bitset"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/bitset.svg",
                    "invert" => true,
                    "link" => "/-/bitset",
                    "stepped" => null
                ],
                [
                    "name" => $pages["score"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/score.svg",
                    "invert" => true,
                    "link" => "/-/score",
                    "stepped" => null
                ],
                [
                    "name" => $pages["logout"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/logout.svg",
                    "invert" => true,
                    "link" => "/-/logout",
                    "stepped" => null
                ]
            ]
        ]*/
    ]
];
$navigation_global = [
    "admin" => false,
    "name" => $lang["navigation"]["general"],
    "icon" => "/assets/icons/global.svg",
    "invert" => true,
    "items" => [
        "main" => [
            "name" => null,
            "minimal" => false,
            "items" => [
                [
                    "name" => $pages["home"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/home.svg",
                    "invert" => true,
                    "link" => "/",
                    "stepped" => null
                ],
                [
                    "name" => $pages["disclaimers"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/disclaimers.svg",
                    "invert" => true,
                    "link" => "/-/disclaimers",
                    "stepped" => null
                ],
                [
                    "name" => $pages["relations"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/relations.svg",
                    "invert" => true,
                    "link" => "/-/relations",
                    "stepped" => null
                ],
                ($_SERVER['HTTP_HOST'] !== "fr.equestria.horse" ? [
                    "name" => $pages["government"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/government.svg",
                    "invert" => true,
                    "link" => "/-/government",
                    "stepped" => null
                ] : null),
                [
                    "name" => $pages["terminology"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/terminology.svg",
                    "invert" => true,
                    "link" => "/-/terminology",
                    "stepped" => null
                ]
            ]
        ]
    ]
];
$navigation_cloudburst = [
    "admin" => $_SERVER['HTTP_HOST'] === "fr.equestria.horse",
    "name" => "Cloudburst System",
    "icon" => getAsset("ynmuc"),
    "invert" => false,
    "items" => [
        "header" => [
            "name" => null,
            "minimal" => false,
            "items" => [
                [
                    "name" => $lang["navigation"]["about"],
                    "icon" => "/assets/icons/about.svg",
                    "invert" => true,
                    "link" => "/cloudburst",
                    "stepped" => null
                ],
                [
                    "name" => $pages["s:history"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/history.svg",
                    "invert" => true,
                    "link" => "/cloudburst/-/history",
                    "stepped" => null
                ],
                [
                    "name" => $pages["s:compare"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/compare.svg",
                    "invert" => true,
                    "link" => "/cloudburst/-/compare",
                    "stepped" => null
                ],
                [
                    "name" => $pages["s:species"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/species.svg",
                    "invert" => true,
                    "link" => "/cloudburst/-/species",
                    "stepped" => null
                ]
            ]
        ],
        "subsystems" => [
            "name" => $lang["navigation"]["subsystems"],
            "minimal" => false,
            "items" => array_map(function ($subsystem) {
                $ssData = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/subsystems/ynmuc-" . $subsystem['source'] . ".json"), true);

                return [
                    "name" => $ssData['name'] ?? $subsystem['source'],
                    "icon" => "/assets/uploads/ss-$subsystem[source].png",
                    "invert" => false,
                    "link" => "/cloudburst/-/subsystem/$subsystem[source]",
                    "stepped" => null
                ];
            }, $cloudburst_subsystemsNotMember)
        ],
        "members" => [
            "name" => $lang["navigation"]["members"],
            "minimal" => false,
            "items" => array_map(function ($member) {
                return [
                    "name" => $member['display_name'] ?? $member['name'],
                    "icon" => getAsset($member["system"], $member["id"], "heads"),
                    "invert" => false,
                    "link" => "/$member[name]",
                    "stepped" => null
                ];
            }, array_filter(scoreOrder(withTravelers(json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/ynmuc/members.json"), true), "ynmuc"), "ynmuc"), function ($member) {
                return $member['name'] !== "unknown" && $member['name'] !== "fusion" && $member['name'] !== "new";
            }))
        ]
    ]
];
$navigation_raindrops = [
    "admin" => false,
    "name" => "Raindrops System",
    "icon" => getAsset("gdapd"),
    "invert" => false,
    "items" => [
        "header" => [
            "name" => null,
            "minimal" => false,
            "items" => [
                [
                    "name" => $lang["navigation"]["about"],
                    "icon" => "/assets/icons/about.svg",
                    "invert" => true,
                    "link" => "/raindrops",
                    "stepped" => null
                ],
                [
                    "name" => $pages["s:history"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/history.svg",
                    "invert" => true,
                    "link" => "/raindrops/-/history",
                    "stepped" => null
                ],
                [
                    "name" => $pages["s:compare"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/compare.svg",
                    "invert" => true,
                    "link" => "/raindrops/-/compare",
                    "stepped" => null
                ],
                [
                    "name" => $pages["s:species"]["name"][$lang["_name"]],
                    "icon" => "/assets/icons/species.svg",
                    "invert" => true,
                    "link" => "/raindrops/-/species",
                    "stepped" => null
                ]
            ]
        ],
        "subsystems" => [
            "name" => $lang["navigation"]["subsystems"],
            "minimal" => false,
            "items" => array_map(function ($subsystem) {
                $ssData = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/subsystems/gdapd-" . $subsystem['source'] . ".json"), true);

                return [
                    "name" => $ssData['name'] ?? $subsystem['source'],
                    "icon" => "/assets/uploads/ss-$subsystem[source].png",
                    "invert" => false,
                    "link" => "/raindrops/-/subsystem/$subsystem[source]",
                    "stepped" => null
                ];
            }, $raindrops_subsystemsNotMember)
        ],
        "members" => [
            "name" => $lang["navigation"]["members"],
            "minimal" => false,
            "items" => array_map(function ($member) {
                return [
                    "name" => $member['display_name'] ?? $member['name'],
                    "icon" => getAsset($member["system"], $member["id"], "heads"),
                    "invert" => false,
                    "link" => "/$member[name]",
                    "stepped" => null
                ];
            }, array_filter(scoreOrder(withTravelers(json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/gdapd/members.json"), true), "gdapd"), "gdapd"), function ($member) {
                return $member['name'] !== "unknown" && $member['name'] !== "fusion" && $member['name'] !== "new";
            }))
        ]
    ]
];

global $parts;

if (isset($parts) && isset($parts[0]) && $parts[0] === "cloudburst") {
    $navigation = [
        "cloudburst" => $navigation_cloudburst,
        "raindrops" => $navigation_raindrops,
        "admin" => $navigation_admin,
        "global" => $navigation_global
    ];
} elseif (isset($parts) && isset($parts[0]) && $parts[0] === "raindrops") {
    $navigation = [
        "raindrops" => $navigation_raindrops,
        "cloudburst" => $navigation_cloudburst,
        "admin" => $navigation_admin,
        "global" => $navigation_global
    ];
} else {
    $navigation = [
        "admin" => $navigation_admin,
        "global" => $navigation_global,
        "cloudburst" => $navigation_cloudburst,
        "raindrops" => $navigation_raindrops
    ];
}