<?php

namespace SHServ;

class EventsHandlers{
	public function handlers(){
		events() -> handler('kernel:Bootstrap.ready_app', function(Array $params) {
			app() -> routes -> routes_init();
			if(!app() -> console_flag) {
				app() -> router -> start_routing();
			}
		});

		events() -> handler('kernel:CallControl.no_calls', function(Array $params) {
			if(app() -> console_flag) {
				return;
			}

			$uri = $_SERVER['REQUEST_URI'] ?? '';
			if(strpos($uri, '?') !== false) {
				list($uri) = explode('?', $uri);
			}

			$root = __DIR__ . '/../dist';
			$filePath = $root . $uri;

			if(
				(str_starts_with($uri, '/assets/') || $uri === '/favicon.ico')
				&& file_exists($filePath)
				&& is_file($filePath)
			) {
				$ext = pathinfo($filePath, PATHINFO_EXTENSION);
				$mimeTypes = [
					'js'    => 'application/javascript',
					'css'   => 'text/css',
					'png'   => 'image/png',
					'jpg'   => 'image/jpeg',
					'jpeg'  => 'image/jpeg',
					'gif'   => 'image/gif',
					'svg'   => 'image/svg+xml',
					'woff'  => 'font/woff',
					'woff2' => 'font/woff2',
					'ttf'   => 'font/ttf',
					'eot'   => 'application/vnd.ms-fontobject',
					'ico'   => 'image/x-icon',
					'json'  => 'application/json',
					'map'   => 'application/json',
				];
				header('Content-Type: ' . ($mimeTypes[$ext] ?? 'application/octet-stream'));
				header('Cache-Control: public, max-age=31536000, immutable');
				readfile($filePath);
				return;
			}

			http_response_code(404);
			echo "404 not found";
		});

		events() -> handler('app:online', function(Array $params) {
			$device = $params["device"];
			$data = $params["data"];
			$device -> device_ip = $data["device_ip"];
			$device -> connection_status = "active";
			$device -> update();
		});

		if(FCONF["devmode"]) {
			events() -> handler("module:Template.start_making", function(Array $params) {
				app() -> devtools -> add_template_to_map(
					$params["template_instance"],
					$params["template_name"]
				);
			});

			events() -> handler("module:Template.ready_template", function(Array $params) {
				app() -> devtools -> render_template_done(
					$params["template_name"]
				);
			});

			events() -> handler("kernel:CallControl.leading_call", function(Array $params) {
				app() -> devtools -> loging_action_call(
					is_string($params["action"]) ? $params["action"] : "anon",
					$params["type"],
					$params["params"]
				);
			});

			events() -> handler("kernel:CallControl.completed_call", function(Array $params) {
				app() -> devtools -> loging_action_time();
			});

			events() -> handler("kernel:Bootstrap.app_finished", function(Array $params){
				if(isset($_GET["devp"])) {
					app() -> devtools -> show();
				}
			});

			events() -> handler("module:ThinBuilder.ready_sql", function(Array $params){
				app() -> devtools -> loging_sql_query($params["sql"]);
			});

			events() -> handler("module:ThinBuilder.query", function(Array $params){
				app() -> devtools -> loging_sql_query_result();
			});
		}
	}
}
