<?php

namespace ControlScripts;

use \SHServ\Entities\Device;

class SpotlightsScope extends \SHServ\Middleware\ControlScripts implements \SHServ\Implements\ControlScriptsInterface {
	protected $sync_map = [
		"connections" => [
			[
				["type" => "relay", "alias" => "spotlight_main_back_1", "channel" => 0],
				["type" => "button", "alias" => "buttons_backdoor", "channel" => 0],
			],

			[
				["type" => "relay", "alias" => "spotlight_main_back_2", "channel" => 0],
				["type" => "button", "alias" => "buttons_backdoor", "channel" => 1],
			],
			
			[
				["type" => "relay", "alias" => "spotlight_main_front_1", "channel" => 0],
				["type" => "button", "alias" => "buttons_backdoor", "channel" => 2],
			],
		]
	];

	public function register_events_handlers(): void {
		$this -> backdoor_btns_online();
		$this -> backdoor_btns_handlers();
	}

	public function register_actions_scripts(): void {
		$this -> add_action_script([
			"alias" => "spotlights_on",
			"name" => "All Spotlights On",
			"icon" => '<i class="ph ph-headlights"></i>',
			"description" => "Включить все прожекторы",
			"author" => "Eugene Sukhodolskiy"
		], function($params) {
			return $this -> all_spotlight_switch(true);
		});

		$this -> add_action_script([
			"alias" => "spotlights_off",
			"name" => "All Spotlights Off",
			"icon" => '<i class="ph ph-headlights"></i><i class="ph ph-x"></i>',
			"description" => "Выключить все прожекторы",
			"author" => "Eugene Sukhodolskiy"
		], function($params) {
			return $this -> all_spotlight_switch(false);
		});
	}

	public function register_regular_scripts(): void {
		$this -> add_regular_script([
			"alias" => "spotlights_by_time",
			"name" => "Spotlights by Time",
			"description" => "Управление уличными прожекторами по времени",
			"author" => "Eugene Sukhodolskiy"
		], function () {
			$tz = new \DateTimeZone('Europe/Kyiv');

			$now = new \DateTime('now', $tz);
			$from = new \DateTime('9:00', $tz);
			$to = new \DateTime('16:30', $tz);

			if ($now >= $from && $now <= $to) {
				$this -> all_spotlight_switch(false);
			}
		});
	}

	// ACTIONS
	
	protected function all_spotlight_switch(Bool $state = false): Array {
		$results = [];
		$btns_block_api = $this -> devices() -> by_alias("buttons_backdoor") -> device_api();
		$device_entries = $this -> helper() -> get_sync_entries_by_type($this -> sync_map, "relay");

		foreach($device_entries as $device_entry) { 
			$relay_api = $this -> devices() -> by_alias($device_entry["alias"]) -> device_api();
			$result = false;

			if($relay_api instanceof \SHServ\Tools\DeviceAPI\Relay) {
				$status = $relay_api -> get_status();

				if(isset($status["channels"][0]["state"]) and $status["channels"][0]["state"] != ($state ? "on" : "off")) {
					$result = $relay_api -> set_state($state);
				}
			}

			$results[$device_entry["alias"]] = $result;
			$this -> helper() -> sync_relay_to_btns($this -> sync_map, $device_entry["alias"]);
		}
		

		return [
			"devices" => $results
		];
	}

	// EVENTS HANDLERS

	protected function backdoor_btns_online(): void {
		$this -> add_event_handler("button@buttons_backdoor.online", function(Device $btns_block, Array $data) {
			$btns_block_api = $btns_block -> device_api();
			if($btns_block_api instanceof \SHServ\Tools\DeviceAPI\Button) {
				$btns_block_api -> set_channel_state("mute", 3);
			}

			$this -> helper() -> sync_btn_channels($this -> sync_map, $btns_block -> alias);
		});
	}

	protected function backdoor_btns_handlers(): void {
		$buttons_backdoor = $this -> helper() -> prepare_sync_map_by_alias($this -> sync_map, "buttons_backdoor");
		foreach($buttons_backdoor as $btn_channel => $entry) { 
			if($entry[0]["type"] != "relay") {
				continue;
			}

			$relay_alias = $entry[0]["alias"];

			$this -> add_event_handler("button@buttons_backdoor({$btn_channel}).press", function(Device $btns_block, Array $data) use ($btn_channel, $relay_alias) {
				$btns_block_api = $btns_block -> device_api();
				$relay_api = $this -> devices() -> by_alias($relay_alias) -> device_api();

				if($relay_api instanceof \SHServ\Tools\DeviceAPI\Relay and $btns_block_api instanceof \SHServ\Tools\DeviceAPI\Button) {
					$relay_api -> toggle_channel(0);

					$this -> helper() -> sync_relay_to_btn_channel($relay_api, $btns_block_api, 0, $btn_channel);
				}
			});
		}
	}
}