<?php
/**
 * app/templates/default/default.inc.php
 *
 * This is the loader for the default template.
 *
 * @version 3.0
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
namespace TheTempusProject\Templates;

use TheTempusProject\Houdini\Classes\Loader;
use TheTempusProject\Houdini\Classes\Template;
use TheTempusProject\Houdini\Classes\Views;
use TheTempusProject\Houdini\Classes\Navigation;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Bedrock\Classes\Config;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\TheTempusProject as App;

class DefaultLoader extends Loader {
    private static $loaded = false;
    const TEMPLATE_NAME = 'Default Tempus Project Template';
    const JQUERY_CDN = 'https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/';
    const BOOTSTRAP_CDN = 'https://cdn.jsdelivr.net/npm/bootstrap@3.3.6/dist/';
    const FONT_AWESOME_URL = 'https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/';

    public function __construct() {
        if ( self::$loaded ) {
            return;
        }
        Components::set( 'DARK_MODE_SETTING', '' );
        Components::set( 'TEMPLATE_URL', Template::parse( '{ROOT_URL}app/templates/default/' ) );
        if ( VENDOR_AUTOLOADED === true ) {
            Components::set( 'FONT_AWESOME_URL', '/vendor/fortawesome/font-awesome/css/' );
            Components::set( 'BOOTSTRAP_CDN', '/vendor/twbs/bootstrap/dist/' );
            Components::set( 'JQUERY_CDN', '/vendor/components/jquery/' );
        } else {
            Components::set( 'BOOTSTRAP_CDN', self::BOOTSTRAP_CDN );
            Components::set( 'JQUERY_CDN', self::JQUERY_CDN );
            Components::set( 'FONT_AWESOME_URL', self::FONT_AWESOME_URL );
        }
        $this->addJs( '<script language="JavaScript" crossorigin="anonymous" type="text/javascript" src="{ROOT_URL}app/js/main.js"></script>' );
        Components::setIfNull( 'LOGO', Config::getValue( 'main/logo' ) ?? TP_DEFAULT_LOGO );
        Components::setIfNull( 'COPY', Views::simpleView( 'footer.copy') );
        Components::setIfNull( 'SOCIAL', Views::simpleView( 'footer.social') );
        Components::prepend( 'FOOTER_LEFT', Views::simpleView( 'footer.left', Navigation::getMenuLinks( App::CONTACT_FOOTER_MENU_NAME ) ) );
        Components::prepend( 'FOOTER_CENTER', Views::simpleView( 'footer.center', Navigation::getMenuLinks( App::INFO_FOOTER_MENU_NAME ) ) );
        Components::prepend( 'FOOTER_RIGHT', Views::simpleView( 'footer.right') );
        Components::setIfNull( 'FOOT', Views::simpleView( 'footer.container') );
        /**
         * Top-Nav
         */
        if ( App::$isLoggedIn ) {
            if ( ! empty( App::$activePrefs['darkMode'] ) ) {
                Components::set( 'DARK_MODE_SETTING', 'true' );
                $this->addCss( '<link rel="stylesheet" href="{ROOT_URL}app/css/main.css">' );
                $this->addCss( '<link rel="stylesheet" href="{ROOT_URL}app/css/main-dark.css" id="dark-mode-stylesheet">' );
            } else {
                Components::set( 'DARK_MODE_SETTING', 'false' );
                $this->addCss( '<link rel="stylesheet" href="{ROOT_URL}app/css/main.css">' );
                $this->addCss( '<link rel="stylesheet" href="{ROOT_URL}app/css/main-dark.css" id="dark-mode-stylesheet" disabled>' );
            }
            Components::set( 'topNavRightDropdown', Template::parse( App::$topNavRightDropdown ) );
            Components::set( 'STATUS', Views::simpleView( 'nav.statusLoggedIn' ) );
            Components::set( 'USERNAME', \ucfirst( App::$activeUser->username ) );
            Components::set( 'AVATAR', App::$activeUser->avatar );
        } else {
            Components::set( 'STATUS', Views::simpleView( 'nav.statusLoggedOut' ) );
            $this->addCss( '<link rel="stylesheet" href="{ROOT_URL}app/css/main.css">' );
            $this->addCss( '<link rel="stylesheet" href="{ROOT_URL}app/css/main-dark.css" id="dark-mode-stylesheet" disabled>' );
        }
        Components::set( 'topNavRight', Template::parse( App::$topNavRight . '{STATUS}' ) );
        Components::set( 'topNavLeft', Views::simpleView( 'nav.main', Navigation::getMenuLinks( App::MAIN_MENU_NAME ) ) );
        Components::set( 'colorSelect', Views::simpleView( 'forms.colorSelect' ) );
        Components::set( 'iconSelect', Views::simpleView( 'forms.iconSelect' ) );
        Navigation::setCrumbComponent( 'BREADCRUMB', Input::get( 'url' ) );
        $this->buildComponents();
        self::$loaded = true;
    }
}