<?php
/**
 * @author Gabriel Anhaia <gabriel@sadglobal.com.br>
 */
define('MAIN_RULE_GROUP', 1);
define('ALTERNATIVE_RULE_GROUP', 2);

/**
 * Consulta dinamica de classes
 */
spl_autoload_register(function ($class) {
    $rules = [
        MAIN_RULE_GROUP => [
            /**
             * Namespaces/Diretorios:
             *
             * Map de caminho dos diretorios
             *
             * Key: Primeira parte do namespace (incluindo o �ltimo backslash)
             * value: path do diretorio a partir da pasta "vendors/"
             *
             * exemplo:
             * 'Foo\\' => 'Foo/' pega os namespaces começados com "Foo\" e
             * busca pela classe em "vendors/Foo/".
             */
            'Utils' => '../utils/',
            /*'Components\\' => '../components/',
            'Config\\'     => '../config/',
            'Elements\\'   => '../elements/',
            'Helpers\\'    => '../helpers/',
            'Layouts\\'    => '../layouts/',
            'Libs\\'       => '../libs/',
            'Pages\\'      => '../pages/',
            'Tests\\'      => '../tests/',
            'Logs\\'       => '../logs/',
            'Vendors\\'    => '',*/
        ],
        ALTERNATIVE_RULE_GROUP => [
            /**
             * Classes:
             *
             * Tambem e possivel apontar diretamente pro arquivo.
             * (Necessario para classes que nao possuem namespace)
             *
             * exemplo:
             * 'NomeDaClasse' => '../../Name/Space/NomeDaClasse'
             */
            /*'Validator' => '../libs/Util/Validator',
            'Curl' => '../libs/Curl',
            'Util' => '../libs/Util/Util',
            'Logs' => 'Logs',
            'ExecutionLog' => 'ExecutionLog',
            'Marketplaces\\B2W\\' => '../components/marketplace/B2W/',
            'Marketplaces\\Cnova\\' => '../components/marketplace/cnova/',
            'Marketplaces\\Walmart\\' => '../components/marketplace/walmart/',
            'Marketplaces\\MercadoLivre\\' => '../components/marketplace/MercadoLivre/',
            'Marketplaces\\ShipmentServices\\' => '../components/marketplace/ShipmentServices/',
            'ERP\\Bling\\' => '../components/sistema_erp/bling_novo/',
            'Suppliers\\'    => '../components/Suppliers/',*/
        ],
    ];

    require_matched_class($class, $rules);
});

if ( ! function_exists('require_matched_class')) {
    function require_matched_class($class, $rulesCollection, $rulesGroup = MAIN_RULE_GROUP)
    {
        if (load_matched_file($class)) {
            return true;
        }

        $originClassName = $class;

        foreach ($rulesCollection[$rulesGroup] as $namespace => $replaceTo) {
            if (is_array($replaceTo)) {
                if ( ! empty($replaceTo['include'])) {
                    require_once __DIR__ . '/' . $replaceTo['include'];
                }
                $class = str_replace($namespace, $replaceTo['class'], $class);
            } else {
                $class = str_replace($namespace, $replaceTo, $class);
            }

            if ($originClassName !== $class) {
                return load_matched_file($class);
            }

        }

        if ($rulesGroup !== ALTERNATIVE_RULE_GROUP) {
            require_matched_class($class, $rulesCollection, ALTERNATIVE_RULE_GROUP);
        }
    }
}

if ( ! function_exists('load_matched_file')) {
    function load_matched_file($class)
    {
        $class = str_replace('\\', '/', $class);
        $requiredDir = __DIR__ . '/' . $class . '.php';

        if (file_exists($requiredDir)) {
            require_once $requiredDir;

            return true;
        }

        return false;
    }
}