<?php
namespace nur\v;

use nur\co;
use nur\str;
use nur\v\html5\Html5FormManager;
use nur\v\model\IFormManager;

/**
 * Class fo: gestion globale des formulaires
 */
class fo {
  const MANAGER_CLASS = Html5FormManager::class;

  protected static $manager_class;

  /**
   * spécifier la classe à utiliser pour instancier le gestionnaire de
   * formulaires
   *
   * 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 IFormManager */
  protected static $manager;

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

  /** obtenir l'instance globale de gestionnaire de formulaire */
  static final function manager(): IFormManager {
    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);
  }

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

  static final function reset_manager($options=null, ?array $schema=null): void { self::manager()->resetManager($options, $schema);}

  static final function push($options=null, ?array $schema=null): void { self::manager()->push($options, $schema); }
  static final function pop(): void { self::manager()->pop(); }

  static final function started(): bool { return self::manager()->started(); }
  static final function start($options=null, ?array $schema=null): void {
    co::_write(self::manager()->start($options, $schema));
  }
  static final function end(): void {
    co::_print(self::manager()->end());
  }

  static final function section($options=null): void {
    co::_write(self::manager()->section($options));
  }
  static final function end_section(): void {
    co::_write(self::manager()->endSection());
  }

  static final function group($options=null): void {
    co::_write(self::manager()->group($options));
  }
  static final function end_group(): void {
    co::_write(self::manager()->endGroup());
  }

  static final function fixed($label, string $name, $value, ?array $options=null): void {
    co::_write(self::manager()->fixed($label, $name, $value, $options));
  }

  static final function hidden(string $name, $value, ?array $options=null): void {
    co::_write(self::manager()->hidden($name, $value, $options));
  }
  static final function hiddens(array $values, string ...$names): void {
    co::_write(self::manager()->hiddens($values, ...$names));
  }

  static final function text($label, string $name, $value, ?array $options=null): void {
    co::_write(self::manager()->text($label, $name, $value, $options));
  }
  static final function texts($label, array $values, string ...$names): void {
    co::_write(self::manager()->texts($label, $values, ...$names));
  }

  static final function password($label, string $name, $value, ?array $options=null): void {
    co::_write(self::manager()->password($label, $name, $value, $options));
  }

  static final function select($label, string $name, $value, ?array $options=null): void {
    co::_write(self::manager()->select($label, $name, $value, $options));
  }

  static final function checkbox($text, string $name, $value, ?bool $checked=null, ?array $options=null): void {
    co::_write(self::manager()->checkbox($text, $name, $value, $checked, $options));
  }
  static final function checkboxes($label, string $name, $values, ?array $options=null): void {
    co::_write(self::manager()->checkboxes($label, $name, $values, $options));
  }

  static final function radiobutton($text, string $name, $value, ?bool $checked=null, ?array $options=null): void {
    co::_write(self::manager()->radiobutton($text, $name, $value, $checked, $options));
  }
  static final function radiobuttons($label, string $name, $value, ?array $options=null): void {
    co::_write(self::manager()->radiobuttons($label, $name, $value, $options));
  }

  static final function textarea($label, string $name, $value, ?array $options=null): void {
    co::_write(self::manager()->textarea($label, $name, $value, $options));
  }

  static final function file($label, string $name, ?array $options=null): void {
    co::_write(self::manager()->file($label, $name, $options));
  }

  static final function submit($options=null): void {
    co::_write(self::manager()->submit($options));
  }

  static final function reset($options=null): void {
    co::_write(self::manager()->reset($options));
  }
}