<?php

namespace modules\selfserve\traits;

require_once WD . '/modules/selfserve/helpers/selfserve_lane_state.php';

use modules\selfserve\helpers\selfserve_lane_state;

trait selfserve_lane_state_t
{
    /**
     * @enum selfserve_lane_state
     * @description The operational state of the lane, which can be one of the following values:
     * @values ENTRANCE_PORT_OPEN_QUEUED, ENTRANCE_PORT_OPEN, MACHINE_RELAY_ON_QUEUED, MACHINE_RELAY_ON, MACHINE_RELAY_OFF_QUEUED, MACHINE_RELAY_OFF, IN_WASH, EXIT_PORT_OPEN_QUEUED, EXIT_PORT_OPEN, IDLE, FAULT, MAINTENANCE, CLOSED
     * @var selfserve_lane_state $state
     */
    public selfserve_lane_state $state;

    /**
     * Get the current operational state of the self-serve lane
     * @return selfserve_lane_state The current operational state of the self-serve lane
     */
    public function getLaneState(): selfserve_lane_state
    {
        $state = $this->getLaneCache($this->id, self::CACHE_SELFSERVE_LANE_KEY_STATE, selfserve_lane_state::class);
        if (!empty($state)) {
            $this->state = $state;
        }
        else if (empty($this->state)) {
            // If not found in cache, default to MANUAL
            $this->state = selfserve_lane_state::IDLE;
            $this->setLaneCache($this->id, self::CACHE_SELFSERVE_LANE_KEY_STATE, $this->state);
        }
        return $this->state;
    }

    /**
     * Set the current operational state of the self-serve lane
     * @param selfserve_lane_state $state The new operational state of the self-serve lane
     * @return selfserve_lane_state_t
     */
    public function setLaneState(selfserve_lane_state $state): self
    {
        $this->state = $state;
        $this->setLaneCache($this->id, self::CACHE_SELFSERVE_LANE_KEY_STATE, $this->state);
        return $this;
    }
}