<?php

namespace ControlScripts;

trait Common {
	public function register_global_device_sync_map() {
		$this -> add_sync_connection([
			["type" => "relay", "alias" => "spotlight_main_back_1", "channel" => 0],
			["type" => "button", "alias" => "buttons_backdoor", "channel" => 0],
			["type" => "button", "alias" => "master_door_btns", "channel" => 0],
		]);

		$this -> add_sync_connection([
			["type" => "relay", "alias" => "spotlight_main_back_2", "channel" => 0],
			["type" => "button", "alias" => "buttons_backdoor", "channel" => 1],
			["type" => "button", "alias" => "master_door_btns", "channel" => 1],
		]);

		$this -> add_sync_connection([
			["type" => "relay", "alias" => "spotlight_main_front_1", "channel" => 0],
			["type" => "button", "alias" => "buttons_backdoor", "channel" => 2],
			["type" => "button", "alias" => "master_door_btns", "channel" => 2],
		]);

		$this -> add_sync_connection([
			["type" => "relay", "alias" => "light_hub_1", "channel" => 0],
			["type" => "button", "alias" => "buttons_office_room", "channel" => 3],
		]);

		$this -> add_sync_connection([
			["type" => "relay", "alias" => "craft_table_lamp", "channel" => 0],
			["type" => "button", "alias" => "buttons_office_room", "channel" => 2],
		]);

		$this -> add_sync_connection([
			["type" => "relay", "alias" => "computer_table_lamp", "channel" => 0],
			["type" => "button", "alias" => "buttons_office_room", "channel" => 1],
		]);

		$this -> add_sync_connection([
			["type" => "relay", "alias" => "light_hub_1", "channel" => 2],
			["type" => "button", "alias" => "buttons_office_room", "channel" => 0],
			["type" => "button", "alias" => "master_room_btns", "channel" => 1],
			["type" => "button", "alias" => "btns_hall2_1", "channel" => 0],
		]);

		$this -> add_sync_connection([
			["type" => "relay", "alias" => "light_hub_1", "channel" => 1],
			["type" => "button", "alias" => "master_room_btns", "channel" => 0],
		]);
	}

	public function btn_on_online(String $alias, Array $muted = []): void {
		$this -> add_event_handler("button@{$alias}.online", function(Device $btns_block, Array $data) use ($muted) {
			$btns_block_api = $btns_block -> device_api();
			if($btns_block_api instanceof \SHServ\Tools\DeviceAPI\Button) {
				foreach($muted as $ch) {
					$btns_block_api -> set_channel_state("mute", $ch);
				}
			}

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

	public function set_btns_click_handlers($alias): void {
		$self = $this;
		$buttons = $this -> helper() -> prepare_sync_map_by_alias($this -> sync_map(), $alias);

		foreach($buttons as $btn_channel => $entry) { 
			if($entry[0]["type"] != "relay") {
				continue;
			}

			$relay_alias = $entry[0]["alias"];
			$relay_channel = $entry[0]["channel"];
			
			$this -> add_event_handler("button@{$alias}({$btn_channel}).press", function(Device $btns_block, Array $data) use ($self, $btn_channel, $relay_alias, $relay_channel) {
				$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($relay_channel);
					$this -> helper() -> sync_relay_to_btns($this -> sync_map(), $relay_alias);
				}
			});
		}
	}
}
