<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {

    $useSymbols=isset($_POST["use_symbols"]) && $_POST["use_symbols"] == "1";

    $tags = explode("\n", $_POST["tags"]);
    $tags = array_filter($tags, function ($tag) {
        return !empty($tag);
    });

    $tags=array_map(function($tag) use ($useSymbols){
        $tag=trim($tag);
        $tag=["text"=>$tag,"symbolPre"=>"","symbolPost"=>""];
        if($useSymbols){
            $tag['symbolPre']=substr($tag['text'],0,1);
            $tag['symbolPost']=substr($tag['text'],-1);
            $tag['text']=substr($tag['text'],1,-1);
        }
        return $tag;
    },$tags);

    if (empty($tags)) {
        header("Location: /?err=notags");
        exit();
    }

    $jobId = microtime(1) . "_" . uniqid();
    #file_put_contents()
    $queueDir = __DIR__ . "/../queue";
    if(!is_dir($queueDir)){
        mkdir($queueDir,0777,true);
    }
    $queueDir = realpath($queueDir);
    $queueFile = $queueDir . "/" . $jobId . ".json";
    file_put_contents($queueFile, json_encode($tags));
    header("Location: /?jobid=" . $jobId);
    exit();
}

if (isset($_GET["jobid"])) {
    $jobId = $_GET["jobid"];
    $queueDir = __DIR__ . "/../queue";
    $queueDir = realpath($queueDir);
    $queueFile = $queueDir . "/" . $jobId . ".json";

    $allJobs = glob($queueDir . "/*.json");
    sort($allJobs);
    $myPositionInQueue = array_search($queueFile, $allJobs);

    if (false === $myPositionInQueue) {
        if (isset($_GET["download"]) && "1" == $_GET["download"]) {
            $dlDir = __DIR__ . "/../html/dl";
            $dlDir = realpath($dlDir);
            $zipFile = $dlDir . "/" . $jobId . ".zip";
            header("Content-Type: application/zip");
            header(
                'Content-Disposition: attachment; filename="' .
                    basename($zipFile) .
                    '"'
            );
            readfile($zipFile);

            exit();
        } else {
            echo <<<EOT
<html>
<head>
    <title>c3RE AuerBoxTag Generator (Download starting)</title>
    <meta http-equiv="refresh" content="1; url='/?jobid=$jobId&download=1'"> 
</head>
<body>
<img src="/logo.png" alt="c3RE Logo" style="height: 200px "/>
Your job $jobId is Finished.

<a href="/">Back</a>
</body>
</html>
EOT;
        }
        header("HTTP/1.1 404 Not Found");
        exit();
    }

    $myPositionInQueue++;

    echo <<<EOT
<html>
<head>
    <title>c3RE AuerBoxTag Generator ($myPositionInQueue)</title>
    <meta http-equiv="refresh" content="1">
</head>
<body>
<img src="/logo.png" alt="c3RE Logo" style="height: 200px "/>
Your job $jobId is $myPositionInQueue in the queue.
</body>
</html>
EOT;
    exit();
}

$err = "";
switch ($_GET["err"]??"") {
    case "notags":
        $err = "Please provide at least one tag";
        break;
}
?>
<html>
<head>
    <title>c3RE AuerBoxTag Generator</title>
</head>
<body>
<img src="/logo.png" alt="c3RE Logo" style="height: 200px "/>
<h1>c3RE AuerBoxTag Generator</h1>
    <div style="color:red"><?php echo $err; ?></div>
<form method="post" action="/">
    <!--label title="Render the first and last Char in Symbola if its not alphanumeric"><input type="checkbox" name="use_symbols" value="1"> Use Symbols
            &#x24D8;
        </label-->
    <br>
    <textarea name="tags" rows="10" cols="30" placeholder="one tag per line please" data-placeholder-nosym="one tag per line please" data-placeholder-sym="♡|||Anna|||♥"></textarea>
    <br>
    <br>
    <input type="submit" value="Lets Go!">
</form>
</body>
</html>
