<?php
namespace nur\b\date;

use nur\A;
use nur\b\coll\BaseArray;
use nur\b\IllegalAccessException;

/**
 * Class Trange: une plage horaire identifiée par une heure de début et une
 * heure de fin (non incluse)
 */
class Trange extends BaseArray {
  /**
   * @return int|null le nombre d'unités par défaut d'une période, si la borne
   * $end n'est pas spécifiée. l'unité utilisée est celle de la borne $start
   */
  protected function TRANGE_SIZE(): ?int {
    return static::TRANGE_SIZE;
  } const TRANGE_SIZE = null;

  function __construct(Time $start, ?Time $end=null, ?array $data=null) {
    if ($end === null) {
      $size = $this->TRANGE_SIZE();
      if ($size !== null) $end = $start->addu($size);
    }
    if ($end === null) $end = Time::undef();
    if (!$end->after($start)) [$start, $end] = [$end, $start];
    $period = [$start, $end];
    A::update_n($period, $data);
    parent::__construct($period);
  }

  function start(): Time {
    return $this->data[0];
  }

  function end(): Time {
    return $this->data[1];
  }

  function __toString(): string {
    return strval($this->data[0])." - ".strval($this->data[1]);
  }

  function diff(): Time {
    $delay = new Delay($this->start());
    return $delay->until($this->end());
  }

  /** vérifier si cette plage est strictement située avant l'heure spécifiée */
  function before(Time $time): bool {
    if ($time->isNull() || $time->isUndef()) return false;
    $end = $this->end();
    if (!$end->isUndef()) return $end->before($time);
    $start = $this->start();
    return !$start->isUndef() && $start->before($time);
  }

  /** vérifier si cette plage contient l'heure spécifiée */
  function contains(Time $time): bool {
    if ($time->isNull() || $time->isUndef()) return false;
    $start = $this->start();
    if (!$start->isUndef() && !$start->before($time)) return false;
    $end = $this->end();
    if (!$end->isUndef() && $end->before($time)) return false;
    return true;
  }

  /** vérifier si cette plage est strictement située après l'heure spécifiée */
  function after(Time $time): bool {
    if ($time->isNull() || $time->isUndef()) return false;
    $start = $this->start();
    if (!$start->isUndef()) return !$start->before($time);
    $end = $this->end();
    return !$end->isUndef() && !$end->before($time);
  }

  #############################################################################
  const KEY_MAP = [
    "start" => 0,
    "end" => 1,
  ];

  function _has($key): bool {
    $key = A::get(self::KEY_MAP, $key, $key);
    return parent::_has($key);
  }

  function &_get($key, $default=null) {
    $key = A::get(self::KEY_MAP, $key, $key);
    return parent::_get($key, $default);
  }

  function _set($key, $value) {
    throw IllegalAccessException::immutable_object();
  }
  function _del($key) {
    throw IllegalAccessException::immutable_object();
  }

  function has($key): bool { return $this->_has($key); }
  function get($key, ?Time $default=null): ?Time { return $this->_get($key, $default); }
  function set($key, ?Time $value): self { return $this->_set($key, $value); }
  function add(?Time $value): self { return $this->_set(null, $value); }
  function del($key): self { return $this->_del($key); }
}