<?php

namespace Plainwork\Ui\Component\Form;

use Plainwork\Http\Request;
use Plainwork\Ui\Component\Form\AbstractFieldset;

class TimeInput extends AbstractFieldset
{
    private ?int $value = 0;
    private int $step = 5;

    public function grab(Request $request): ?int
    {
        $name = $this->getName();
        $nameH = $name . '_h';
        $nameM = $name . '_m';

        $retH = $request->getPostParam($nameH);
        $retM = $request->getPostParam($nameM);

        if (null === $retM) {
            return null;
        }

        $ret = ((int) $retH) * 60 * 60 + ((int) $retM) * 60;

        return $ret;
    }

    public function setValue(?int $v): self
    {
        $this->value = $v;

        return $this;
    }

    public function getValue(): ?int
    {
        return $this->value;
    }

    public function getStep(): int
    {
        return $this->step;
    }

    public function setStep(int $step): self
    {
        $this->step = $step;

        return $this;
    }

    public function setToStart(): self
    {
        $this->setValue(0);

        return $this;
    }

    public function setToEnd(): self
    {
        $v = 23 * 60 * 60 + (60 - $this->getStep()) * 60;
        $this->setValue($v);

        return $this;
    }
}
