<?php
// Use Master Page
include_once "master.php";
$master = new MasterPage;
$master->addLocalScript("js/like_entry.js");
$master->addLocalScript("js/map_explore.js");
$master->addLocalStylesheet("css/explore.css");
$master->show_explore_menu = true;

// Get DB connection
require_once "./library/database.php";
$connect = Database::getInstance();

// Get AtlasDetails for settings
$atlasDetails = $connect->getAtlasDetails();

if($atlasDetails->enablePreLaunchFeatures == 1)
{
    $master->addLocalScript("js/countdown.js");
}

// Load currentMemory if linking directly to one
$currentMemory = null;

if(isset($_GET['entry']))
{
    $entryCode = (string)$_GET["entry"];
    $currentMemory = $connect->getMemoryByEntryCode($entryCode);
}

$pageTitle = "Explore " . $master->days_helper->selected_day->getDateForDisplay();

if($currentMemory != null)
{
    // Update social media sharing tags for current mememory.
    $master->addMemorySharingTags($currentMemory);
    // Update page title with Title of current Memory.
    $pageTitle = $currentMemory->getTitleForDisplay(); 
    // Overwrite selected day with the day of the currentMemory.
    $master->days_helper->setSelectedDayById($currentMemory->day_id);
}

$isLikedTrue = false;

if(isset($_GET['liked']))
{
    $isLikedTrue = (string)$_GET["liked"] == "true";
}

$isAudioAutoplayOn = UserDetails::getIsAudioAutoplayOn();

echo $master->renderTop($pageTitle);
?>

<input type="hidden" id="HiddenAutoplay" value="<?= $isAudioAutoplayOn ? "true" : "false" ?>" />
<input type="hidden" id="HiddenMapboxPublicToken" value="<?= $atlasDetails->mapboxPublicToken ?>" />

<div class="map-wrapper">
    <div id="map"></div>

    <div class="memory-wrapper">
        <div class="panel-header memory-panel-header">
            <div class="container">
                <h2 class="panel-header-title memory-panel-title"></h2>
                <button type="button" class="btn btn-outline-primary panel-close memory-close">
                    <i class="bi bi-x-lg" aria-hidden="true"></i>
                    Close
                </button>
            </div>
        </div>
        <div class="container">
            <div class="memory row">
                <p></p>
                <?php
                    include 'explore_entry.php';
                ?>
            </div>
        </div>
    </div>

    <div class="liked-entries-wrapper">
        <div class="panel-header liked-entries-panel-header">
            <div class="container">
                <h2 class="panel-header-title">
                    <i class="bi bi-heart" aria-hidden="true"></i>
                    Liked by visitors
                </h2>
                <button type="button" class="btn btn-outline-primary refresh-liked-entries-button" title="Show another selection of liked entries" hidden>
                    <i class="bi bi-shuffle" aria-hidden="true"></i>
                    Shuffle
                </button>
                <button type="button" class="btn btn-outline-primary panel-close liked-entries-close">
                    <i class="bi bi-x-lg" aria-hidden="true"></i>
                    Close
                </button>
            </div>
        </div>
        <div class="container">
            <div class="entries">
            <?php if($isLikedTrue) : ?>
                <input type="hidden" id="HiddenAutoLoadLikedEntries" value="true" />
            <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<div class="memory-overlay">
    <div>
        <a href="#" class="close" title="Close">
            <span aria-hidden="true">&times;</span>
        </a>
        <img src="#">
    </div>
</div>

<?php if($atlasDetails->enablePreLaunchFeatures == 1) : ?>

<div class="countdown <?= (!$master->days_helper->isSelectedDayLatestDay() ? "countdown-hidden" : "") ?>">
    <div class="container">
        <a href="submit.php">
            <h3 class="countdown-output">Loading...</h3>
            <input type="hidden" id="HiddenCountdownDate" value="<?= $master->days_helper->latest_day->getDateForInput() ?>" />
            <p>Register to be reminded when the <?= $master->days_helper->latest_day->getYear() ?> Memory Cloud Atlas goes live</p>
        </a>
    </div>
</div>

<?php endif; ?>

<div class="modal fade" id="ArchiveModal" tabindex="-1" aria-labelledby="ArchiveModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h1 class="modal-title fs-5" id="ArchiveModalLabel">Select Date</h1>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body btn-group-vertical">
                <?php 
                foreach ($master->days_helper->active_days as $day): 
                    $isSelected = $master->days_helper->selected_day->day_id == $day->day_id;
                    $isLatest = $master->days_helper->latest_day->day_id == $day->day_id;
                ?>
                    <a 
                        href="explore.php?day=<?= $day->day_id ?>" 
                        class="day-link btn btn-<?= ($isSelected ? "info" : "outline-primary") ?>" 
                        data-latest="<?= $isLatest ? 'true' : 'false' ?>"
                        data-day-id="<?= $day->day_id ?>"
                        data-day-style-url="<?= $day->mapboxStyleUrl ?>"
                        data-day-name="<?= $master->days_helper->getDateForDisplayWithoutYear($day) ?>"
                        data-day-year="<?= $day->getYear() ?>">
                        <?= $master->days_helper->getAltDisplayName($day) ?>
                    </a>
                <?php endforeach ?>
            </div>
        </div>
    </div>
</div>

<?php
echo $master->renderBottom();
$connect = null;
?>
