<?php
/**
 * class routing
 *
 * Description for class routing
 *
 * @author:
*/
namespace core;

use FastRoute;

class routing {
	private $status = -1;
	private $handler = null;
	private $vars = array();

	public function __construct() {
		$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
			$r->addGroup("/administrator", function (FastRoute\RouteCollector $r) {
				$r->addRoute("GET", "", ["index", "index", "administrator"]);
				
				require __DIR__."/../area/administrator/config/routing.php";
			});

			$r->addGroup("/prezenter", function (FastRoute\RouteCollector $r) {
				$r->addRoute("GET", "", ["index", "index", "presenter"]);
				
				require __DIR__."/../area/presenter/config/routing.php";
			});

			$r->addGroup("/p", function (FastRoute\RouteCollector $r) {
				require __DIR__."/../area/presentation/config/routingP.php";
			});

			$r->addGroup("/r", function (FastRoute\RouteCollector $r) {
				require __DIR__."/../area/presentation/config/routingR.php";
			});
			
			require __DIR__."/../config/routing.php";
		});

		$uri = $_SERVER['REQUEST_URI'];
		if(false !== $pos = strpos($uri, '?')) {
			$uri = substr($uri, 0, $pos);
		}

		$uri = rawurldecode($uri);
		
		$routeInfo = $dispatcher->dispatch($_SERVER["REQUEST_METHOD"], $uri);
		switch ($routeInfo[0]) {
			case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
				$this->status = 405;
				$allowedMethods = $routeInfo[1];
				//405 Method Not Allowed
				__debug(405);
				break;
			case FastRoute\Dispatcher::FOUND:
				$this->status = 200;
				$this->handler = $routeInfo[1];
				$this->vars = $routeInfo[2];
				break;
			case FastRoute\Dispatcher::NOT_FOUND:
			default:
				$this->status = 404;
				//404 Not Found
				__debug(404);
				break;
		}
	}

	public function is_match() {
		if($this->status != 200) {
			return false;
		}

		return true;
	}

	public function get() {
		return array($this->handler, $this->vars);
	}
}
?>