<?php
namespace nur\v;

use nur\str;
use nur\v\base\RouteManager;
use nur\v\model\IPage;
use nur\v\model\IRouteManager;

/**
 * Class route: gestion globale des routes
 */
class route {
  const MANAGER_CLASS = RouteManager::class;

  protected static $manager_class;

  /**
   * spécifier la classe à utiliser pour instancier le gestionnaire de routes
   *
   * cette méthode *doit* être appelée avant d'appeler la méthode
   * {@link manager()}
   */
  static final function set_manager_class(string $manager_class) {
    self::$manager_class = $manager_class;
  }

  /** @var IRouteManager */
  protected static $manager;

  static final function set_manager(IRouteManager $manager) {
    self::$manager = $manager;
  }

  /** obtenir l'instance globale de gestionnaire de route */
  static final function manager(): IRouteManager {
    if (self::$manager === null) {
      $manager_class = self::$manager_class;
      if ($manager_class === null) $manager_class = static::MANAGER_CLASS;
      self::$manager = new $manager_class();
    }
    return self::$manager;
  }

  static final function __callStatic($name, $args) {
    $name = str::us2camel($name);
    return call_user_func_array([self::manager(), $name], $args);
  }

  #############################################################################

  const MODE_EXACT = IRouteManager::MODE_EXACT;
  const MODE_PREFIX = IRouteManager::MODE_PREFIX;
  const MODE_PACKAGE = IRouteManager::MODE_PACKAGE;
  const MODE_PACKAGE2 = IRouteManager::MODE_PACKAGE2;

  static final function add(array ...$routes): void { self::manager()->addRoute(...$routes); }
  static final function set_error($page): void { self::manager()->setErrorPage($page); }

  static final function get_page(?string $path=null): IPage { return self::manager()->getPage($path); }
  static final function get_path($page): string { return self::manager()->getPath($page); }
}
