<?php
require_once 'functions/init.php';

// UI Components
/* foreach (glob('components/spaces/*.php') as $file) */
/* require_once $file; */

require_once 'components/core/head.php';

if (isset($_GET['view'])) {
    switch ($_GET['view']) {
        case 'essay':
            if (isset($_GET['id'])) {
                // TODO: Need another check here to verify the user is in the Essay's space.
                $essay = Essay::retrieveFromDB($_GET['id']);
                $space = $essay->getSpace();

                if (isset($_SESSION['user']) && $space->allowUser($_SESSION['user']))
                    echo displayEssay($essay);
                else if ($space->getVisibility() == 1)
                    echo displayEssay($essay);
                else
                    echo displaySpace(2025);
            } else
                echo displaySpace(2025);
            break;
        case 'show':
            // TODO: After implementing the notification modal, notify the user on success/fail auth events.
            if (isset($_GET['id'])) {
                $space = Space::retrieveFromDB($_GET['id']);
                if (isset($_SESSION['user']))
                    if ($space->allowUser(unserialize($_SESSION['user'])))
                        echo displaySpace($_GET['id']);
                    else if ($space->getVisibility() == 1)
                        echo displaySpace($_GET['id']);
                    else
                        echo 'TODO: Notification Modal -> User not a member';
                else {
                    if ($space->getVisibility() == 1)
                        echo displaySpace($_GET['id']);
                    else
                        echo 'TODO: Notification Modal -> Space is not Open';
                }
            } else
                echo displaySpaceList();
            break;
        case 'list':
            if (isset($_SESSION['user']))
                echo displaySpaceList();
            else
                echo displaySpace(2025);
            break;

        default:
            echo displaySpaceList();
            break;
    }
} else {
    echo displaySpace(2025);
}

if (isset($_SESSION['user']))
    echo actionButton();

require_once 'components/core/foot.php';

function actionButton()
{
    // TODO: Position these on the arc of a circle.
    $html = '
        <div class="circleLayout">
            <div id="newPost" class="actionButton" style="display: none;"><i class="nf nf-cod-new_file"></i></div>
            <div id="newSpace" class="actionButton" style="display: none;"><i class="nf nf-cod-new_folder"></i></div>
            <div id="filterSort" class="actionButton" style="display: none;"><i class="nf nf-oct-filter"></i></div>
            <div id="actionButton" class="actionButton"><i class="nf nf-cod-symbol_property"></i></div>
        </div>
    ';

    return $html;
}

function displayEssay(Essay $e)
{
    $html = '
        <div class="std_width std_border padding center top_margin" style="height: 87%; overflow-y: auto;">
    ' . $e->getHTML() . '
        </div>
    ';

    return $html;
}

function displaySpaceList()
{
    $user = unserialize($_SESSION['user']);

    $html = '
    <div class="std_width std_border padding center top_margin" style="height: 87%; overflow-y: auto;">
        <div class="column space_between full_width">
            <h1>Spaces</h4>
        </div>
        <div class="line"></div>
    ';

    $spaces = $user->getSpaces();
    foreach ($spaces as $s) {
        $html .= $s->getCardHTML();
    }

    $html .= '
    </div>
    ';

    return $html;
}

function displaySpace($id = 2025)
{
    $space = Space::retrieveFromDB($id);
    if ($space) {
        $html = '
        <div class="std_width std_border padding center top_margin" style="max-height: 89vh; overflow-y: auto;">
            <div class="column space_between full_width">
                <h1>' . $space->getName() . '</h4>
                <p>' . $space->getDescription() . '</p>
            </div>
            <div class="line"></div>
        ';

        // TODO: Need to investigate this bug in Posts
        $posts = $space->getPosts();
        foreach ($posts as $post) {
            foreach ($post as $p) {
                $html .= $p->getCardHTML();
            }
        }

        $html .= '
            </div>
        ';

        return $html;
    }
}
