<?php

require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/init.inc"; global $title; global $isLoggedIn; global $lang; global $pages; global $pages;
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/header.inc'; global $_PROFILE;
use om\IcalParser;

$poniesHavingSex = [];

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

foreach ($actions as $action) {
    if ($action["type"] !== "sexual") continue;

    foreach ($action["ponies"] as $ponies) {
        $id = implode("", $ponies["members"]);

        $member = getMemberWithoutSystem($ponies["members"][0]);
        $otherMember = getMemberWithoutSystem($ponies["members"][1]);

        $parts = [
            $member["id"],
            $otherMember["id"]
        ];

        asort($parts);
        $poniesHavingSex[] = $parts[0];
        $poniesHavingSex[] = $parts[1];
    }
}

$fronter = array_map(function ($i) {
    return $i["id"];
}, ($_PROFILE["login"] === "raindrops" ? json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/gdapd/fronters.json"), true) : json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/ynmuc/fronters.json"), true))["members"])[0] ?? null;

$viewingPonyHasSex = in_array($fronter, $poniesHavingSex) || isset($_GET['toys']);

?>

<br>
<div class="container">
    <div id="page-content">
        <h3 id="date" style="text-align: center;margin-bottom:20px;">Date</h3>
        <script>
            function _Date() {
                let d = new Date();
                let m = d.getMonth();
                let w = d.getDay();

                switch (m) {
                    case 0: m = "Jan"; break;
                    case 1: m = "Feb"; break;
                    case 2: m = "Mar"; break;
                    case 3: m = "Apr"; break;
                    case 4: m = "May"; break;
                    case 5: m = "Jun"; break;
                    case 6: m = "Jul"; break;
                    case 7: m = "Aug"; break;
                    case 8: m = "Sep"; break;
                    case 9: m = "Oct"; break;
                    case 10: m = "Nov"; break;
                    case 11: m = "Dec"; break;
                }

                switch (w) {
                    case 0: w = "Sun"; break;
                    case 1: w = "Mon"; break;
                    case 2: w = "Tue"; break;
                    case 3: w = "Wed"; break;
                    case 4: w = "Thu"; break;
                    case 5: w = "Fri"; break;
                    case 6: w = "Sat"; break;
                }

                function fixed(number) {
                    if (number < 10) {
                        return "0" + number;
                    } else {
                        return number.toString();
                    }
                }

                document.getElementById("date").innerText = `${w} ${d.getDate()} ${m} ${d.getHours()}:${fixed(d.getMinutes())}`;
            }

            setInterval(_Date);
        </script>

        <hr>
        <h4>Next fronters <span class="small"><a href="/-/fronting">(edit)</a></span></h4>
        <?php

        $scheduleCloudburstToday = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/planner/ynmuc.json"), true)[date('Y-m-d')] ?? [];
        $scheduleRaindropsToday = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/planner/gdapd.json"), true)[date('Y-m-d')] ?? [];

        $scheduleCloudburstTomorrow = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/planner/ynmuc.json"), true)[date('Y-m-d', time() + 86400)] ?? [];
        $scheduleRaindropsTomorrow = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/planner/gdapd.json"), true)[date('Y-m-d', time() + 86400)] ?? [];

        $scheduleCloudburstNextday = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/planner/ynmuc.json"), true)[date('Y-m-d', time() + 172800)] ?? [];
        $scheduleRaindropsNextday = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/planner/gdapd.json"), true)[date('Y-m-d', time() + 172800)] ?? [];

        $today1 = $_PROFILE["login"] === "raindrops" ? $scheduleRaindropsToday : $scheduleCloudburstToday;
        $today2 = $_PROFILE["login"] === "raindrops" ? $scheduleCloudburstToday : $scheduleRaindropsToday;
        $tomorrow1 = $_PROFILE["login"] === "raindrops" ? $scheduleRaindropsTomorrow : $scheduleCloudburstTomorrow;
        $tomorrow2 = $_PROFILE["login"] === "raindrops" ? $scheduleCloudburstTomorrow : $scheduleRaindropsTomorrow;
        $nextday1 = $_PROFILE["login"] === "raindrops" ? $scheduleRaindropsNextday : $scheduleCloudburstNextday;
        $nextday2 = $_PROFILE["login"] === "raindrops" ? $scheduleCloudburstNextday : $scheduleRaindropsNextday;

        ?>

        <h5>Fronters today</h5>
        <ul>
            <?php foreach ($today1 as $index => $id): $member = getMemberWithoutSystem($id[0]); ?>
            <li>
                <?php if ($member["name"] === "fusion"): ?>
                <img alt="" src="/assets/logo/logo.png" style="height: 24px; vertical-align: middle;"> <span style="vertical-align: middle;">Merged members</span>
                <?php else: ?>
                <img alt="" src="<?= getAsset($member['system'], $member["id"], "heads") ?>" style="height: 24px; vertical-align: middle;"> <b style="vertical-align: middle;"><?= getMiniName($member["display_name"] ?? $member["name"]) ?></b>
                <?php endif; ?>
                <?php if (isset($id[1])): $member2 = getMemberWithoutSystem($id[1]); ?>
                <b style="vertical-align: middle;">and <img alt="" src="<?= getAsset($member['system'], $member2["id"], "heads") ?>" style="height: 24px; vertical-align: middle;"> <?= getMiniName($member2["display_name"] ?? $member2["name"]) ?></b>
                <?php endif; ?>
                <?php if (isset($today2[$index])): $member2 = getSystemMember($_PROFILE["login"] === "raindrops" ? "ynmuc" : "gdapd", $today2[$index][0]); ?><span style="vertical-align: middle;">with</span> <img alt="" src="<?= getAsset($member2['system'], $member2["id"], "heads") ?>" style="height: 24px; vertical-align: middle;"> <span style="vertical-align: middle;"><?= getMiniName($member2["display_name"] ?? $member2["name"]) ?><?php if (isset($today2[$index][1])): $member2 = getSystemMember($_PROFILE["login"] === "raindrops" ? "ynmuc" : "gdapd", $today2[$index][1]); ?>
                    and <img alt="" src="<?= getAsset($member['system'], $member2["id"], "heads") ?>" style="height: 24px; vertical-align: middle;"> <?= getMiniName($member2["display_name"] ?? $member2["name"]) ?>
                    <?php endif; ?></span><?php endif; ?>
            </li>
            <?php endforeach; ?>
        </ul>

        <h5>Fronters tomorrow</h5>
        <ul>
            <?php foreach ($tomorrow1 as $index => $id): $member = getSystemMember($_PROFILE["login"] === "raindrops" ? "gdapd" : "ynmuc", $id[0]); ?>
            <li>
                <?php if ($member["name"] === "fusion"): ?>
                <img alt="" src="/assets/logo/logo.png" style="height: 24px; vertical-align: middle;"> <span style="vertical-align: middle;">Merged members</span>
                <?php else: ?>
                <img alt="" src="<?= getAsset($member['system'], $member["id"], "heads") ?>" style="height: 24px; vertical-align: middle;"> <b style="vertical-align: middle;"><?= getMiniName($member["display_name"] ?? $member["name"]) ?></b>
                <?php endif; ?>
                <?php if (isset($id[1])): $member2 = getMemberWithoutSystem($id[1]); ?>
                    <b style="vertical-align: middle;">and <img alt="" src="<?= getAsset($member['system'], $member2["id"], "heads") ?>" style="height: 24px; vertical-align: middle;"> <?= getMiniName($member2["display_name"] ?? $member2["name"]) ?></b>
                <?php endif; ?>
                <?php if (isset($tomorrow2[$index])): $member2 = getSystemMember($_PROFILE["login"] === "raindrops" ? "ynmuc" : "gdapd", $tomorrow2[$index][0]); ?><span style="vertical-align: middle;">with</span> <img alt="" src="<?= getAsset($member2['system'], $member2["id"], "heads") ?>" style="height: 24px; vertical-align: middle;"> <span style="vertical-align: middle;"><?= getMiniName($member2["display_name"] ?? $member2["name"]) ?><?php if (isset($tomorrow2[$index][1])): $member2 = getSystemMember($_PROFILE["login"] === "raindrops" ? "ynmuc" : "gdapd", $tomorrow2[$index][1]); ?>
                    and <img alt="" src="<?= getAsset($member2['system'], $member2["id"], "heads") ?>" style="height: 24px; vertical-align: middle;"> <?= getMiniName($member2["display_name"] ?? $member2["name"]) ?>
                    <?php endif; ?></span><?php endif; ?>
            </li>
            <?php endforeach; ?>
        </ul>

        <h5>Fronters on <?= date('l', time() + 172800) ?></h5>
        <ul>
            <?php foreach ($nextday1 as $index => $id): $member = getSystemMember($_PROFILE["login"] === "raindrops" ? "gdapd" : "ynmuc", $id[0]); ?>
                <li>
                    <?php if ($member["name"] === "fusion"): ?>
                        <img alt="" src="/assets/logo/logo.png" style="height: 24px; vertical-align: middle;"> <span style="vertical-align: middle;">Merged members</span>
                    <?php else: ?>
                        <img alt="" src="<?= getAsset($member['system'], $member["id"], "heads") ?>" style="height: 24px; vertical-align: middle;"> <b style="vertical-align: middle;"><?= getMiniName($member["display_name"] ?? $member["name"]) ?></b>
                    <?php endif; ?>
                    <?php if (isset($id[1])): $member2 = getMemberWithoutSystem($id[1]); ?>
                        <b style="vertical-align: middle;">and <img alt="" src="<?= getAsset($member['system'], $member2["id"], "heads") ?>" style="height: 24px; vertical-align: middle;"> <?= getMiniName($member2["display_name"] ?? $member2["name"]) ?></b>
                    <?php endif; ?>
                    <?php if (isset($nextday2[$index])): $member2 = getSystemMember($_PROFILE["login"] === "raindrops" ? "ynmuc" : "gdapd", $nextday2[$index][0]); ?><span style="vertical-align: middle;">with</span> <img alt="" src="<?= getAsset($member2['system'], $member2["id"], "heads") ?>" style="height: 24px; vertical-align: middle;"> <span style="vertical-align: middle;"><?= getMiniName($member2["display_name"] ?? $member2["name"]) ?><?php if (isset($nextday2[$index][1])): $member2 = getSystemMember($_PROFILE["login"] === "raindrops" ? "ynmuc" : "gdapd", $nextday2[$index][1]); ?>
                        and <img alt="" src="<?= getAsset($member2['system'], $member2["id"], "heads") ?>" style="height: 24px; vertical-align: middle;"> <?= getMiniName($member2["display_name"] ?? $member2["name"]) ?>
                    <?php endif; ?></span><?php endif; ?>
                </li>
            <?php endforeach; ?>
        </ul>

        <hr>
        <h4>Next events</h4>
        <?php

        $cal = new IcalParser();
        $results = $cal->parseFile($_SERVER['DOCUMENT_ROOT'] . "/includes/data/calendar/calendar.ics");
        $events = [];

        foreach ($cal->getEvents()->sorted() as $event) {
            $events[] = [
                "id" => $event['UID'],
                "date" => [
                    "created" => $event['CREATED']->format('c'),
                    "modified" => $event['LAST-MODIFIED']->format('c'),
                    "start" => $event['DTSTART']->format('c'),
                    "end" => $event['DTEND']->format('c'),
                    "duration" => strtotime($event['DTEND']->format('c')) - strtotime($event['DTSTART']->format('c')),
                    "full_day" => strtotime($event['DTEND']->format('c')) - strtotime($event['DTSTART']->format('c')) >= 86400,
                    "days" => (
                        strtotime($event['DTEND']->format('c')) - strtotime($event['DTSTART']->format('c')) >= 86400 ?
                        round((strtotime($event['DTEND']->format('c')) - strtotime($event['DTSTART']->format('c'))) / 86400) :
                        null
                    )
                ],
                "name" => $event['SUMMARY'],
                "description" => $event['DESCRIPTION'] ?? "",
            ];
        }

        $events = array_values(array_filter($events, function ($i) {
            return (
                strtotime($i["date"]["end"]) > time() &&
                strtotime($i["date"]["start"]) < time() + 2629800
            );
        }));

        ?>
        <ul>
            <?php foreach ($events as $event): ?>
            <li>
                <?php if ($event["date"]["full_day"]): ?>
                <b>
                    <?php if (strtotime($event["date"]["start"]) < time()): ?>
                    Started <?= timeAgo($event["date"]["start"]) ?>, ends <?= timeIn($event["date"]["end"]) ?>
                    <?php else: ?>
                    <?= ucfirst(relativeDate($event["date"]["start"], false)) ?>
                    <?php endif; ?>
                    ·
                </b>
                <?php else: ?>
                <b>
                    <?= ucfirst(relativeDate($event["date"]["start"])) ?>
                    ·
                </b>
                <?php endif; ?>
                for <?= duration($event["date"]["duration"]) ?>
                ·
                <?= $event["name"] ?>
            </li>
            <?php endforeach; ?>
        </ul>
    </div>
</div>

<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/footer.inc'; ?>