<?php

spl_autoload_register(function ($class_name) {
    $path = explode('\\', $class_name);
    $fileName = $path[sizeof($path)-1];
    
    $path[sizeof($path)-1] = '';

    //$path = strtolower(DIR_ROOT. implode(DIRECTORY_SEPARATOR, $path));
    $path = DIR_ROOT . strtolower(implode(DIRECTORY_SEPARATOR, $path));

    if(is_file($path . $fileName . '.php')){
        return include $path . $fileName . '.php';
    }

    if(is_file(strtolower($path . $fileName . '.php'))){
        return include strtolower($path . $fileName . '.php');
    }

    @include $path . $fileName . '.php';
});

class Connection {

    private function __construct() {
        
    }

    public static function open($connection_info = null) {

        $user   = isset($connection_info['user']) ? $connection_info['user'] : NULL;
        $pass   = isset($connection_info['pass']) ? $connection_info['pass'] : NULL;
        $name   = isset($connection_info['name']) ? $connection_info['name'] : NULL;
        $host   = isset($connection_info['host']) ? $connection_info['host'] : NULL;
        $driver = isset($connection_info['driver']) ? $connection_info['driver'] : NULL;
        $port   = isset($connection_info['port']) ? $connection_info['port'] : NULL;
        $prefix = isset($connection_info['prefix']) ? $connection_info['prefix'] : '';

        switch ($driver) {
            case 'pgsql':
                $port = $port ? $port : '5432';
                $conn = new PDO("pgsql:dbname={$name}; user={$user}; password={$pass};host=$host;port={$port}");
                break;
            case 'mysql':
                $port = $port ? $port : '3306';
                $conn = new PDO("mysql:host={$host};port={$port};dbname={$name}", $user, $pass);
                break;
            case 'sqlite':
                $conn = new PDO("sqlite:{$name}");
                break;
            case 'ibase':
                $conn = new PDO("firebird:dbname={$name}", $user, $pass);
                break;
            case 'oci8':
                $conn = new PDO("oci:dbname={$name}", $user, $pass);
                break;
            case 'mssql':
                $conn = new PDO("mssql:host={$host},1433;dbname={$name}", $user, $pass);
                break;
        }
        $conn->prefix = $prefix;
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $conn;
    }

}

function is_ajax_request() {
    if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
        return true;
    } else {
        return false;
    }
}

function last_class() {
    $class = get_declared_classes()[sizeof(get_declared_classes()) - 1];
    return new $class;
}

global $_LANG;
$_LANG = Array();

class Lang {

    public static function getStrings($module = '', $lang = null){
        global $_LANG;
        if($lang == null){
            $lang = APP_LANG;
        }
        if (isset($_LANG[$lang][$module])) {
            return $_LANG[$lang][$module];
        }
        return $module;
    }

    public static function getString($string = '', $module = null, $lang = null) {
        global $_LANG;
        if($lang == null){
            $lang = APP_LANG;
        }
        if (isset($_LANG[$lang][$string]) && is_string($_LANG[$lang][$string])) {
            return $_LANG[$lang][$string];
        }
        if (isset($_LANG[$lang][$module][$string])) {
            return $_LANG[$lang][$module][$string];
        }
        return $string;
    }

    public static function buildStrings() {
        global $_LANG;

        if (isset($_SESSION['cache']['strings']) && CACHE_LANG) {
            $_LANG = $_SESSION['cache']['strings'];
            return;
        }

        $rdi = new RecursiveDirectoryIterator(DIR_APP);
        foreach (new RecursiveIteratorIterator($rdi) as $file) {
            if (preg_match("/lang\/.*\.php$/", $file)) {
                $included_lang = explode('/', $file);
                $included_lang = end($included_lang);
                $included_lang = str_replace('.php', '', $included_lang);
                $lang = Array();
                include_once $file;
                $_LANG[$included_lang] = $lang;
                $lang = Array();
            }
        }
    }
}

abstract class Controller {
    
}
