<?php

namespace PhangoApp\PhaRouter;
use PhangoApp\PhaUtils\Utils;

class Config {
    
    static public $home_module='welcome';
    
    static public $modules_allowed=['welcome' => 'modules/welcome'];
    
    static public $base_url='';
    
    static public $data=[];
    
    static public $index_file='index.php/';
    
    static public $get_path_info=false;

    static public $on_other=false;
    
}

/*Examples
*  Url: http://domain.com/
*  Url: http://domain.com/index.php/{module}
*  Url: http://domain.com/index.php/{module}/app/{arg1}/{arg2} -> modules/module/controller_app.php controller($args);
*  Url: http://domain.com/index.php/{module}/index/{arg1}/{arg2} -> modules/module/controller_index.php AppController->App($args);
*/ 

function get_controller($controller, $args=[]) {

    $controller=__DIR__.'/../'.$controller;

    if(is_file($controller)) {
        
        include_once($controller);
        
        if(class_exists('AppController')) {
        
            $controller=new \AppController();
            
            //return $controller->App($args);
            return call_user_func_array(array($controller, 'App'), $args);
        
        }
        else {
            
            return call_user_func_array('controller', $args);
        }
        
    }
    else {

        echo 'Page not found';
        
        if(!\PhangoApp\PhaRouter\Config::$on_other) {

            http_response_code(404);
   
        }
    
        exit(1);
        
    }
    
}

//Route is: /module/switch/
//Route is: /module/file/switch/
//Route is: /module/file/switch/parameter1/parameter2

function get_route($path_info) {

    if($path_info!='') {
        
       $arr_path=filter_path($path_info);
        
        switch (count($arr_path)) {
            
            case 0:
            
                $controller=\PhangoApp\PhaRouter\Config::$modules_allowed[\PhangoApp\PhaRouter\Config::$home_module].'/controllers/app.php';
        
                return \PhangoApp\PhaRouter\get_controller($controller, []);
            
            break;
            
            case 1:
                
                if(isset(Config::$modules_allowed[$arr_path[1]])) {
                    
                    $module_path=Config::$modules_allowed[$arr_path[1]];
            
                    $controller=$module_path.'/controllers/app.php';
        
                    return \PhangoApp\PhaRouter\get_controller($controller, []);
                    
                }
            
            break;
            
            case 2:
                
                if(isset(Config::$modules_allowed[$arr_path[1]])) {
            
                    $module_path=Config::$modules_allowed[$arr_path[1]];
            
                    $controller=$module_path.'/controllers/'.$arr_path[2].'.php';
                    
                    return \PhangoApp\PhaRouter\get_controller($controller, []);
                    
                }
            
            break;
            
            default:
                
                if(isset(Config::$modules_allowed[$arr_path[1]])) {
                
                    $module_path=Config::$modules_allowed[$arr_path[1]];
                    
                    $controller=$module_path.'/controllers/'.$arr_path[2].'.php';
            
                    $args=array_slice($arr_path, 2, count($arr_path));
                    
                    return \PhangoApp\PhaRouter\get_controller($controller, $args);
                    
                }
            
            break;
            
        }
        
    }
    else {
        
        $controller=\PhangoApp\PhaRouter\Config::$modules_allowed[\PhangoApp\PhaRouter\Config::$home_module].'/controllers/app.php';
        
        return \PhangoApp\PhaRouter\get_controller($controller, []);
        
    }
    
    //If not return, page not found.
    
    echo 'Page not found';
    
    if(!\PhangoApp\PhaRouter\Config::$on_other) {
    
        http_response_code(404);

    }

    exit(1);


}

function filter_path($path_info) {
    
    $final_path=[];
    
    $arr_path=explode('/', $path_info);
        
    if($arr_path[count($arr_path)-1]=='') {
        
        unset($arr_path[count($arr_path)-1]);
        
    }

    $z=1;

    unset($arr_path[0]);
    
    foreach($arr_path as $k => $info) {
        
        $v=Utils::slugify($info);
        
        if($v!='') {
            
            $final_path[$z]=$v;
            
            $z++;
            
        }
    }
    
    return $final_path;
    
}

class Url {

    static public function make_url($module, $script='', $args=[]) {
                
        $url=$module;
        
        if($script!='') {
            
            $url.='/'.$script;
            
        }
        
        if(count($args)>0) {
            
            $url.='/'.implode('/', $args);
            
        }
        
        return Config::$base_url.Config::$index_file.$url;
        
    }

    static public function make_media_url($file) {
        
        return Config::$base_url.$file;
        
    }

}

class Controller {
    
    
    
}
