<?php

class router {
        /*
         * @the registry
        */
        private $registry;

        function __construct($registry) {
                global $argv;

                require 'config/routes.php';
                $request_uri = $_SERVER['REQUEST_URI'];

                if (isset($argv) && count($argv) > 1) {
                        $request_uri = $argv[1];
                }

                if (strstr($request_uri, '?')) {
                        $params = substr($request_uri, strpos($request_uri, '?')+1);
                        $request_uri = substr($request_uri, 0, strpos($request_uri, '?'));

                        parse_str($params, $_GET);
                }

                $request_uri_exploded = explode('/', substr($request_uri, 1));
                $request_uri_exploded_rev = array_reverse($request_uri_exploded);

                foreach ($routes as $uri => $controller) {
                        // $uri = str_replace('*', end($request_uri_exploded), $uri);
                        // echo $uri . '<br />';

                        $uri = str_split($uri);
                        $i = count($request_uri_exploded)-2;
                        foreach ($uri as $index => $char) {
                                if ($char == '*') {
				                    if (isset($request_uri_exploded_rev[$i])) {
                                    	$uri[$index] = $request_uri_exploded_rev[$i];
                                        $i--;
				                    }
                                }
                        }
                        $uri = implode('', $uri);

                        if (trim($uri, '/') == trim($request_uri, '/')) {

                            $registry->args = $request_uri_exploded;
                            $cont = explode('.', $controller);
                            if ($cont[0] == 'module') {
                                $class = ucfirst($cont[1]) . 'Controller';
                                require 'modules/' . strtolower($cont[1]) . '/controller/'.strtolower($cont[1]).'.php';
                            }else{
                                $class = ucfirst($controller) . 'Controller';
                                require 'controllers/' . strtolower($controller) . '.php';
                            }

                            $controller = new $class($registry);

                            if (isset($request_uri_exploded[1]) && method_exists($controller, $request_uri_exploded[1])) {
                                // echo $class . ' / ' . $request_uri_exploded[1] . "\n";

                                call_user_func(array($controller, $request_uri_exploded[1]));
                                exit;
                            } else {
                                // echo $class . ' / index' . "\n";

                                $controller->index();
                                exit;
                            }
                        }
                }

                $pageModel = new pageModel();
                if (count($request_uri_exploded) == 1) {
                	$page = $pageModel->get(['slug' => $request_uri_exploded[0]]);

                	if ($page) {
                		$page['widgets'] = json_decode($page['widgets'], true);
                		require 'controllers/contentpage.php';
                        $controller = new ContentPageController();
                        $controller->dynamicPage($page);
                		exit;
                	}
                }
                header("Location: /404.html");
               //header("Status: 404 Not Found");
                //echo "404 Not Found...";
        }
}