<?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));
                
				if (count($request_uri_exploded) == 2) {
					//echo "<br /><br /><br /><br />Solution maybe " . print_r($request_uri_exploded);
					$pagezModel = new pagezModel();
					$page = $pagezModel->get(['id' => $request_uri_exploded[1]]);

					if ($page) {
						//$page['widgets'] = json_decode($page['widgets'], true);
						require 'controllers/pagez.php';
						$controller = new PagezController();
						$controller->dynamicPage($page);
						exit;
					}
				}
                
                $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';
								//$class = "ListingController";
                                
                                require 'modules/' . strtolower($cont[1]) . '/controller/'.strtolower($cont[1]).'.php';
                            }else{
                                $class = ucfirst($controller) . 'Controller';
                                require 'controllers/' . strtolower($controller) . '.php';
                            }

                            $controller = new $class($registry);
//							print_r($controller);
//							exit;

                            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";
                                //echo "hey";
                                $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...";
        }
}