<?php

namespace SHServ;

use \Fury\Modules\Router\Router;
use \Fury\Modules\ThinBuilder\ThinBuilder;
use \Fury\Modules\ErrorHandler\ErrorHandler;
use \SHServ\Factory\Factory;
use \SHServ\DevTools;

class App extends \Fury\Kernel\BaseApp{
	public $routes;
	public $router;
	public $events_handlers;
	public $error_handlers;
	public $thin_builder;
	public $console_flag;

	// CUSTOM
	public $utils;
	public $sessions;
	public $factory;
	public $devtools;

	public $control_scripts_instances = [];

	public function __construct(){
		parent::__construct();

		global $argv;
		$this -> console_flag = isset($argv) ? true : false;
		$this -> app_init();
	}

	public function app_init(){
		if(!$this -> console_flag) {
			$this -> error_handlers = new ErrorHandler();
		}
		
		\Fury\Modules\Template\Template::set_driver(new \Fury\Drivers\TemplateDriver());

		$this -> control_scripts_init();
		$this -> router_json_to_post_emulate();

		$this -> devtools = new DevTools();
		$this -> router = new Router();
		$this -> routes = new Routes($this -> router);
		$this -> thin_builder = new ThinBuilder(FCONF['db'], new \Fury\Drivers\ThinBuilderDriver(bootstrap()));
		$this -> events_handlers = new EventsHandlers();
		$this -> events_handlers -> handlers();

		// CUSTOM
		$this -> utils = new Utils();
		$this -> sessions = new Sessions();
		$this -> factory = new Factory();
	}

	public function root_folder(){
		list($root) = explode('SHServ', __DIR__);
		return $root;
	}

	protected function router_json_to_post_emulate() {
		$content_type = $_SERVER['CONTENT_TYPE'] ?? '';

		if (stripos($content_type, 'application/json') === false) {
	    return;
		}

		$raw_body = file_get_contents('php://input');
		$json_data = json_decode($raw_body, true);

		if (json_last_error() !== JSON_ERROR_NONE) {
	    return;
		}

		foreach($json_data as $param_name => $param_val) {
			$_POST[$param_name] = $param_val;
		}
	}

	public function control_scripts_init() {
		$scripts_dir = scandir(__DIR__ . "/../ControlScripts/");
		$scripts = array_filter($scripts_dir, function($item) {
			return !is_dir($item) and (pathinfo($item))["extension"] == "php";
		});

		foreach($scripts as $script_name) {
			$script_name = basename($script_name, ".php");
			$full_script_name = "\\ControlScripts\\{$script_name}";

			$script = new $full_script_name();
			$this -> control_scripts_instances[$script_name] = $script;
		}
	}
}

new App();