<?php

namespace ControlScripts;

use \SHServ\Entities\Device;

class OfficeRoomScope extends \SHServ\Middleware\ControlScripts implements \SHServ\Implements\ControlScriptsInterface {
	public function register_events_handlers(): void {
	}

	public function register_actions_scripts(): void {
		$this -> add_action_script([
			"alias" => "computer_table_lamp_on",
			"name" => "ВКЛ комп. лампу",
			"icon" => '<i class="ph ph-lamp"></i><i class="ph ph-desktop"></i>',
			"description" => "Включить настольную компьютерную лампу",
			"author" => "Eugene Sukhodolskiy"
		], function($params) {
			return $this -> lamp_switch("computer_table_lamp", true);
		});

		$this -> add_action_script([
			"alias" => "computer_table_lamp_off",
			"name" => "ВЫКЛ комп. лампу",
			"icon" => '<i class="ph ph-lamp"></i><i class="ph ph-desktop"></i><i class="ph ph-x"></i>',
			"description" => "Выключить настольную компьютерную лампу",
			"author" => "Eugene Sukhodolskiy"
		], function($params) {
			return $this -> lamp_switch("computer_table_lamp", false);
		});

		$this -> add_action_script([
			"alias" => "craft_table_lamp_on",
			"name" => "ВКЛ крафт. лампу",
			"icon" => '<i class="ph ph-lamp"></i><i class="ph ph-wrench"></i>',
			"description" => "Включить настольную лампу на столе для крафта",
			"author" => "Eugene Sukhodolskiy"
		], function($params) {
			return $this -> lamp_switch("craft_table_lamp", true);
		});

		$this -> add_action_script([
			"alias" => "craft_table_lamp_off",
			"name" => "ВЫКЛ крафт. лампу",
			"icon" => '<i class="ph ph-lamp"></i><i class="ph ph-wrench"></i><i class="ph ph-x"></i>',
			"description" => "Выключить настольную лампу на столе для крафта",
			"author" => "Eugene Sukhodolskiy"
		], function($params) {
			return $this -> lamp_switch("craft_table_lamp", false);
		});
	}

	public function register_regular_scripts(): void {
	}

	// ACTIONS
	
	protected function lamp_switch(String $device_alias, Bool $state = false): Array {
		$relay_api = $this -> devices() -> by_alias($device_alias) -> device_api();
		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")) {
				return [
					"device" => $relay_api -> set_state($state)
				];
			}
		}
		
		return [
			"result" => false
		];
	}
}