<?php
define('DOC_START', microtime(true));
define('DOC_PATH', 'https://patchii.net/flashii/sharp-chat/raw/branch/mistress/Protocol.md');

function clean_section_name(string $name): string {
    return str_replace('`', '', trim($name, " \t\n\r\0\x0B#"));
}

function create_section_anchor(string $name): string {
    return preg_replace('#[^0-9a-zA-Z]#', '-', strtolower($name));
}

$validColourSchemes = ['Light' => '', 'Dark' => 'dark', 'Mio' => 'mio'];

if(isset($_GET['c']) && is_string($_GET['c']) && in_array($_GET['c'], $validColourSchemes)) {
    setcookie('scd_cs', $_GET['c'], strtotime('+1 year'), '/sockchat');
    header('Location: ./');
    exit;
}

$colorScheme = isset($_COOKIE['scd_cs']) && in_array($_COOKIE['scd_cs'] ?? null, $validColourSchemes) ? $_COOKIE['scd_cs'] : '';

require_once 'parsedown.php';

$docPath = DOC_PATH;

$mdLocalPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . md5($docPath);

if(is_file($mdLocalPath) && (filemtime($mdLocalPath) + 300) >= time()) {
    $protInfo = file_get_contents($mdLocalPath);
} else {
    $protInfo = file_get_contents($docPath);
    file_put_contents($mdLocalPath, $protInfo);
}

$protLines = explode("\n", $protInfo);
$sections = [];

foreach($protLines as $line) {
    if(empty($line))
        continue;
    
    if($line[0] === '#') {
        $depth = 0;
        
        for($i = 0;;$i++) {
            if($line[$i] === '#')
                $depth++;
            else
                break;
        }

        $name = clean_section_name($line, " \t\n\r\0\x0B#");

        if($depth > 1) {
            $anchor = create_section_anchor($name);
            $sections[] = [
                'name' => $name,
                'anchor' => $anchor,
                'depth' => $depth,
            ];
        }
        continue;
    }
}
?>
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Sock Chat Documentation</title>
        <link href="./assets/style.css" rel="stylesheet">
    </head>
    <body<?php if(!empty($colorScheme)) { echo ' class="' . $colorScheme . '"'; } ?>>
        <div class="navigation">
            <div class="navigation-inner">
                <a class="logo" href="#">Sock Chat Docs</a>
                <div class="options">
                <?php foreach($validColourSchemes as $name => $scheme): ?>
                <a href="?c=<?=$scheme;?>">[<?=$name;?>]</a> 
                <?php endforeach; ?>
                </div>
                <?php foreach($sections as $section): ?>
                <a href="#<?=$section['anchor'];?>" class="nav-link depth-<?=$section['depth'];?>">
                    <?=$section['name'];?>
                </a>
                <?php endforeach; ?>
            </div>
        </div>
        <div class="content">
            <div class="content-wrapper">
                <?=(new Parsedown)->text($protInfo);?>
            </div>
            <div class="attribs">
                <div class="attrib">Sock Chat was created by <a href="http://dev.aroltd.com">reemo</a></div>
                <div class="attrib">Markdown parsing by a <a href="./src.php?f=parsedown.php">modified</a> version of <a href="https://github.com/erusev/parsedown">Parsedown</a></div>
                <div class="attrib">Maintained by <a href="https://flash.moe">flashwave</a></div>
                <div class="attrib"><a href="./src.php?f=index.php">This page</a> is automatically generated using Protocol.md from the <a href="https://patchii.net/flashii/sharp-chat">Sharp Chat</a> repository.</div>
                <div class="attrib">Loaded in <?=number_format(microtime(true) - DOC_START, 5);?> seconds.</div>
            </div>
        </div>
    </body>
</html>