<!DOCTYPE html>

<?php
require_once("classes/SubjectData.php");
require_once("classes/TopicData.php");
require_once("classes/User.php");

session_start();

if (!isset($_GET["subject"])) {
    die("Ungültige Seite");
}
$subjectData = SubjectData::fromName($_GET["subject"]);
if (!isset($subjectData)) {
    die("Ungültige Seite");
}
if (!isset($_GET["topic"])) {
    die("Ungültige Seite");
}
$topicData = TopicData::fromName($_GET["subject"], $_GET["topic"]);
if (!isset($topicData)) {
    die("Ungültige Seite");
}

?>

<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo($topicData->displayName); ?> - <?php echo($subjectData->displayName); ?></title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="assets/css/topic.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/gsap.min.js"></script>
    <script src="https://cdn.tailwindcss.com"></script>
    <!--<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>-->
    <script src="assets/js/sidebar.js"></script>
    <script src="assets/js/tasks.js" defer></script>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.18/dist/katex.min.css"
          integrity="sha384-veTAhWILPOotXm+kbR5uY7dRamYLJf58I7P+hJhjeuc7hsMAkJHTsPahAl0hBST0" crossorigin="anonymous">
    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.18/dist/katex.min.js"
            integrity="sha384-v6mkHYHfY/4BWq54f7lQAdtIsoZZIByznQ3ZqN38OL4KCsrxo31SLlPiak7cj/Mg"
            crossorigin="anonymous"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.18/dist/contrib/auto-render.min.js"
            integrity="sha384-hCXGrW6PitJEwbkoStFjeJxv+fSOOQKOPbJxSfM6G5sWZjAyWhXiTIIAmQqnlLlh" crossorigin="anonymous"
            onload="renderMathInElement(document.body);"></script>

    <script>
        document.addEventListener("DOMContentLoaded", function () {
            renderMathInElement(document.body, {
                // customised options
                // • auto-render specific keys, e.g.:
                delimiters: [
                    {left: "$$", right: "$$", display: true},
                    {left: "\\(", right: "\\)", display: false},
                    {left: "\\begin{equation}", right: "\\end{equation}", display: true},
                    {left: "\\begin{array}", right: "\\end{array}", display: true},
                    {left: "\\begin{align}", right: "\\end{align}", display: true},
                    {left: "\\begin{alignat}", right: "\\end{alignat}", display: true},
                    {left: "\\begin{gather}", right: "\\end{gather}", display: true},
                    {left: "\\begin{CD}", right: "\\end{CD}", display: true},
                    {left: "\\[", right: "\\]", display: true}
                ],
                // • rendering keys, e.g.:
                throwOnError: false
            });
        });
    </script>
</head>


<body class="min-h-screen">
<?php include 'header.php'; ?>

<!-- Left Sidebar -->
<nav class="sidebar bg-[<?php echo($subjectData->color); ?>] pt-24">
    <div class="sidebar-header">
        <i class="fas <?php echo($subjectData->icon); ?>"></i>
        <h2><?php echo($subjectData->displayName); ?></h2>
    </div>
    <a href="index.php" class="nav-link">
        <i class="fas fa-home"></i> Startseite
    </a>
    <a href="subject.php?subject=<?php echo($subjectData->id); ?>" class="nav-link">
        <i class="fas fa-book"></i> <?php echo($subjectData->displayName); ?> Übersicht
    </a>
    <a href="#" class="nav-link active">
        <i class="fas <?php echo($topicData->icon); ?>"></i> <?php echo($topicData->displayName); ?>
    </a>
</nav>

