<?php
/**
 * bin/autoload.php
 *
 * Handles the application startup by requiring our autoloaders and loading constants.
 *
 * @version 3.0
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
namespace TheTempusProject;

use TheTempusProject\Classes\Plugin;
use TheTempusProject\Hermes\Classes\Autoloader;
use TheTempusProject\Houdini\Classes\Template;
use TheTempusProject\Houdini\Classes\Views;

// Basic constants needed for loading files
define( 'APP_ROOT_DIRECTORY', dirname( __DIR__ ) . DIRECTORY_SEPARATOR );
define( 'CONFIG_DIRECTORY', APP_ROOT_DIRECTORY . 'app' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR );

// App Constants
if ( ! defined( 'TEMPUS_PROJECT_CONSTANTS_LOADED' ) ) {
    require_once CONFIG_DIRECTORY . 'constants.php';
}

// Determine which autoloader to sue
if ( file_exists( VENDOR_DIRECTORY . 'autoload.php' ) ) {
    // Composer Autoloader
    require_once VENDOR_DIRECTORY . 'autoload.php';
    define( 'VENDOR_AUTOLOADED', true );
} else {
    sideLoad();
}

if ( class_exists( 'TheTempusProject\Hermes\Classes\Autoloader' ) && TEMPUS_PROJECT_CONSTANTS_LOADED ) {
    ttp_autoload();
}

// Print an error page when autoloader can't be used
if ( ! VENDOR_AUTOLOADED && ! defined( 'TEMPUS_PROJECT_AUTOLOADED' ) ) {
    echo file_get_contents( ERRORS_DIRECTORY . 'autoload.html' );
    exit;
}

function ttp_autoload() {
    $Autoloader = new Autoloader;
    $Autoloader->addNamespace(
        APP_SPACE . '\Controllers',
        CONTROLLER_DIRECTORY,
        false,
    );
    $Autoloader->addNamespace(
        APP_SPACE . '\Controllers\Admin',
        ADMIN_CONTROLLER_DIRECTORY,
        false,
    );
    $Autoloader->addNamespace(
        APP_SPACE . '\Controllers\Api',
        API_CONTROLLER_DIRECTORY,
        false,
    );
    $Autoloader->addNamespace(
        APP_SPACE . '\Models',
        MODEL_DIRECTORY,
        false,
    );
    $Autoloader->addNamespace(
        APP_SPACE . '\Classes',
        CLASSES_DIRECTORY,
        false,
    );
    $Autoloader->includeFolder(FUNCTIONS_DIRECTORY);
    $Autoloader->register();

    // handle plugins
    $pluginDirectoryArray = Plugin::getPluginDirectories();
    foreach ( $pluginDirectoryArray as $pluginName => $locations ) {
        foreach ( $locations as $location ) {
            foreach ( $location as $currentFolder => $file ) {
                switch ($file) {
                    case 'controllers':
                        if (file_exists($currentFolder . 'api')) {
                            $Autoloader->addNamespace( APP_SPACE . '\Controllers\Api', $currentFolder . 'api' . DIRECTORY_SEPARATOR, false );
                        }
                        if (file_exists($currentFolder . 'admin')) {
                            $Autoloader->addNamespace( APP_SPACE . '\Controllers\Admin', $currentFolder . 'admin' . DIRECTORY_SEPARATOR, false );
                        }
                        $Autoloader->addNamespace( APP_SPACE . '\Controllers', $currentFolder, false );
                        break;
                    case 'models':
                        $Autoloader->addNamespace( APP_SPACE . '\Models', $currentFolder, false );
                        break;
                    case 'views':
                        Views::addViewLocation($pluginName, $currentFolder);
                        break;
                    case 'templates':
                        Template::addTemplateLocation($currentFolder);
                        break;
                    case 'forms.php':
                        require_once( $currentFolder );
                        break;
                    case 'plugin.php':
                        require_once( $currentFolder );
                        break;
                    case 'config':
                        if ( file_exists( $currentFolder . 'constants.php' ) ) {
                            require_once( $currentFolder . 'constants.php' );
                        }
                        break;
                }
            }
        }
    }
    define('TEMPUS_PROJECT_AUTOLOADED', true);
}

function sideLoad() {
    // Hermes Constants
    if ( ! defined( 'HERMES_CONSTANTS_LOADED' ) ) {
        if ( defined( 'HERMES_CONFIG_DIRECTORY' ) ) {
            require_once HERMES_CONFIG_DIRECTORY . 'constants.php';
        }
    }

    // Bedrock Constants
    if ( ! defined( 'BEDROCK_CONSTANTS_LOADED' ) ) {
        if ( defined( 'BEDROCK_CONFIG_DIRECTORY' ) ) {
            require_once BEDROCK_CONFIG_DIRECTORY . 'constants.php';
        }
    }

    // Canary Constants
    if ( ! defined( 'CANARY_CONSTANTS_LOADED' ) ) {
        if ( defined( 'CANARY_CONFIG_DIRECTORY' ) ) {
            require_once CANARY_CONFIG_DIRECTORY . 'constants.php';
        }
    }

    // Require common functions
    require_once FUNCTIONS_DIRECTORY . 'common.php';

    // Hermes Autoloader (Autoloader)
    if ( ! defined( 'HERMES_AUTOLOADED' ) ) {
        if ( defined( 'HERMES_ROOT_DIRECTORY' ) ) {
            require_once HERMES_ROOT_DIRECTORY . 'bin' . DIRECTORY_SEPARATOR . 'autoload.php';
        }
    }

    // Canary Autoloader (Debugging)
    if ( ! defined( 'CANARY_AUTOLOADED' ) ) {
        if ( defined( 'CANARY_ROOT_DIRECTORY' ) ) {
            require_once CANARY_ROOT_DIRECTORY . 'Bin' . DIRECTORY_SEPARATOR . 'autoload.php';
        }
    }

    // Bedrock Autoloader (Core Functionality)
    if ( ! defined( 'BEDROCK_AUTOLOADED' ) ) {
        if ( defined( 'BEDROCK_ROOT_DIRECTORY' ) ) {
            require_once BEDROCK_ROOT_DIRECTORY . 'Bin' . DIRECTORY_SEPARATOR . 'autoload.php';
        }
    }

    // Houdini Autoloader (Frontend Driver)
    if ( ! defined( 'HOUDINI_AUTOLOADED' ) ) {
        if ( defined( 'HOUDINI_ROOT_DIRECTORY' ) ) {
            require_once HOUDINI_ROOT_DIRECTORY . 'bin' . DIRECTORY_SEPARATOR . 'autoload.php';
        }
    }

    define( 'VENDOR_AUTOLOADED', false );
}

require_once 'tempus_project.php';
