<?php   // Functions

    session_start();

    if (!isset($_SESSION['userID'])) {
        $_SESSION['userAccreditation'] = 0;
        $_SESSION['userLevel'] = 0;
    }

    if (!isset($_COOKIE['theme'])) {
        setcookie('theme', 0, time()+10*60*60);
        $darkTheme = $darkThemes[0];
        $lightTheme = $lightThemes[0];
    }

    if (isset($_GET['theme'])) {
        $newTheme = (int)$_GET['theme'] % 2;
        setcookie('theme', $newTheme);    // % 2 => Number of different themes
        $darkTheme = $darkThemes[$newTheme];
        $lightTHeme = $lightThemes[$newTheme];

    } else if (isset($_COOKIE['theme'])) {
        $theme = (int)$_COOKIE['theme'] % 2;
        $darkTheme = $darkThemes[$theme];
        $lightTheme = $lightThemes[$theme];
    }

    $status = "";

    function markdownContent($filePath, $rootFilePath) {
        $markdownContent = file_get_contents($filePath);
        require_once $rootFilePath . 'include/parsedown.php';
        $parsedown = new Parsedown();
        return $parsedown->text($markdownContent);
    }

    function fillHead($rootPageURL, $pageTitle, $darkTheme, $lightTheme) {

        echo ('<meta charset="UTF-8">');
        echo ('<meta name="viewport" content="width=device-width, initial-scale=1.0">');
        echo ('<link rel="stylesheet" href="' . $rootPageURL . 'src/css/style.css">');
        echo ('<link rel="icon" href="' . $rootPageURL . 'src/img/favicon.ico">');

        echo ('<style>');

        echo ('@media (prefers-color-scheme: dark) {');
        echo ('.body {');
        echo ($darkTheme);
        echo ('}');
        echo ('}');

        echo ('@media (prefers-color-scheme: light) {');
        echo ('.body {');
        echo ($lightTheme);
        echo ('}');
        echo ('}');

        echo ('</style>');

        echo ('<title>' . $pageTitle . '</title>');
    }

    function fillHeader($rootPageURL, $headerTitle, $headerSubtitle) {

        echo ('<div class="logo-container">');

        echo ('<a href="' . $rootPageURL . '">');
        echo ('<img src="' . $rootPageURL . 'src/img/athena-mono.png" class="logo-img">');
        echo ('</a>');

        echo ('</div>');

        echo ('<div class="content">');

        echo ('<div class="header-title-container">');
        echo ('<div class="header-title">' . $headerTitle . '</div>');
        echo ('<div class="header-subtitle">' . $headerSubtitle . '</div>');
        echo ('</div>');

        echo ('</div>');
    }

    function fillNav($rootPageURL) {

        echo ('<a href="' . $rootPageURL . 'news"><div class="nav-link">News</div></a>');
        echo ('<a href="' . $rootPageURL . 'about"><div class="nav-link">A propos</div></a>');

        /*if ($_SESSION['userLevel'] >= 3) {
            echo ('<a href="' . $rootPageURL . 'admin"><div class="nav-link">Admin</div></a>');
        }*/

        if(isset($_SESSION['userID'])) {
            echo ('<a href="' . $rootPageURL . 'account"><div class="nav-link">Profil</div></a>');
            echo ('<a href="' . $rootPageURL . 'settings"><div class="nav-link">Options</div></a>');
            echo ('<a href="' . $rootPageURL . 'login/logout.php"><div class="nav-link">Déconnexion</div></a>');
        } else {
            echo ('<a href="' . $rootPageURL . 'login"><div class="nav-link">Se connecter</div></a>');
        }
    }

    function fillFooter($footerText) {
        echo($footerText);
    }

    function sqlConnect($dbHost, $dbName, $dbUser, $dbPass) {
        try {
            $pdo = new PDO('mysql:host=' . $dbHost . ';dbname=' . $dbName . ';charset=UTF8mb4', $dbUser, $dbPass);
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        } catch(Exception $e) {
            require('error.php');
            die();
        }

        return $pdo;
    }

    function userWidget($userPPURL, $userDisplayName, $userName, $userLevel, $certificationColors, $rootPageURL)  {

        echo ('<a href="' . $rootPageURL . 'users?u=' . $userName . '">');
        echo ('<div class="user-widget">');

        echo ('<div class="user-pp">');
        echo ('<img src="' . $userPPURL . '">');
        echo ('</div>');

        echo ('<div class="user-info">');

        echo ('<div class="user-display-name">');
        echo ($userDisplayName);
        if ($userLevel > 1) {
            echo ('<svg class="certification" fill="' . $certificationColors[$userLevel] . '" viewBox="0 0 22 22" aria-label="' . $userLevels[$userLevel] . '" role="img">');
            echo ('<g><path d="M20.396 11c-.018-.646-.215-1.275-.57-1.816-.354-.54-.852-.972-1.438-1.246.223-.607.27-1.264.14-1.897-.131-.634-.437-1.218-.882-1.687-.47-.445-1.053-.75-1.687-.882-.633-.13-1.29-.083-1.897.14-.273-.587-.704-1.086-1.245-1.44S11.647 1.62 11 1.604c-.646.017-1.273.213-1.813.568s-.969.854-1.24 1.44c-.608-.223-1.267-.272-1.902-.14-.635.13-1.22.436-1.69.882-.445.47-.749 1.055-.878 1.688-.13.633-.08 1.29.144 1.896-.587.274-1.087.705-1.443 1.245-.356.54-.555 1.17-.574 1.817.02.647.218 1.276.574 1.817.356.54.856.972 1.443 1.245-.224.606-.274 1.263-.144 1.896.13.634.433 1.218.877 1.688.47.443 1.054.747 1.687.878.633.132 1.29.084 1.897-.136.274.586.705 1.084 1.246 1.439.54.354 1.17.551 1.816.569.647-.016 1.276-.213 1.817-.567s.972-.854 1.245-1.44c.604.239 1.266.296 1.903.164.636-.132 1.22-.447 1.68-.907.46-.46.776-1.044.908-1.681s.075-1.299-.165-1.903c.586-.274 1.084-.705 1.439-1.246.354-.54.551-1.17.569-1.816zM9.662 14.85l-3.429-3.428 1.293-1.302 2.072 2.072 4.4-4.794 1.347 1.246z"></path></g>');
            echo ('</svg>');
        }
        echo ('</div>');

        echo ('<div class="user-name">@' . $userName . '</div>');

        echo ('</div>');

        echo ('</div>');
        echo ('</a>');
    }

    function listArticles($result, $rootPageURL) {
        
        foreach($result as $article) {
            $pubDateTime = strtotime($article['date']);
            $pubDate = date('d/m/Y', $pubDateTime);
            $articleID = $article['ID'];
            $articleTitle = $article['title'];
            $articleResume = $article['resume'];
            $miniatureURL = empty($article['miniature']) ? $rootPageURL . "src/img/empty.jpg" : $article['miniature'];
            if(isset($article['username']) && isset($article['display_name'])) {
                $authorLink = ' | <a href="user.php?user=' . $article['username'] . '">' . $article['display_name'] . '</a>';
            } else {
                $authorLink = '';
            }

            echo('<div class="article-preview">');

            echo('<a href="'. $rootPageURL .'news?article=' . $articleID . '" class="article-link">');
            echo('<div class="article-illustration">');
            
            echo('<img src="' . $miniatureURL . '" class="article-miniature">');
            
            echo('</div>');
            echo('</a>');

            echo('<div class="article-info">');
            echo('<div class="article-data">n° ' . $articleID . ' | ' . $pubDate . '</div>');
            
            echo('<a href="'. $rootPageURL .'news?article=' . $articleID . '" class="article-link">');
            echo('<div class="article-title">' . $articleTitle . '</div>');
            echo('</a>');

            echo('<div class="article-resume">' . $articleResume . '</div>');

            echo('</div>');
            echo('</div> ');
        }
    }

    function textInput($type, $shapePath, $name, $placeholder, $value) {

        echo ('<div class="text-input">');
        echo ('<label for="' . $name . '">');
        echo ('<svg viewBox="0 0 24 24" aria-hidden="true" class="input-icon">');
        echo ('<g>' . $shapePath . '</g>');
        echo ('</svg>');
        echo ('</label>');
        echo ('<input type="' . $type . '" placeholder="' . $placeholder . '" name="' . $name . '" value="' . $value . '">');
        echo ('</div>');
    }

    function fileInput($name) {
        echo ('<div class="file-input">');
        echo ('<input type="file" name="' . $name . '">');
        echo ('</div>');
    }

    function selectInput($name, $label, $options, $defaultValue) {
        echo('<div class="select-input">');
        echo('<label for="' . $name . '">' . $label . '</label>');
        echo('<select name="' . $name .'" required>');

        foreach($options as $value => $name) { 
            $selectStatus = $value == $defaultValue ? "selected" : "";
            echo ('<option value="' . $value .'" ' . $selectStatus . '>' . $name . '</option>'); 
        }

        echo('</select>');
        echo('</div>');
    }

?>