<?php
/*
  This file is part of Irydeo Observatory Webpage (IOW).

  IOW is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  IOW is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with IOW.  If not, see <http://www.gnu.org/licenses/>.
 */
ini_set('display_errors', 1);


include_once '../../libs/i18n/lang.php';
include_once '../../libs/render/Render.php';
include_once '../../libs/fs/FileSystem.php';

//include IOSC
include_once '../../libs/iosc/IOSC.php';
require_once '../../libs/MobileDetect/Mobile_Detect.php';

//include 'phar://inc/iosc.phar/iosc/IOSC.php';
//require_once 'inc/observatory/Observatory.php';
//require_once 'inc/observatory/Weather.php';
//require_once 'inc/observatory/Camera.php';
//require_once 'inc/observatory/AAGCW.php';
// Debug
$isDebug = false;

// Objects
$render = new Render();
$fs = new FileSystem();
$camera = new Camera("admin", "iryfoscam99", "192.168.1.50");

// To be faster, now is disabled
// $camera->saveImage($_SERVER['DOCUMENT_ROOT'] . "/data/remote-res/camera/irydeo-latest.jpg");

$obs = new Observatory();
$weather = new OpenWeather();
$detect = new Mobile_Detect;

// Section selection
$whereWeAre = "";           // To store where we are (section, home, object, content...)
$isHome = true;             // Flag variable
$latestContent = "";        // Latest contens
$sectionIsEmpty = false;    // Stores if requested section is empty
// Number of latest items to show
$itemsToShow = 4;
if ($detect->isMobile()) {
    $itemsToShow = 2;
}


/* Check if there is a section */
if (isset($_GET['section'])) {
    $sectionSet = true;
    // Stores URL of latest modified folder IN selected section
    $latest = $fs->getLatest(htmlspecialchars($_GET['section']));
    $isHome = false;
    $whereWeAre = "section";

    // Check if section if empty
    if (strlen($latest) == 0) {
        $sectionIsEmpty = true;
    }
}

/* Check is there is a regular content */
if (isset($_GET['content'])) {
    $sectionSet = false;
    $isHome = false;
    $whereWeAre = "content";
}

/* Check is oven */
if (isset($_GET['oven'])) {
    $sectionSet = false;
    $isHome = false;
    $whereWeAre = "oven";
    $sectionIsEmpty = true;
}

/* Check is unconfirmed objects */
if (isset($_GET['uts'])) {
    $sectionSet = false;
    $isHome = false;
    $whereWeAre = "uts";
    $sectionIsEmpty = true;
}

/* Check if is a object */
if (isset($_GET['object'])) {
    $sectionSet = false;
    $latest = htmlspecialchars($_GET['object']);
    $isHome = false;
    $whereWeAre = "object";
}

