<?php

namespace ControlScripts\Scopes;

use \SHServ\Entities\Device;

class LightHubScope extends \SHServ\Middleware\ControlScripts implements \SHServ\Implements\ControlScriptsInterface {

	use \ControlScripts\Common;

	public function register_sync_map(): void {
		$this -> register_global_device_sync_map();
	}
	
	public function register_events_handlers(): void {
		$this -> btn_on_online("master_room_btns", [2, 3]);
		$this -> btn_on_online("btns_hall2_1");
		$this -> btn_on_online("hall_secondary");
		$this -> btn_on_online("bed_btns_right_1");
		$this -> btn_on_online("bed_btns_left");
		$this -> btn_on_online("kitchen_buttons_1");
		$this -> btn_on_online("kitchen_buttons_2");
		$this -> btn_on_online("first_hall_buttons1", [3]);
		$this -> btn_on_online("first_hall_buttons2");
		$this -> set_btns_click_handlers("master_room_btns");
		$this -> set_btns_click_handlers("btns_hall2_1");
		$this -> set_btns_click_handlers("hall_secondary");
		$this -> set_btns_click_handlers("bed_btns_right_1");
		$this -> set_btns_click_handlers("bed_btns_left");
		$this -> set_btns_click_handlers("kitchen_buttons_1");
		$this -> set_btns_click_handlers("kitchen_buttons_2");
		$this -> set_btns_click_handlers("first_hall_buttons1");
		$this -> set_btns_click_handlers("first_hall_buttons2");

		


		$this -> btn_on_online("plants_room_btns", [1]);
		$this -> set_btns_click_handlers("plants_room_btns");
		
	}

	public function register_actions_scripts(): void {
		$this -> add_action_script([
			"alias" => "master_room_lamp_switcher",
			"name" => "Осн. свет в спальне",
			"icon" => '<i class="ph ph-lightbulb"></i>',
			"description" => "Включить/выключить основной свет в спальне",
			"author" => "Eugene Sukhodolskiy"
		], function($params) {
			return $this -> lamp_switch("light_hub_1", 1);
		});

		$this -> add_action_script([
			"alias" => "hallway2_lamp_switcher",
			"name" => "Осн. свет в холе, эт2",
			"icon" => '<i class="ph ph-lightbulb"></i>',
			"description" => "Включить/выключить основной свет в холе на втором этаже",
			"author" => "Eugene Sukhodolskiy"
		], function($params) {
			return $this -> lamp_switch("light_hub_1", 2);
		});

		$this -> add_action_script([
			"alias" => "bathroom2_lamp_switcher",
			"name" => "Осн. свет в ванной 2",
			"icon" => '<i class="ph ph-lightbulb"></i>',
			"description" => "Включить/выключить основной свет в ванной комнате на верху",
			"author" => "Eugene Sukhodolskiy"
		], function($params) {
			return $this -> lamp_switch("light_hub_1", 3);
		});

		// ---

		$this -> add_action_script([
			"alias" => "hall1_light_switcher",
			"name" => "Свет в прихожей",
			"icon" => '<i class="ph ph-lightbulb"></i>',
			"description" => "Включить/выключить свет в прихожей",
			"author" => "Eugene Sukhodolskiy"
		], function($params) {
			return 
				$this -> lamp_switch("fisrt_floor_big_relay", 3) and $this -> lamp_switch("fisrt_floor_big_relay", 4);
		});

		$this -> add_action_script([
			"alias" => "kitchen_light_switcher",
			"name" => "Полный свет на кухне",
			"icon" => '<i class="ph ph-lightbulb"></i>',
			"description" => "Включить/выключить весь имеющийся свет на кухне",
			"author" => "Eugene Sukhodolskiy"
		], function($params) {
			return 
				$this -> lamp_switch("fisrt_floor_big_relay", 0) and $this -> lamp_switch("fisrt_floor_big_relay", 1);
		});

		$this -> add_action_script([
			"alias" => "hatch_open",
			"name" => "Открыть люк",
			"icon" => '<i class="ph ph-arrow-up"></i>',
			"description" => "Открыть люк на кухне",
			"author" => "Eugene Sukhodolskiy"
		], function($params) {
			return 
				$this -> hatch_open("kitchen_hatch", 100);
		});

		$this -> add_action_script([
			"alias" => "hatch_close",
			"name" => "Закрыть люк",
			"icon" => '<i class="ph ph-arrow-down"></i>',
			"description" => "Закрыть люк на кухне",
			"author" => "Eugene Sukhodolskiy"
		], function($params) {
			return 
				$this -> hatch_close("kitchen_hatch", 100);
		});
	}

	public function register_regular_scripts(): void {
	}

	// ACTIONS
	
	protected function lamp_switch(String $relay_alias, int $channel): Array {
		$relay_api = $this -> devices() -> by_alias($relay_alias) -> device_api();

		$result = false;
		if($relay_api instanceof \SHServ\Tools\DeviceAPI\Relay) {
			$result = $relay_api -> toggle_channel($channel);
			$this -> helper() -> sync_relay_to_btns($this -> sync_map(), $relay_alias);
		}
		
		return [
			"result" => $result
		];
	}

	protected function hatch_do(String $hatch_alias, String $operation = "open", int $percent = 100): Array | bool {
		$hatch_api = $this -> devices() -> by_alias($hatch_alias) -> device_api();

		$result = false;
		if($hatch_api instanceof \SHServ\Tools\DeviceAPI\Hatch) {
			switch($operation) {
				case "open": $result = $hatch_api -> open($percent);
					break;
				case "close": $result = $hatch_api -> close($percent);
					break;
			}
		}
		
		return $result;
	}

	protected function hatch_open(String $hatch_alias, int $percent = 100) {
		return $this -> hatch_do($hatch_alias, "open", $percent);
	}

	protected function hatch_close(String $hatch_alias, int $percent = 100) {
		return $this -> hatch_do($hatch_alias, "close", $percent);
	}
	
}
