<?php

namespace modules\shelly\helpers;

use classes\shelly;
use Exception;

/**
 * @class shelly_device_switch
 * @description
 * @docs https://shelly-api-docs.shelly.cloud/cloud-control-api/communication-v2#control-a-switch
 */
class shelly_device_switch extends shelly_device_state
{
    /**
     * @var boolean $on
     * @description The output state
     * @values true | false
     */
    public bool $on;
    /**
     * @var int $channel
     * @description The channel number
     * @values 0 | 1 | 2 | 3
     * @default 0
     */
    public int $channel = 0;
    /**
     * @var int $toggle_after
     * @description After how many seconds, the state should be set to opposite the value of "on"
     * @values 1
     */
    public int $toggle_after = 1;

    /**
     * Send a request to the shelly API to switch the output
     * @param bool $on
     * @return array|object|null
     * @throws Exception
     */
    public function switch(bool $on): array|object|null
    {
        $parameters = [
            'id' => (string)$this->id,
            'channel' => (int)$this->channel,
            'on' => $on,
            'toggle_after' => (int)$this->toggle_after,
        ];
        return (new shelly())->sendPostRequest('/v2/devices/api/set/switch', $parameters);
    }
}