<?php

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

class Config {
    
    static public $home_module='welcome';
    
    static public $modules_allowed=['welcome'];
    
    static public $base_url='';
    
}

function get_controller($controller, $args=[]) {
    
    if(is_file($controller)) {
        
        include_once($controller);
        
        return call_user_func_array('controller', $args);
        
    }
    else {

        echo 'Page not found';
        
        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='modules/'.\PhangoApp\PhaRouter\Config::$home_module.'/controllers/app.php';
        
                return \PhangoApp\PhaRouter\get_controller($controller, []);
            
            break;
            
            case 1:
            
                $controller='modules/'.$arr_path[1].'/controllers/app.php';
            
                if(in_array($arr_path[1], Config::$modules_allowed)) {
        
                    return \PhangoApp\PhaRouter\get_controller($controller, []);
                    
                }
            
            break;
            
            case 2:
            
                $controller='modules/'.$arr_path[1].'/controllers/'.$arr_path[2].'.php';
        
                if(in_array($arr_path[1], Config::$modules_allowed)) {
        
                    return \PhangoApp\PhaRouter\get_controller($controller, []);
                    
                }
            
            break;
            
            default:
            
                $controller='modules/'.$arr_path[1].'/controllers/'.$arr_path[2].'.php';
            
                if(in_array($arr_path[1], Config::$modules_allowed)) {
            
                    $args=array_slice($arr_path, 2, count($arr_path));
                    
                    return \PhangoApp\PhaRouter\get_controller($controller, $args);
                    
                }
            
            break;
            
        }
        
    }
    else {
        
        $controller='modules/'.\PhangoApp\PhaRouter\Config::$home_module.'/controllers/app.php';
        
        return \PhangoApp\PhaRouter\get_controller($controller, []);
        
    }
    
    //If not return, page not found.
    
    echo 'Page not found';
        
    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='', $file='', $args=[]) {
        
        $url='/'.$module;
        
        if($script!='') {
            
            $url.='/'.$script;
            
        }
        
        if($file!='') {
            
            $url.='/'.$file;
            
        }
        
        if(count($args)>0) {
            
            $url.='/'.implode('/', $args);
            
        }
        
        return Config::$base_url.'/index.php'.$url;
        
    }

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

}
