<?php

namespace SHServ\Middleware;

use \SHServ\Models\Devices;

abstract class ControlScripts {
	protected $devices_model;
	protected $regular_scripts;

	abstract protected function register_events_handlers(): void;
	abstract protected function register_regular_scripts(): void;

	public function __construct() {
		$this -> register_events_handlers();
		$this -> register_regular_scripts();
	}

	protected function add_event_handler(String $event_name, callable $handler): void {
		events() -> handler("app:{$event_name}", function(Array $params) use ($handler) {
			$handler($params["device"], $params["data"]);
		});
	}

	protected function devices(): Devices {
		if(!$this -> devices_model) {
			$this -> devices_model = new Devices();
		}

		return $this -> devices_model;
	}

	protected function add_regular_script(String $alias, callable $script): void {
		$this -> regular_scripts[$alias] = $script;
	}

	public function get_regular_scripts(): Array {
		return $this -> regular_scripts;
	}
}