<?php

namespace SHServ\Tools\DeviceAPI;

class Hatch extends Base {
	public function get_state(): String | null {
		$status_response = $this -> get_status();

		if(($status_response["status"] ?? null) != "ok") {
			return null;
		}

		return $status_response['hatch']['state'] ?? null;
	}

	public function is_opened(): Bool | null {
		$state = $this -> get_state();

		return is_null($state) 
			? null 
			: $state == "open";
	}

	public function is_closed(): Bool | null {
		$state = $this -> get_state();

		return is_null($state) 
			? null 
			: $state == "closed";
	}

	public function is_opening(): Bool | null {
		$state = $this -> get_state();

		return is_null($state) 
			? null 
			: $state == "opening";
	}

	public function is_closing(): Bool {
		$state = $this -> get_state();

		return is_null($state) 
			? null 
			: $state == "closing";
	}

	public function open(int $percent = 100) {
		return $this -> post_action("open", [
			"percent" => max(0, $percent)
		]);
	}

	public function close(int $percent = 100) {
		return $this -> post_action("close", [
			"percent" => max(0, $percent)
		]);
	}
}
