<?php global $app; global $_PROFILE; $front = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/gdapd/fronters.json"), true)["members"]; ?>

<h2>Sex alert
    <details style="display: inline-block;font-size:12px;">
        <summary class="text-muted" style="opacity:.5;"></summary>
        <label><input id="test-mode" type="checkbox"> Test Mode</label> · <label><input id="fake-requests" type="checkbox"> Fake Requests</label>
    </details>
</h2>

<?php if ($_PROFILE["login"] === "cloudburst"): ?>
<div class="alert alert-danger">
    <b>Notice:</b> Only <?= $app["other"]["name"] ?> can use the sex alert. If you wish to wake somepony up, use the <a href="/-/wakeup" style="filter: invert(1) hue-rotate(180deg);">wake-up alert</a> instead.
</div>
<?php endif; ?>

<?php if (isset($front[0])): ?>
    <?php if (!parseMetadata(json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/metadata/" . $front[0]["id"] . ".json"), true))["other"] && !parseMetadata(json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/metadata/" . $front[0]["id"] . ".json"), true))["other2"]): ?>
        <div class="alert alert-warning">
            <?php

            if (count($front) === 1) {
                echo("<b>" . ($front[0]["display_name"] ?? $front[0]["name"]) . "</b> is currently at front and you cannot have sex with this pony.");
            } elseif (count($front) === 2) {
                echo("<b>" . ($front[0]["display_name"] ?? $front[0]["name"]) . " and " . ($front[1]["display_name"] ?? $front[1]["name"]) . "</b> are currently at front and you cannot have sex with them.");
            } else {
                echo("Ponies are currently at front and you cannot have sex with them.");
            }

            ?> However, if you wish, any of the ponies mentioned on this page can take control of the body for the amount of time required for sex.
        </div>
    <?php elseif (!parseMetadata(json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/metadata/" . $front[0]["id"] . ".json"), true))["other"]): ?>
        <div class="alert alert-warning">
            <?php

            if (count($front) === 1) {
                echo("<b>" . ($front[0]["display_name"] ?? $front[0]["name"]) . "</b> is currently at front and may not want to have sex with you.");
            } elseif (count($front) === 2) {
                echo("<b>" . ($front[0]["display_name"] ?? $front[0]["name"]) . " and " . ($front[1]["display_name"] ?? $front[1]["name"]) . "</b> are currently at front and may not want to have sex with you.");
            } else {
                echo("Ponies are currently at front and may not want to have sex with you.");
            }

            ?> However, if this pony is not wanting to have sex with you, any of the ponies mentioned on this page can take control of the body for the amount of time required for sex.
        </div>
    <?php endif; ?>
<?php endif; ?>

<?php

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

if ((int)date('G') > 19) {
    $day = date('Y-m-d', time() + 86400);
} else {
    $day = date('Y-m-d');
}

if (isset($school[$day]) && $school[$day]["firstClass"]["timestamp"] < time() * 1000) {
    $day = date('Y-m-d', time() + 86400);
}

if (!isset($school[$day])) {
    echo('<div class="alert alert-secondary">No information about Raindrops\'s school schedule for ' . date('F jS', strtotime($day)) . ' currently, try again later.</div>');
} elseif (isset($school[$day]["firstClass"]["timestamp"])) {
    $tod = round($school[$day]["firstClass"]["timestamp"] / 1000) - strtotime($day);
    $hours = $tod / 3600;
    $time = date('g:i a', $tod);

    if ($hours < 8.5) {
        echo('<div class="alert alert-danger">Raindrops starts school at ' . $time . ' (local time) tomorrow (' . date('F jS', strtotime($day)) . '), it is recommended that you don\'t wake them up to have sex.</div>');
    } else if ($hours < 9.5) {
        echo('<div class="alert alert-warning">Raindrops starts school at ' . $time . ' (local time) tomorrow (' . date('F jS', strtotime($day)) . '), they can be wakened up at night to have sex for at most an hour.</div>');
    } else {
        echo('<div class="alert alert-success">Raindrops starts school at ' . $time . ' (local time) tomorrow (' . date('F jS', strtotime($day)) . '), they can be wakened up at night to have sex for over an hour.</div>');
    }
} else {
    echo('<div class="alert alert-success">Raindrops does not have school tomorrow (' . date('F jS', strtotime($day)) . '), they can be wakened up at night to have sex for as long as you wish.</div>');
}

?>

<span data-bs-toggle="modal" <?php if ($_PROFILE["login"] === "cloudburst"): ?>class="disabled"<?php endif; ?> data-bs-target="#turn-on" id="btn-on" style="background: #7f0000;font-size: 48px;padding: 10px 50px;border-radius: 10px;width: max-content;display: block;margin-left: auto;margin-right: auto;cursor: pointer;">Turn <b>ON</b></span>
<span onclick="disableAlert()" id="btn-off" style="display:none;background: #007f0b;font-size: 48px;padding: 10px 50px;border-radius: 10px;width: max-content;margin-left: auto;margin-right: auto;cursor: pointer;">Turn <b>OFF</b></span>
<p style="text-align:center;margin-top:10px;">Sending next notification <b><span id="next-notification">never</span></b> · Local time for Raindrops: <span id="raindrops-time">--:--</span><br>Make sure you run the <code>/sex</code> command on the server before turning the alert on.</p>

<script>
    function updateTime() {
        let time = (new Intl.DateTimeFormat("en-US", {
            timeZone: 'Europe/Paris',
            hour: 'numeric',
            minute: 'numeric',
            hour12: true
        })).format(new Date());

        document.getElementById("raindrops-time").innerText = time;
    }

    updateTime();

    setInterval(() => {
        updateTime();
    }, 10000);
</script>

<style>
    .modal-header {
        border-bottom: 1px solid #353738;
    }

    .modal-content {
        border: 1px solid rgba(255, 255, 255, .2);
        background-color: #111;
    }

    .btn-close {
        filter: invert(1);
    }

    #btn-on.disabled, #btn-off.disabled {
        opacity: .5;
        pointer-events: none;
    }
</style>

<div class="modal" id="turn-on">
    <div class="modal-dialog">
        <div class="modal-content">

            <div class="modal-header">
                <h4 class="modal-title">Somepony will be awoken.</h4>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>

            <div class="modal-body">
                <button onclick="enableAlert();" data-bs-dismiss="modal" class="btn btn-success" style="font-size:20px;font-weight:bold;display:block;width:100%;">I wish to proceed.</button>

                <hr>

                <div class="alert alert-warning">
                    This alert system is designed to emit sudden alerts, and may surprise somepony if e.g. they are sleeping. Keep that in mind.
                </div>

                <p>
                    <b>Disclaimer:</b> This is NOT an emergency alert system, use the "Emergency alert" option is you need immediate help and comfort.
                </p>
                <p>
                    © <?= date('Y') ?> Equestria.dev
                </p>
            </div>
        </div>
    </div>
</div>

<script>
    window.alertInterval = null;
    window.alertIntervalAmount = 5;
    window.alertIntervalCounter = window.alertIntervalAmount;

    function sendNotification() {
        window.alertIntervalCounter = -1;

        if (document.getElementById("test-mode").checked) {
            document.getElementById("next-notification").innerText = "now";
            if (document.getElementById("fake-requests").checked) {
                window.alertIntervalCounter = window.alertIntervalAmount;
                document.getElementById("next-notification").innerText = "in " + window.alertIntervalAmount + " seconds";
            } else {
                window.fetch("/api/pleasure").then(() => {
                    window.alertIntervalCounter = window.alertIntervalAmount;
                    document.getElementById("next-notification").innerText = "in " + window.alertIntervalAmount + " seconds";
                })
            }
        } else {
            document.getElementById("next-notification").innerText = "now";
            if (document.getElementById("fake-requests").checked) {
                window.alertIntervalCounter = window.alertIntervalAmount;
                document.getElementById("next-notification").innerText = "in " + window.alertIntervalAmount + " seconds";
            } else {
                window.fetch("/api/pleasure-real").then(() => {
                    window.alertIntervalCounter = window.alertIntervalAmount;
                    document.getElementById("next-notification").innerText = "in " + window.alertIntervalAmount + " seconds";
                })
            }
        }
    }

    function enableAlert() {
        sendNotification();
        document.getElementById("btn-on").style.display = "none";
        document.getElementById("btn-off").style.display = "block";
        document.getElementById("test-mode").disabled = true;
        document.getElementById("fake-requests").disabled = true;

        window.alertInterval = setInterval(() => {
            window.alertIntervalCounter--;

            if (window.alertIntervalCounter === 0) {
                sendNotification();
            } else if (window.alertIntervalCounter > -1) {
                document.getElementById("next-notification").innerText = "in " + window.alertIntervalCounter + " second" + (window.alertIntervalCounter > 1 ? "s" : "");
            }
        }, 1000);
    }

    function disableAlert() {
        clearInterval(window.alertInterval);
        document.getElementById("next-notification").innerText = "never";
        document.getElementById("btn-on").style.display = "block";
        document.getElementById("btn-off").style.display = "none";
        document.getElementById("test-mode").disabled = false;
        document.getElementById("fake-requests").disabled = false;
    }
</script>

<?php if ($_PROFILE["login"] !== "cloudburst"): ?>
<hr>

<p><?= $_PROFILE["login"] === "raindrops" ? "These ponies will have sex with " . $app["other"]["name"] : "These ponies will have sex with you" ?>:</p>
<ul>
    <?php foreach (scoreOrderGlobal() as $pony): if ($pony["system"] === "gdapd" && isset($pony["_metadata"]["other"]) && $pony["_metadata"]["other"]): ?>
        <li><a href="/<?= $pony["name"] ?>"><?= $pony["display_name"] ?? $pony["name"] ?></a> (<?= $pony["_metadata"]["sexually_active"] ? "no consent is required" : "consent is mandatory" ?>)</li>
    <?php endif; endforeach; ?>
</ul>

<p><?= $_PROFILE["login"] === "raindrops" ? "These ponies may have sex with " . $app["other"]["name"] . " but are unsure about it" : "These ponies may have sex with you but are unsure about it" ?>:</p>
<ul>
    <?php foreach (scoreOrderGlobal() as $pony): if ($pony["system"] === "gdapd" && isset($pony["_metadata"]["other2"]) && $pony["_metadata"]["other2"]): ?>
        <li><a href="/<?= $pony["name"] ?>"><?= $pony["display_name"] ?? $pony["name"] ?></a></li>
    <?php endif; endforeach; ?>
</ul>

<p><?= $_PROFILE["login"] === "raindrops" ? "These ponies will have sex with you" : "These ponies will have sex with Raindrops" ?>:</p>
<ul>
    <?php foreach (scoreOrderGlobal() as $pony): if ($pony["system"] === $app["other"]["id"] && isset($pony["_metadata"]["other3"]) && $pony["_metadata"]["other3"]): ?>
        <li><a href="/<?= $pony["name"] ?>"><?= $pony["display_name"] ?? $pony["name"] ?></a></li>
    <?php endif; endforeach; ?>
</ul>

<p><?= $_PROFILE["login"] === "raindrops" ? "These ponies may have sex with you but are unsure about it" : "These ponies may have sex with Raindrops but are unsure about it" ?>:</p>
<ul>
    <?php foreach (scoreOrderGlobal() as $pony): if ($pony["system"] === $app["other"]["id"] && isset($pony["_metadata"]["other4"]) && $pony["_metadata"]["other4"]): ?>
        <li><a href="/<?= $pony["name"] ?>"><?= $pony["display_name"] ?? $pony["name"] ?></a></li>
    <?php endif; endforeach; ?>
</ul>
<?php endif; ?>