<?php
require_once('functions.php');
require_once('config.php');
?>

<html>
    <head>
        <?php
            $stylesheets = [ "style.css", "bad.css"];
            if ( isset($_GET['style']) ) {
                $style_index = $_GET['style'];
                echo '<link rel="stylesheet" href="' . $stylesheets[$style_index] . '">';
            } else {
                $randomelement =  rand(0, sizeof($stylesheets)-1);
                echo '<link rel="stylesheet" href="' . $stylesheets[$randomelement] . '">';
            }
        ?>
        <title>Werde Cyber-Therapeut</title>
    </head>
    <body>
        <?php
        $fragenkatalog = json_decode(file_get_contents($files['questions']), true);
        /* 
        ######################
        # Print Questionaire #
        ######################
        */
        if (empty($_POST)) {    
        ?>
            <h1 class='blink'>Werde Cyber-Therapeut</h1>
            <form class='content' name="qualification" method="post" action="">
             <img class='bad' src="images/img-not-found-transparent.png" alt="Bild nicht gefunden (absichtlich hier platziert)."><br />
                <input type='text' name="nickname" value="Nickname">
                <?php
                foreach ($fragenkatalog as $kategorie => $fragen) {
                    echo "<fieldset class='category'><label class='categorylabel content testform'>{$kategorie}</label>";
                    foreach ($fragen as $frage => $antworten) {
                        echo "<fieldset class='question'><label class='question testform'>{$frage}</label><br />";
                        foreach ($antworten as $antwort) {
                            echo "<input type='radio' value='{$antwort[0]}' id='question[{$kategorie}][{$frage}][{$antwort[1]}]' name='question[{$kategorie}][{$frage}]'><label class='answer' for='question[{$kategorie}][{$frage}][{$antwort[1]}]'>{$antwort[1]}</label><br />";
                        }
                        echo "</fieldset>";
                    }
                    echo "</fieldset>";
                }
                ?>
                <input type="Submit" value="Finish" />
                <img class='bad' src="images/hit-counter.png" alt="wirklich nutzloser Besuchercounter der immer 07284527 anzeigt.">
            </form>
   
        <?php
        /* 
        #####################
        # Calculate Results #
        #####################
        */
        } else {
            
            $score = array();
            $max_score = array();
            $score_text = array();
            $cheater = false;
            foreach ($fragenkatalog as $kategorie => $fragen) {
                $max_score[$kategorie] = get_max_sum_for_category($fragen);
                if ($_POST['question'][$kategorie]) {
                    $score[$kategorie] = array_sum($_POST['question'][$kategorie]);
                } else {
                    $score[$kategorie] = 0;
                }
                $score_text[$kategorie] = implode("/", [$score[$kategorie], $max_score[$kategorie]]);
                if ($score[$kategorie] > $max_score[$kategorie]) {
                    $cheater = true;
                }
            }
            $total_score = array_sum($score);
            $total_max_score = array_sum($max_score);
            $level = calculate_level($total_score, $total_max_score);
            $nickname = $_POST['nickname'];
            if ($cheater) {
              $nickname = "Cheater! ${nickname}";
            }
            $certificate_data = array(
                'nickname' => $nickname, 
                'score'    => "{$total_score}/{$total_max_score}", 
                'level'    => $level['title'],
                'details'  => $score_text
            );
            $encrypted_data = encrypt_n_sign($certificate_data);
            $query = "certificate=" . urlencode($encrypted_data['message']);
            $image_url = "certificate.php?". $query;
            $image_alt_text = "Certificate for {$level['title']} {$_POST['nickname']}";
            /* 
            #################
            # Print Results #
            #################
            */
            ?>
            <div class="content">
                <h1>Herzlichen Glückwunsch <?= $level['title'] ?> <?= e($_POST['nickname']) ?></h1>
                <p>Du hast <b><?= $total_score ?></b> von <?= $total_max_score ?> möglichen Punkten erreicht.</p>
                <p><?= $level['comment'] ?></p>
                <div id="certificate">
                  <a href='<?= $image_url ?>' target="_new">
                    <img src='<?= $image_url ?>' alt='<?= $image_alt_text ?>' width=800 ><br />
                    Zertifikat herunterladen
                  </a>
                </div>

                <h3>Aufschlüsselung der Punkte nach Kategorien</h3>
                <?php foreach ($score as $category => $value): ?>
                Kategorie <b><?= $category ?></b>: <?= $value ?>/<?= $max_score[$category] ?><br />
                <?php endforeach; ?>
            </div>
        <?php } ?>
    
    </body>