/* If none of before elements, it is home */
if ($isHome) {
    $latest = $fs->getLatest();
    $latestContent = $fs->getLatest("data/contents/");
    $sectionSet = false;
    $whereWeAre = "home";
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Ek+Mukta">
        <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
        <link href="js/irydeo-menu/hamburguers/hamburgers.min.css" rel="stylesheet"/>
        <link rel="stylesheet" type="text/css" href="css/styles.css">
        <link rel="stylesheet" type="text/css" href="css/gallery.css">
        <link rel="stylesheet" href="https://aladin.u-strasbg.fr/AladinLite/api/v2/latest/aladin.min.css" />
        <script type="text/javascript" src="js/jquery-3.5.1.min.js"></script>
        <script src="js/irydeo-menu/mmenu/dist/js/jquery.mmenu.all.min.js" type="text/javascript"></script>
        <script src="js/irydeo-stick-menu.js" type="text/javascript"></script>
        <link href="js/irydeo-stick-menu.css" type="text/css" rel="stylesheet" />
        <link href="js/irydeo-menu/mmenu/dist/css/jquery.mmenu.all.css" type="text/css" rel="stylesheet" />

        <!-- New slider -->
        <script type="text/javascript" src="js/jquery.immersive-slider.js"></script>
        <link href='css/personalized-slider.css' rel='stylesheet' type='text/css'>
        <link href='css/immersive-slider.css' rel='stylesheet' type='text/css'>
        <!-- End new slider -->

        <title><?php echo $lang['PAGE_TITLE']; ?></title>
    </head>

    <body onload="setEqualHeight()">
        <div id="irydeo-webpage" class="mm-slideout">
            <div id="preload_area"></div>

            <header>
                <div id="hamburger-container">
                    <a href="#" id="hamburger" class="hamburger hamburger--squeeze">
                        <span class="hamburger-box">
                            <span class="hamburger-inner"></span>
                        </span>
                    </a>

                    <!-- Image after scrolling -->
                    <a href="/">
                        <img id="img_header_small" src="images/logo.png" alt="Irydeo Logo">
                    </a>

                    <span id="lang-change-control">
                        <a href="<?php
                        $_GET['lang'] = "en";
                        $append = "?";
                        foreach ($_GET as $key => $value) {
                            $append .= htmlspecialchars("$key=$value&");
                        }
                        echo "/" . $append;
                        ?>">EN</a> | <a href="<?php
                           $_GET['lang'] = "es";
                           $append = "?";
                           foreach ($_GET as $key => $value) {
                               $append .= htmlspecialchars("$key=$value&");
                           }
                           echo "/" . $append;
                           ?>">ES</a>
                    </span>
                </div>
                <!-- JS will asign correct image -->
                <a href="/">
                    <img id="header-image" src="images/empty.png" alt="Irydeo Observatory"/>
                </a>

                <!-- This div is used to indicate the original position of the scrollable image, after scrolling it, an image in fixed header will be displayed -->
                <div class="scroller_anchor"></div>
            </header>

            <div id="container">
                
                <?php
                /**
                 * Section render selection                
                 */
                if (isset($_GET['content'])) {
                    // This is an article or resources, no render common HTML
                    $render->renderContent($_GET['content']);
                } else {
                    // Only render if section is not empty
                    if (!$sectionIsEmpty) {
                        // Common HTML for all sections (home, section, object)

                        $latestFileInformation = parse_ini_file(htmlspecialchars($latest) . "/img.ini");
                        ?>
                        <?php
                        /* At home we are not going to show latest image, instead we will show personalized carrousel */
                        if (!$isHome) {
                            ?>
                            <section id="latest">
                                <h1><?php
                                    if ($whereWeAre == "object") {
                                        echo $latestFileInformation['name'];
                                    } else {
                                        echo $lang['LATEST_WORK'];
                                    }
                                    ?></h1>

                                <p><?php
                                    if ($whereWeAre != "object") {
                                        echo $lang['LATEST_WORK_EXPLANATION'];
                                    }
                                    ?></p>
                                <div class='imgContainer'>
                                    <div id="first">
                                        <div id="objectName">
                                            <h2><?php
                                                echo $latestFileInformation['name'];
                                                echo " (" . $latestFileInformation['type'] . ")";
                                                ?></h2>
                                            <h3><?php echo $latestFileInformation['date']; ?></h3>
                                        </div>
                                        <img src="images/grey_pixel.png" alt="Separator">

                                        <?php
                                        /* Table to format image data */
                                        include "./inc/imageDataTable.php";
                                        ?>


                                    </div>
                                    <div id="second">
                                        <a href="<?php
                                        if ($whereWeAre == "object") {
                                            echo $latest . "/object-img.jpg";
                                        } else {
                                            echo "index.php?object=" . $latest;
                                        }
                                        ?>" <?php
                                           if ($whereWeAre == "object") {
                                               echo "target='_blank'";
                                           }
                                           ?>>
                                            <img src="<?php echo $latest . "/object-img.jpg"; ?>" alt="Latest image" id="latestImage"/>
                                        </a>
                                    </div>
                                    <div class="clear"></div>
                                </div>
                            </section>
                            <?php
                        } else {
                            ?>

                            <!-- New slider -->
                            <div class="wrapper">
                                <div class="main">
                                    <div class="page_container">
                                        <div id="immersive_slider">

                                            <?php
                                            /* Include all featured items for main carrousel */
                                            include "./inc/featured.php";
                                            ?>

                                            <a href="#" class="is-prev">&laquo;</a>
                                            <a href="#" class="is-next">&raquo;</a>
                                        </div>
                                    </div>
                                </div>

                                <script type="text/javascript">
                                    $(document).ready(function () {
                                        $("#immersive_slider").immersive_slider({
                                            container: ".main"
                                        });
                                    });

                                </script>
                            </div>
                            <!-- End new slider -->
                            <?php
                        }
                        /* End if it is home */
                        ?>

                        <?php
                        /** END of common HTML */
                        // We can show a section or a full object data, no both
                        if (!isset($_GET['object'])) {
                            ?>
                            <section class="newestButOlder">
                                <?php
                                if ($sectionSet) {
                                    // Inside a section, show all contents
                                    $render->renderLatestUpdatedItems(9999, false, $_GET['section'], $_SERVER['DOCUMENT_ROOT']);
                                } else {
                                    // Inside home
                                    if ($isHome) {
                                        // Previous behaviour was to show latest 4 objects
                                        // $render->renderLatestUpdatedItems(4, false, "data/works/", $_SERVER['DOCUMENT_ROOT']);
                                        ?>
                                        <p class='msg_intro'><?php
                                            echo $lang['MSG_IRYDEO'];
                                            ?></p>

                                        <h3 class='headOfGroup'><a href="/index.php?content=data/contents/results"><?php echo $lang['MSG_INTRO_3']; ?></a></h3>
                                        <section class="ovenImages">
                                            <?php echo $render->renderLatestObservations($itemsToShow * 2, "data/contents/results/images/2025", $_SERVER['DOCUMENT_ROOT']); ?>                                                                      
                                        
                                        </section>

                                        
                                        <h3 class='headOfGroup'><a href="index.php?section=data/works/supernovas"><?php echo $lang['MENU_SUPERNOVAS']; ?></a></h3>
                                            <?php
                                            $render->renderLatestUpdatedItems($itemsToShow, true, "data/works/supernovas", $_SERVER['DOCUMENT_ROOT']);
                                            ?>

                                        <h3 class='headOfGroup'><a href="index.php?section=data/works/minorobjects"><?php echo $lang['MENU_MINOR_OBJECTS']; ?></a></h3>
                                            <?php
                                            $render->renderLatestUpdatedItems($itemsToShow, true, "data/works/minorobjects", $_SERVER['DOCUMENT_ROOT']);
                                            ?>

                                        <h3 class='headOfGroup'><a href="index.php?section=data/works/astrophoto"><?php echo $lang['MENU_DEEP_SKY']; ?></a></h3>
                                            <?php
                                            $render->renderLatestUpdatedItems($itemsToShow, true, "data/works/astrophoto", $_SERVER['DOCUMENT_ROOT'], true, true);
                                        }
                                    }
                                    ?>
                            </section>



            <?php
        } else {
            // Is an object
            if (!empty($_GET['object'])) {
                $render->showObject($_GET['object']);

                // Si no hay .ini ni tampoco hay la imagen que le corresponda, no se renderiza nada (esto se reutiliza siempre)
                // repensar... a ver si se puede poner algún flag o bien, otro parámetro por GET, que quizás es lo más simple..., de tal
                // manera que todo (inicio, secciones y objeto comparten estructura), cambia cuando pasemos "article". 
            }
        }
    } // Enf if section or object is not empty
    else {
        if (($whereWeAre != "oven") && ($whereWeAre != "uts")) {
            // If section is empty, render empty image
            ?>
                            <img src="images/section_empty.png" id="sectionEmpty" alt="Section is empty"/>
                            <?php
                        }
                    }
                }
                ?>
                <?php if ($isHome) { ?>

                    <!-- Other news and articles to show (latest content from "contents" -->
    <?php
    if (file_exists($latestContent . "/home.ini")) {
        ?>
                        <div id="moreInfo">
                            <p>
                                <a href="index.php?content=<?php echo $latestContent; ?>">
                                    <img class="floatLeft" src="<?php echo $latestContent; ?>/home.jpg" alt="Latest content"/>
                                </a>

                            <h4>
                                <a href="index.php?content=<?php echo $latestContent; ?>">
        <?php
        $latestContentInformation = parse_ini_file($latestContent . "/home.ini");
        echo $latestContentInformation['name'];
        ?>
                                </a>
                            </h4>
        <?php
        $render->renderMarkdownFile($latestContent, "home");
        ?> 
                            </p>
                            <div class="clear"></div>
                        </div>
    <?php } // End if file exists     ?>

                    <?php if (($whereWeAre == "home") && false) { // Oven preview section    ?>          
                        <div id="oven">
                            <h3 class='headOfGroup'><a href="/index.php?uts=see"><?php echo $lang['UTS']; ?></a></h3>

                            <section class="ovenImages">
        <?php echo $render->renderOvenPreview(4, "data/uts/", $_SERVER['DOCUMENT_ROOT']); ?>

                            </section>
                        </div>
    <?php } // End oven preview     ?>


                    <!-- Observatory image and status --> 
                    <section id="pre-footer">
                        <div id="irydeo-overview">
                            <img src="data/remote-res/camera/irydeo-latest.jpg" id="irydeo-overview-image" alt="Latest Irydeo Image"/>
                        </div>
    <?php
    /* Real time Irydeo features */
    include "./inc/irydeoRealTime.php";
    ?>
                    </section>

<?php } // End if home        ?>

                <?php if ($whereWeAre == "oven") { // Oven section    ?>          
                    <div id="oven">
                        <h1><?php echo $lang['OVEN']; ?></h1>
                        <p>
    <?php echo $lang['OVEN_EXPLANATION']; ?>
                        </p>
                        <section class="ovenImages objectDescription">
    <?php $render->renderOven("data/oven/", $_SERVER['DOCUMENT_ROOT']); ?>
                        </section>
                    </div>
<?php } // End oven     ?>


<?php if ($whereWeAre == "uts") { // Unconfirmed object section     ?>          
                    <div id="oven">
                        <h1><?php echo $lang['UTS']; ?></h1>
                        <p>
    <?php echo $lang['UTS_EXPLANATION']; ?>
                        </p>
                        <section class="ovenImages">
    <?php $render->renderOven("data/uts/", $_SERVER['DOCUMENT_ROOT']); ?>
                        </section>
                    </div>
<?php } // End oven     ?>
            </div>

            <div class="page-divider"></div>
            <footer id="footer">
                <div class="page-divider"></div>

                <div id="socialLinks" class="social-links">
                    <h4><?php echo $lang['CONTACT_SOCIAL_NETWORKS']; ?></h4>                    
                    <a href="https://twitter.com/irydeo_obs" target="_blank"><img class="social-links" src="images/social/ico_twitter.svg" alt="Twitter"/></a>
                    <a href="https://www.facebook.com/irydeoobs.obs" target="_blank"><img class="social-links" src="images/social/ico_facebook.svg" alt="Facebook"/></a>                    
                </div>

                <p>&copy; 2017-2026, Francisco José Calvo Fernández</p>
                <p>Powered by Irydeo Observatory Software Compilation 3.2 (php8)</p>
            </footer>
        </div>

<?php
/* Include all code for side menu */
include "./inc/irydeoMenu.php";
?>

        <script src="js/coreUtils.js" type="text/javascript"></script>

    </body>

</html>

