<?php

namespace SHServ;

class Routes {
	/**
	 * Instance of Router module
	 */
	protected \Fury\Modules\Router\Router $router;

	/**
	 * Controllers folder
	 */
	protected String $cf;

	/**
	 * Controllers namespace
	 */
	protected String $cn;

	/**
	 * @method __construct
	 * @param \Fury\Modules\Router\Router $router [description]
	 */
	public function __construct(\Fury\Modules\Router\Router $router) {
		$this -> router = $router;
		$this -> cf = FCONF["controllers_folder"];
		$this -> cn = "\\" . FCONF["app_name"] . "\\" . FCONF["controllers_folder"];
	}

	public function routes_init() {
		$this -> uri_routes();
		$this -> get_routes();
		$this -> post_routes();
	}

	protected function uri_routes() {
		// pages
		// $this -> router -> uri("/", "{$this -> cn}\\SearchController@search_page");
		// $this -> router -> uri('/not-found.html', "{$this -> cn}\\InfoPagesController@not_found_page");
		
		// $this -> router -> uri('/uadpost/$alias', "{$this -> cn}\\UAdPostController@view_page");

		
		// $this -> router -> uri(
		// 	'/profile/orders/$utype/exclude-states', 
		// 	function($args) {
		// 		return app() -> utils -> redirect(
		// 			app() -> routes -> urlto("OrderController@orders_cur_user_page", ["utype" => $args["utype"]])
		// 		);
		// 	}
		// );
		

		$this -> router -> uri("/test/device-scanner", function($args){
			$device_scanner = new Tools\DeviceScanner();
			$devices = $device_scanner -> scan_range("192.168.68.2", "192.168.68.254");
			var_dump($device_scanner -> filter_by_status($devices, "normal"));
		});
	}

	protected function get_routes() {
		// $this -> router -> get(
		// 	[ "uadpost_id", "state" ], 
		// 	"{$this -> cn}\\UAdPostController@change_uadpost_state", 
		// 	"/profile/uadposts/change-state.html"
		// );
	}

	protected function post_routes() {
		$this -> router -> post(
			[ "email", "password", "password_again" ], 
			"{$this -> cn}\\AuthController@signup", 
			"/auth/signup"
		);
	}

	/**
	 * urlto, get url by action name [with arguments]
	 * @var String   $action_name - Action name, like a "HelloController@world_action"
	 * @var Array   $url_args - Assoc array with arguments (ONLY FOR REQUEST TYPE GET)
	 * 
	 * @return String  Url to action, ready for use
	 */
	public function urlto(String $action_name, Array $url_args = []) {
		$routes_map = $this -> router -> get_routes_map();
		$desired_action = "{$this -> cn}\\{$action_name}";
		foreach($routes_map["uri"] as $url => $action) {
			if($action == $desired_action) {
				foreach($url_args as $arg_name => $arg_val) {
					$url = str_replace("\${$arg_name}", $arg_val, $url);
				}

				return $url;
			}
		}

		foreach($routes_map["post"] as $url_pattern => $action){
			if($action == $desired_action) {
				list($url) = explode("?", $url_pattern); 
				return $url;
			}
		}

		foreach($routes_map["get"] as $url_pattern => $action){
			if($action == $desired_action) {
				list($url, $param_names) = explode("?", $url_pattern); 
				if(count($url_args)) {
					$param_names = explode("&", $param_names);
					$params = array_flip($param_names);
					foreach($params as $key => $val) {
						if(!isset($url_args[$key])) {
							continue;
						}

						$params[$key] = $url_args[$key];
					}
				}else{
					$params = [];
				}

				return $url . "?" . http_build_query($params);
			}
		}
	}
}