<!-- Main Content -->
<main class="main-content">
    <div class="max-w-7xl mx-auto mt-5">
        <div class="mt-16"></div>

        <!-- Related Topics -->
        <div class="related-topics bg-gray-100 p-4 rounded-lg">
            <h4>Verwandte Themen:</h4>
            <ul class="flex flex-wrap gap-2">
                <?php
                foreach ($topicData->relatedTopics as $relatedTopicName) {
                    $relatedTopic = $subjectData->topics[$relatedTopicName];
                    if (!isset($relatedTopic)) {
                        continue;
                    }
                    ?>
                    <li onclick="event.stopPropagation();"
                        class="border-[<?php echo($subjectData->color); ?>] border-2">
                        <a href='<?php echo("topic.php?subject=$subjectData->id&topic=$relatedTopic->id") ?>'
                           class="block">
                            <?php echo($relatedTopic->displayName); ?>
                        </a>
                    </li>
                    <?php
                }
                ?>
            </ul>
        </div>

        <!-- Topic Content -->
        <div class="content-card mt-[8px]">
            <h1 class="content-title"><?php echo($topicData->displayName); ?></h1>
            <p class="content-text">
                <?php echo($topicData->description); ?>
            </p>
            <div class="content-text article-section">
                <?php echo($topicData->getFinishedArticle()); ?>
            </div>

            <div class="exercise-section bg-gray-100">
                <h3 style="margin-bottom: 1rem;" class="text-[var(--primary-color)]">Übungen herunterladen:</h3>
                <div style="display: flex; flex-wrap: wrap; gap: 0.5rem;">
                    <?php
                    foreach ($topicData->files as $fileName) {
                        ?>

                        <a href='<?php echo("config/subjects/$subjectData->id/topics/$topicData->id/downloads/$fileName") ?>'
                           target="_blank" download
                           class="download-btn border-[<?php echo($subjectData->color); ?>] border-2">
                            <i class="fas fa-file-pdf"></i>
                            <?php echo($fileName); ?>
                        </a>

                        <?php
                    }
                    ?>
                </div>
            </div>
        </div>

        <!-- Tasks -->
        <?php
        $tasks = $topicData->getTasks();
        // nur anzeigen, wenn Aufgaben vorhanden sind
        if ($tasks != null) {
            ?>
            <div class="content-card mt-[8px]">
                <h1 class="content-title">Aufgaben</h1>
                <div style="display: flex; flex-wrap: wrap; gap: 0.5rem;">
                    <?php
                    foreach ($tasks as $taskIndex => $task) {
                        $taskId = $taskIndex;
                        ?>
                        <div class="task-container border-2 border-[var(--primary-color)] p-4 rounded-xl"
                             data-task-id="<?php echo htmlspecialchars($taskId, ENT_QUOTES, 'UTF-8'); ?>">
                            <p>
                                <?php echo '<b>Aufgabe ' . ($taskIndex + 1) . ':</b> </br>' . htmlspecialchars($task->getText(), ENT_QUOTES, 'UTF-8') . '</br></br>'; ?>
                            </p>
                            <!-- Diesen Bereich für jeden Eintrag in Variables anzeigen lassen, z.B. x = [Textfeld] ... -->
                            <?php
                            $variables = $task->getVariables();
                            foreach ($variables as $variableIndex => $variable) {
                                $correctAnswer = $variable;
                                ?>
                                <div class="variable-container" style="display: flex; flex-wrap: wrap; gap: 0.5rem; ">
                                    <label for="answer<?php echo $taskId . '_' . $variableIndex; ?>"></label>
                                    <input class="variable-input border-2 border-[var(--primary-color)]" id="answer<?php echo $taskId . '_' . $variableIndex; ?>" type="text"
                                           placeholder="<?php echo $variableIndex; ?>" data-correct-answer="<?php echo $correctAnswer; ?>"/>
                                    <button class="check-answer bg-white text-[var(--primary-color)] border-2 border-[var(--primary-color)] w-10 h-10 flex items-center justify-center rounded-lg hover:bg-[var(--primary-color)] hover:text-white transition duration-300">
                                        <i class="fa-solid fa-check"></i>
                                    </button>
                                    <button class="show-answer bg-white text-[var(--primary-color)] border-2 border-[var(--primary-color)] w-10 h-10 flex items-center justify-center rounded-lg hover:bg-[var(--primary-color)] hover:text-white transition duration-300">
                                        <i class="fa-solid fa-eye"></i>
                                    </button>
                                </div>
                                <?php
                            }
                            ?>

                        </div>
                        <hr>
                        <?php
                    }
                    ?>
                </div>
            </div>
            <!-- Toasts Container -->
            <div id="toasts" class="fixed top-5 right-5 flex flex-col items-end space-y-4 z-50">
                <!-- Einzelne Toasts werden hier dynamisch hinzugefügt -->
            </div>

            <?php
        }
        ?>


        <!-- Related Topics -->
        <div class="related-topics bg-gray-100 p-4 rounded-lg">
            <h4>Verwandte Themen:</h4>
            <ul class="flex flex-wrap gap-2">
                <?php
                foreach ($topicData->relatedTopics as $relatedTopicName) {
                    $relatedTopic = $subjectData->topics[$relatedTopicName];
                    if (!isset($relatedTopic)) {
                        continue;
                    }
                    ?>
                    <li onclick="event.stopPropagation();"
                        class="border-[<?php echo($subjectData->color); ?>] border-2">
                        <a href='<?php echo("topic.php?subject=$subjectData->id&topic=$relatedTopic->id") ?>'
                           class="block">
                            <?php echo($relatedTopic->displayName); ?>
                        </a>
                    </li>
                    <?php
                }
                ?>
            </ul>
        </div>
    </div>
</main>

<?php
if (isset($_SESSION['user']) && $_SESSION['user']->isLoggedIn()) {
    ?>
    <a href="topicEditor.php?subject=<?php echo $subjectData->id ?>&topic=<?php echo $topicData->id ?>"
       class="fixed z-90 w-14 h-14 bottom-20 right-5 flex items-center justify-center text-4xl text-white rounded-lg bg-[var(--primary-color)] hover:bg-[var(--accent-color)] transition duration-300">
        <span class="pb-1.5">&#9998;</span>
    </a>
    <?php
}
?>

<footer class="sticky top-[100vh] lg:ms-[280px] w-full lg:w-auto bg-white/80 backdrop-blur-lg shadow-sm p-5">
    <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
        <div class="flex justify-between">
            <p class="text-gray-600">&copy; Horst-Schlämmer-Gedächtnis-Gymnasium</p>
            <p><a href="impressum.php"
                  class="text-[var(--primary-color)] hover:text-[var(--accent-color)]">Impressum</a></p>
        </div>
    </div>
</footer>

</body>
</html>
