<?php

namespace SHServ;

class Routes {

	use \SHServ\Routes\DevMode;
	use \SHServ\Routes\RESTAPI_v1;

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

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

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

	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();

		$this -> restapi_uri_routes();
		$this -> restapi_get_routes();
		$this -> restapi_post_routes();

		// DEV MODE
		if(FCONF["devmode"]) {
			$this -> devmode_uri_routes();
			$this -> devmode_get_routes();
			$this -> devmode_post_routes();
		}
	}

	// Examples
	// $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");
	protected function uri_routes() {
		$this -> router -> uri("/", function(){
			return "Smart home server. <br>Version 0.1 dev";
		});

		$this -> router -> uri("/cron/regular-scripts.html", "{$this -> cn}\\CronController@run_regular_cron_scripts");
		$this -> router -> uri("/cron/status-update-scanning.html", "{$this -> cn}\\CronController@status_update_scanning");

		
	}

	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"
		// );

		$this -> router -> post(
			[ "event_name", "device_id", "data"],
			"{$this -> cn}\\EventsController@new_event",
			"/events/new"
		);

	}

	/**
	 * 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);
			}
		}
	}
}