<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

declare(strict_types = 1);

namespace Kufer\Web\Classes\Dbfilter\RegisteredCall;

use Kufer\Web\Classes\Dbfilter\AbstractSQLFilter;
use Kufer\Web\Classes\Dbfilter\SQLFilterUtils;


class RegisteredDBCallTimeNoDateTime extends RegisteredDBCallString
{
  public function getType(): int
  {
    return \Kufer\Web\Classes\Dbfilter\AbstractSQLFilter::TYPE_TIME_NO_DATETIME;
  }

  public function getCapabilities(): int
  {
    return static::SUPPORTS_EQUALS
      | static::SUPPORTS_EQUALS_DEFAULT
      | static::SUPPORTS_GREATER
      | static::SUPPORTS_GREATER_DEFAULT
      | static::SUPPORTS_LESS
      | static::SUPPORTS_LESS_DEFAULT;
  }

  public function equals(\Kufer\Web\Classes\Dbfilter\AbstractSQLFilter $filter, $value): callable
  {
    return $this->equalsDefault($filter, $value, $this->default);
  }

  public function equalsDefault(AbstractSQLFilter $filter, $value, $default): callable
  {
    return parent::equalsDefault($filter, $this->normalize($value), $this->normalize($default));
  }

  public function less(\Kufer\Web\Classes\Dbfilter\AbstractSQLFilter $filter, $value): callable
  {
    return $this->lessDefault($filter, $value, $this->default);
  }

  public function lessDefault(AbstractSQLFilter $filter, $value, $default): callable
  {
    return parent::lessDefault($filter, $this->normalize($value), $this->normalize($default));
  }

  public function greater(\Kufer\Web\Classes\Dbfilter\AbstractSQLFilter $filter, $value): callable
  {
    return $this->greaterDefault($filter, $value, $this->default);
  }

  public function greaterDefault(AbstractSQLFilter $filter, $value, $default): callable
  {
    return parent::greaterDefault($filter, $this->normalize($value), $this->normalize($default));
  }

  private function normalize(string $time): string
  {
    if ($time === '') {
      return '00:00';
    }

    return \DateTimeOperations::getValidTimeString(\ArrayOperations::lastValue(explode(' ', $time)));
  }
}