<?php

$pageStyles[] = 'app/css/home.css';
$pageStyles[] = 'app/css/viewer/shared.css';
$pageStyles[] = 'app/css/viewer/dashboard.css';
$pageStyles[] = 'app/css/viewer/project.css';
$pageStyles[] = 'app/css/viewer/task_list.css';
$pageStyles[] = 'app/css/viewer/task.css';
$pageStyles[] = 'app/css/viewer/version.css';
$pageStyles[] = 'app/css/viewer/profile.css';
$pageStyles[] = 'app/css/viewer/admin.css';
$pageStyles[] = 'app/css/popups.css';

$pageScripts[] = 'https://unpkg.com/cytoscape@3.34.0/dist/cytoscape.min.js';
$pageScripts[] = 'app/js/home/core.js';
$pageScripts[] = 'app/js/home/dashboard.js';
$pageScripts[] = 'app/js/home/project.js';
$pageScripts[] = 'app/js/home/task_list.js';
$pageScripts[] = 'app/js/home/task.js';
$pageScripts[] = 'app/js/home/version.js';
$pageScripts[] = 'app/js/home/profile.js';
$pageScripts[] = 'app/js/home/admin.js';
$pageScripts[] = 'app/js/home/router.js';
$pageScripts[] = 'app/js/home/events.js';
$pageScripts[] = 'app/js/popups.js';

$user = requireLogin();
$userPicture = null;
$isAdmin = userIsAdmin((int)$user['id']);
$canCreateProjects = $isAdmin || userHasRight((int)$user['id'], 'Create Projects');
$canCreateTasks = $isAdmin || userHasRight((int)$user['id'], 'Create Tasks');
$canCreateVersions = $isAdmin || userHasRight((int)$user['id'], 'Create Versions');
$canEditProjects = $isAdmin || userHasRight((int)$user['id'], 'Edit Projects');
$canEditTasks = $isAdmin || userHasRight((int)$user['id'], 'Edit Tasks');
$canEditVersions = $isAdmin || userHasRight((int)$user['id'], 'Edit Versions');

if ($user['picture'] !== null) {
    $pictureInfo = @getimagesizefromstring($user['picture']);
    $pictureMime = is_array($pictureInfo) && isset($pictureInfo['mime'])
        ? $pictureInfo['mime']
        : 'image/png';
    $userPicture = 'data:' . $pictureMime . ';base64,' . base64_encode($user['picture']);
}
?>

<div
    class="kiln-app"
    data-current-user-id="<?= (int)$user['id'] ?>"
    data-is-admin="<?= $isAdmin ? '1' : '0' ?>"
    data-can-create-versions="<?= $canCreateVersions ? '1' : '0' ?>"
    data-can-edit-projects="<?= $canEditProjects ? '1' : '0' ?>"
    data-can-edit-tasks="<?= $canEditTasks ? '1' : '0' ?>"
    data-can-edit-versions="<?= $canEditVersions ? '1' : '0' ?>"
>
    <header class="kiln-topbar">

        <div class="kiln-topbar-left">
            <div class="kiln-brand">
                <span class="kiln-brand-mark">PK</span>
                <span>ProjectKiln</span>
            </div>
        </div>

        <div class="kiln-topbar-center">
            <button class="kiln-top-btn" data-view="dashboard">
                <i class="fa-solid fa-chart-line me-1"></i>
                Dashboard
            </button>

            <?php if ($canCreateProjects): ?>
                <button class="kiln-top-btn" data-popup-open="createProject">
                    <i class="fa-solid fa-plus me-1"></i>
                    Project
                </button>
            <?php endif; ?>

            <?php if ($canCreateTasks): ?>
                <button class="kiln-top-btn" data-popup-open="createTask">
                    <i class="fa-solid fa-plus me-1"></i>
                    New Task
                </button>
            <?php endif; ?>

            <?php if ($isAdmin): ?>
                <button class="kiln-top-btn" id="adminMenuButton">
                    <i class="fa-solid fa-screwdriver-wrench me-1"></i>
                    Admin
                </button>
            <?php endif; ?>
        </div>

        <div class="kiln-topbar-right dropdown">
            <button
                class="kiln-account-btn dropdown-toggle"
                type="button"
                data-bs-toggle="dropdown"
            >
                <?php if ($userPicture): ?>
                    <img src="<?= htmlspecialchars($userPicture) ?>" alt="" class="user-avatar" id="accountAvatarImage">
                <?php else: ?>
                    <span class="user-avatar user-avatar-fallback" id="accountAvatarFallback">
                        <i class="fa-solid fa-user"></i>
                    </span>
                <?php endif; ?>
                <span id="accountName"><?= htmlspecialchars($user['name']) ?></span>
            </button>

            <ul class="dropdown-menu dropdown-menu-end">
                <li>
                    <span class="dropdown-item-text small text-secondary">
                        <span id="accountEmail"><?= htmlspecialchars($user['email']) ?></span>
                    </span>
                </li>

                <li><hr class="dropdown-divider"></li>

                <li>
                    <button class="dropdown-item" type="button" id="profileMenuButton">
                        <i class="fa-solid fa-user-pen me-2"></i>
                        Edit Profile
                    </button>
                </li>

                <li><hr class="dropdown-divider"></li>

                <li>
                    <form method="post" action="?page=logout">
                        <button class="dropdown-item text-danger" type="submit">
                            <i class="fa-solid fa-right-from-bracket me-2"></i>
                            Sign Out
                        </button>
                    </form>
                </li>
            </ul>
        </div>

    </header>

    <div class="kiln-body">

        <aside class="kiln-sidebar" id="sidebar">

            <?php if ($canCreateProjects): ?>
                <button class="kiln-sidebar-action" data-popup-open="createProject">
                    <i class="fa-solid fa-plus me-2"></i>
                    Create new project
                </button>
            <?php endif; ?>

            <nav class="kiln-tree" id="projectTree">
                <div class="kiln-tree-muted">
                    Loading projects...
                </div>
            </nav>

        </aside>

        <main class="kiln-viewer" id="viewer">
            <?php require __DIR__ . '/viewer/dashboard.php'; ?>
            <?php require __DIR__ . '/viewer/project.php'; ?>
            <?php require __DIR__ . '/viewer/task_list.php'; ?>
            <?php require __DIR__ . '/viewer/task.php'; ?>
            <?php require __DIR__ . '/viewer/version.php'; ?>
            <?php require __DIR__ . '/viewer/profile.php'; ?>
            <?php require __DIR__ . '/viewer/admin.php'; ?>
        </main>

    </div>
</div>

<?php require __DIR__ . '/popups.php'; ?>
