<?php

if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/data/evening/ignored.json")) file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/evening/ignored.json", "[]");
if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/data/evening/locked.json")) file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/evening/locked.json", "{}");

if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/data/cache/evening.json")) unlink($_SERVER['DOCUMENT_ROOT'] . "/includes/data/cache/evening.json");

global $use2023UI;

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

if (isset($_GET["ignore"]) && isset($_GET["day"])) {
    if ($_GET["ignore"] === "1") {
        if (preg_match("/^\d{4}-\d{2}-\d{2}$/m", $_GET["day"]) === false) {
            header("Location: /$_GET[_]");
            die();
        }

        if (!in_array($_GET["day"], $ignored)) {
            $ignored[] = $_GET["day"];
        }

        if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/data/cache/evening.json")) unlink($_SERVER['DOCUMENT_ROOT'] . "/includes/data/cache/evening.json");

        file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/evening/ignored.json", json_encode($ignored));
        header("Location: /$_GET[_]");
        die();
    } elseif ($_GET["ignore"] === "0") {
        if (preg_match("/^\d{4}-\d{2}-\d{2}$/m", $_GET["day"]) === false) {
            header("Location: /$_GET[_]");
            die();
        }

        if (in_array($_GET["day"], $ignored)) {
            unset($ignored[array_search($_GET["day"], $ignored)]);
        }

        if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/data/cache/evening.json")) unlink($_SERVER['DOCUMENT_ROOT'] . "/includes/data/cache/evening.json");

        file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/evening/ignored.json", json_encode($ignored));
        header("Location: /$_GET[_]");
        die();
    }

    die();
}

if (isset($_GET["lock"]) && isset($_GET["day"]) && isset($_GET["data"])) {
    if (isset($locked[$_GET["day"]])) {
        unset($locked[$_GET["day"]]);

        if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/data/cache/evening.json")) unlink($_SERVER['DOCUMENT_ROOT'] . "/includes/data/cache/evening.json");

        file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/evening/locked.json", json_encode($locked));
        header("Location: /$_GET[_]");
        die();
    } else {
        $locked[$_GET["day"]] = $_GET["data"];

        if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/data/cache/evening.json")) unlink($_SERVER['DOCUMENT_ROOT'] . "/includes/data/cache/evening.json");

        file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/evening/locked.json", json_encode($locked));
        header("Location: /$_GET[_]");
        die();
    }
}