<?php

	class Controller{
		public static function getPath():string{
			$path = preg_replace("/[\.\,\#\?\*\@\!\$\&\+\(\)\[\]\{\}\´\`\^\~\'\"\=\;\ç\º\ª\á\à\ã\â\ä\ñ\é\è\ë\ê\í\ì\î\ï\ó\ò\ô\õ\ö\ú\ù\û\ü\Á\À\Ã\Â\Ä\Ñ\É\È\Ë\Ê\Í\Ì\Î\Ï\Ó\Ò\Ô\Õ\Ö\Ú\Ù\Û\Ü\Ç\ \¨\¬\£\¢\§\°\%]/", "", urldecode(explode("?", str_replace(ROOT_DIRECTORY, "", str_replace("/index.php", "", $_SERVER["REQUEST_URI"])))[0]));

			if (preg_match("/^[\/]/", $path) == 0) {
				$path = "/".$path;
			}

			if (preg_match("/[\/]$/", $path) == 0) {
				$path .= "/";
			}

			if ($path == "/") {
				return "";
			}
			else{
				return $path;
			}
		}

		public static function haveMethodPermission(?string $path = null):bool{
			if (empty($path)) {
				$path = self::getPath();
			}

			if (isset(ROUTES[$path])){
				if (isset(ROUTES[$path]["method"]) and gettype(ROUTES[$path]["method"]) == "string") {
					if (ROUTES[$path]["method"] == $_SERVER["REQUEST_METHOD"] or ROUTES[$path]["method"] == "ALL") {
						return true;
					}
					else{
						return false;
					}
				}
				else{
					return true;
				}
			}
			else{
				return false;
			}
		}

		public static function haveAuthenticationPermission(?string $path = null):bool{
			if (empty($path)) {
				$path = self::getPath();
			}

			if (isset(ROUTES[$path])){
				if (isset(ROUTES[$path]["authentication"]) and gettype(ROUTES[$path]["authentication"]) == "boolean") {
					if (
						ROUTES[$path]["authentication"] == false or 
						(
							isset(get_session("usuario")["id"]) and 
							!empty(get_session("usuario")["id"])
						)
					) {
						return true;
					}
					else{
						return false;
					}
				}
				else{
					return true;
				}
			}
			else{
				return false;
			}
		}

		public static function validateCSRF(?string $path = null):bool{
			if (empty($path)) {
				$path = self::getPath();
			}

			if (isset(ROUTES[$path])){
				if (isset(ROUTES[$path]["csrf_validation"]) and gettype(ROUTES[$path]["csrf_validation"]) == "boolean"){
					if (ROUTES[$path]["csrf_validation"] == true) {
						if (request_csrf_token() == get_csrf_token()) {
							return true;
						}
						else{
							return false;
						}
					}
					else{
						return true;
					}
				}
				else{
					return true;
				}
			}
			else{
				return false;
			}
		}

		public static function setHeadersConfigs(?string $path = null):void{
			if (empty($path)) {
				$path = self::getPath();
			}

			if (isset(ROUTES[$path])){
				if (isset(ROUTES[$path]["headers"]) and gettype(ROUTES[$path]["headers"]) == "array") {
					foreach (ROUTES[$path]["headers"] as $key => $value) {
						header("{$key}: {$value}");
					}
				}
			}
		}
		
		public static function existPage(?string $path = null):bool{
			if (empty($path)) {
				$path = self::getPath();
			}

			if (isset(ROUTES[$path]) and isset(ROUTES[$path]["file"])){
				if (file_exists(ROUTES[$path]["file"])) {
					return true;
				}
				else{
					return false;
				}
			}
			else{
				return false;
			}
		}

		public static function getPage(?string $path = null):?string {
			if (empty($path)) {
				$path = self::getPath();
			}

			if (self::existPage($path)) {
				return ROUTES[$path]["file"];
			}
			else{
				return null;
			}
		}

		public static function getLink(?string $path = null):?string {
			if (empty($path)) {
				$path = self::getPath();
				$path = empty($path) ? "/" : $path;
			}

			if (preg_match("/^[\/]/", $path) == 0) {
				$path = "/".$path;
			}

			return ROOT_DIRECTORY.$path;
		}

		public static function getErrorPage(string $page):string{
			if (ERRORS_PAGES[$page] != null and file_exists(ERRORS_PAGES[$page])) {
				return ERRORS_PAGES[$page];
			}
			else{
				throw new Exception("ERROR: This error page does not exist.", 500);
			}
		}
	}

?>
