<?php

use HtmlBuilder\HtmlElement as Html;

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 build_input_wrap($label, $element) {
    return Html::instance('div')
                    ->add_property('class', 'form-group col-xs-12')
                    ->add_child(Html::instance('label')->content($label))
                    ->add_child($element);
}

function build_input_text($label, $name, $value = '') {
    return Html::instance('div')
                    ->add_property('class', 'form-group col-xs-12')
                    ->add_child(Html::instance('label')->content($label))
                    ->add_child(Html::instance('input')
                            ->add_property('type', 'text')
                            ->add_property('name', $name)
                            ->add_property('value', $value)
                            ->add_property('class', 'form-control'));
}

function build_input_password($label, $name) {
    return Html::instance('div')
                    ->add_property('class', 'form-group col-xs-12')
                    ->add_child(Html::instance('label')->content($label))
                    ->add_child(Html::instance('input')
                            ->add_property('type', 'password')
                            ->add_property('name', $name)
                            ->add_property('class', 'form-control'));
}

function build_select($name, $values, $selected = '') {
    $select = Html::instance('select')
            ->add_property('class', 'form-control')
            ->add_property('name', $name);

    if ($selected == '') {
        $select->add_child(
                Html::instance('option')
                        ->add_property('value', '0')
                        ->content('Selecione')
                        ->add_property('selected', 'selected')
        );
    } else {
        $select->add_child(
                Html::instance('option')
                        ->add_property('value', '0')
                        ->content('Selecione')
        );
    }

    foreach ($values as $value) {
        if ($value->id == $selected) {
            $select->add_child(
                    Html::instance('option')
                            ->add_property('value', $value->id)
                            ->content($value->content)
                            ->add_property('selected', 'selected')
            );
        } else {
            $select->add_child(
                    Html::instance('option')
                            ->add_property('value', $value->id)
                            ->content($value->content)
            );
        }
    }
    return $select;
}

function build_remote_popup($relay = '', $action = '', $name = '', $label = '', $value = false) {

    $label = Html::instance('label')->content($label);

    $first_section = Html::instance('div')->add_property('class', 'col-xs-12 col-md-3');
    $first_section_input = Html::instance('div')->add_property('class', 'input-group');
    
    if($value){
        $first_section_input->add_child(Html::instance('input')
                ->add_property('class', 'form-control remote-popup-number')
                ->add_property('value', $value));
    }else{
        $first_section_input->add_child(Html::instance('input')
                ->add_property('class', 'form-control remote-popup-number'));
    }
    $button_input = Html::instance('span')
            ->add_property('class', 'input-group-btn')
            ->add_child(Html::instance('button')
                    ->add_property('class', 'btn btn-default')
                    ->add_child(Html::instance('i')
                            ->add_property('class', 'fa fa-search')));
    
    $first_section_input->add_child($button_input);
    
    $first_section->add_child($first_section_input);
    
    $second_section = Html::instance('div')
            ->add_property('class', 'col-xs-12 col-md-9')
            ->add_child(Html::instance('input')
                    ->add_property('class', 'form-control remote-popup-label')
                    ->add_property('type', 'text')
                    ->add_property('disabled', 'true'));
    
    $wrapper = Html::instance('div')
            ->add_property('class', 'form-group col-xs-12 remote-popup')
            ->add_property('data-relay', $relay)
            ->add_property('data-mode', $action)
            ->add_child($label)
            ->add_child(Html::instance('br'))
            ->add_child($first_section)
            ->add_child($second_section);

    return $wrapper->build();
}

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

global $_LANG;
$_LANG = Array();

class Lang {

    public static function get_string($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 build_strings() {
        global $_LANG;

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

        $rdi = new RecursiveDirectoryIterator(DIR_APPLICATION);
        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 {
    
}

/*function load_view($name, $values = null, $location = null) {

    if (!$location) {
        $segments = explode('/', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]['file']);
        array_pop($segments);
        $location = implode('/', $segments) . '/views/';
    }
    
    if (is_array($values) && count($values) > 0) {
        extract($values, EXTR_PREFIX_SAME, 'data');
    }
    
    $location .= $name;
    
    if( strpos('.php', $location) == 0){
        $location .= '.php';
    }
    
    if (!file_exists($location)) {
        //return require_once GENERAL_VIEW_FOLDER . 'errors/404.php';
    }
    return require_once( $location );
}*/
