<?php

namespace modules\selfserve\traits;
require_once WD . '/modules/selfserve/helpers/selfserve_lane_status.php';
require_once WD . '/modules/selfserve/classes/selfserve_lane.php';
use modules\selfserve\classes\selfserve_lane;

trait selfserve_lane_timer_t
{
    const DEFAULT_WASH_START_TIME = null;
    /**
     * The timestamp of the start of the current wash cycle
     * @var int|null $wash_start_time
     */
    public ?int $wash_start_time = null;
    /**
     * Get the wash start time
     * @return int|null The timestamp of the start of the current wash cycle, or null if not set
     */
    public function getWashStartTime(): ?int
    {
        $wash_start_time = $this->getLaneCache($this->id, self::CACHE_SELFSERVE_LANE_KEY_WASH_START_TIME);
        if (!empty($wash_start_time)) {
            $this->wash_start_time = $wash_start_time;
        }
        return $this->wash_start_time;
    }
    /**
     * Set the wash start time
     * @param int|null $timestamp The timestamp of the start of the current wash cycle, or null to clear
     * @return selfserve_lane_timer_t|selfserve_lane
     */
    public function setWashStartTime(?int $timestamp): self
    {
        $this->wash_start_time = $timestamp;
        $this->setLaneCache($this->id, self::CACHE_SELFSERVE_LANE_KEY_WASH_START_TIME, $this->wash_start_time);
        return $this;
    }

    /**
     * Get the elapsed time since the wash started
     * @return int|null The elapsed time in seconds since the wash started, or null if wash has not started
     */
    public function getElapsedWashTime(): ?int
    {
        $wash_start_time = $this->getWashStartTime();
        if ($wash_start_time === null) {
            return null; // Wash has not started
        }
        return time() - $wash_start_time;
    }
}