<?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 . '" class="user-widget-link">');
        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 class="cls-1" d="M959.75,457.79l-37.33-41.28c-13.58-15.01-19.1-35.64-14.85-55.43l11.69-54.41c6.79-31.59-11.46-63.19-42.21-73.11l-52.97-17.08c-19.26-6.21-34.36-21.31-40.57-40.57l-17.08-52.97c-9.92-30.75-41.51-49-73.11-42.21l-54.41,11.69c-19.79,4.25-40.41-1.27-55.43-14.85l-41.28-37.33c-23.96-21.67-60.45-21.67-84.42,0l-41.28,37.33c-15.01,13.58-35.64,19.1-55.43,14.85l-54.41-11.69c-31.59-6.79-63.19,11.46-73.11,42.21l-17.08,52.97c-6.21,19.26-21.31,34.36-40.57,40.57l-52.97,17.08c-30.75,9.92-49,41.51-42.21,73.11l11.69,54.41c4.25,19.79-1.27,40.41-14.85,55.43l-37.33,41.28c-21.67,23.97-21.67,60.45,0,84.42l37.33,41.28c13.58,15.01,19.1,35.64,14.85,55.43l-11.69,54.41c-6.79,31.59,11.46,63.19,42.21,73.11l52.97,17.08c19.26,6.21,34.36,21.31,40.57,40.57l17.08,52.97c9.92,30.75,41.51,49,73.11,42.21l54.41-11.69c19.79-4.25,40.41,1.28,55.43,14.85l41.28,37.33c23.97,21.67,60.45,21.67,84.42,0l41.28-37.33c15.01-13.58,35.64-19.1,55.43-14.85l54.41,11.69c31.59,6.79,63.19-11.46,73.11-42.21l17.08-52.97c6.21-19.26,21.31-34.36,40.57-40.57l52.97-17.08c30.75-9.92,49-41.51,42.21-73.11l-11.69-54.41c-4.25-19.79,1.28-40.41,14.85-55.43l37.33-41.28c21.67-23.96,21.67-60.45,0-84.42ZM749.62,426.54l-288.48,288.48c-15.74,15.74-41.25,15.74-56.99,0l-153.77-153.77c-20.45-20.45-20.45-53.62,0-74.07,20.45-20.45,53.62-20.45,74.07,0l108.19,108.19,242.91-242.91c20.46-20.45,53.62-20.45,74.07,0,20.45,20.45,20.45,53.62,0,74.07Z"/></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>');
    }

?>