<?php

namespace modules\shelly\helpers;

/**
 * @class shelly_device_state
 * @description The universal device state object
 * @docs https://shelly-api-docs.shelly.cloud/cloud-control-api/communication-v2#get-devices-state
 */
class shelly_device_state
{
    /**
     * @var string $id
     * @description The device id
     */
    public string $id;
    /**
     * @var string $type
     * @description The device type
     */
    public string $type;
    /**
     * @var string $code
     * @description The device code
     */
    public string $code;
    /**
     * @var string $gen
     * @description The device generation
     */
    public string $gen;
    /**
     * @var int $online
     * @description The device online status
     * @values 0 | 1
     */
    public int $online;
    /**
     * @var object $status
     * @description The device status
     */
    public object $status;
    /**
     * @var object $settings
     * @description The device settings
     */
    public object $settings;

    /**
     * Populate the device state object with the provided data
     * @param array|object $data
     * @return self
     */
    public function populate(array|object $data): self
    {
        // If the data is an object, convert it to an array
        if (is_object($data)) {
            $data = (array)$data;
        }
        // Loop through the data and assign the values to the object properties
        foreach ($data as $key => $value) {
            if (property_exists($this, $key)) {
                $this->$key = $value;
            }
        }
        return $this;
    }
}