<?php
namespace nur\v\vp;

use nur\authz;
use nur\b\authnz\IAuthzUser;
use nur\config;
use nur\v\base\TNavigablePage;
use nur\v\fo;
use nur\v\icon;
use nur\v\model\INavigablePage;
use nur\v\navbar;
use nur\v\nb;
use nur\v\v;

/**
 * Class NavigablePage: un page navigable authentifiée
 */
class NavigablePage extends AInitAuthzPage implements INavigablePage {
  use TNavigablePage;

  const NAVBAR_OPTIONS = null;
  const NAVIGATION_SHOW_NAVIGATION = true;
  const NAVIGATION_IMPLEMENTS_OWN_LAYOUT = false;
  const CONTAINER_OPTIONS = null;

  const REQUIRE_AUTH = true;
  const REQUIRE_AUTHZ = true;
  const REQUIRE_ROLE = null;
  const REQUIRE_PERM = null;

  function afterConfig(): void {
    # initialiser la session avant setup. ainsi, dans les fonction beforeSetup(),
    # setup() et afterSetup(), la session est disponible
    $this->ensureAuthOrRedirect(static::REQUIRE_AUTH, static::REQUIRE_AUTHZ, static::REQUIRE_ROLE, static::REQUIRE_PERM);
    parent::afterConfig();
  }

  /** @return string|array */
  protected function MENU() {
    return static::MENU;
  } const MENU = "menu";
  protected function MENU_SELECT(): ?string {
    return static::MENU_SELECT;
  } const MENU_SELECT = null;
  const MENU_LOGOUT = true;

  const MENU_SULOGIN = true;

  protected function getAuthzNbtext(IAuthzUser $user): array {
    $username = $user->getUsername();
    $role = $user->getRole();
    $profile = config::get_profile();
    return nb::text([
      "Connecté en tant que ", v::b($username),
      v::if($role !== null, [" avec le rôle ", v::b($role)]),
      " dans le profil ", v::b($profile),
    ]);
  }

  protected function getLoginNblink(): array {
    return nb::link($this->getLoginUrl(), icon::login("Se connecter"), [
      "class" => "login",
      "accesskey" => "r",
    ]);
  }

  protected function getLogoutNblink(): array {
    return nb::link($this->getLogoutUrl(), icon::logout("Se déconnecter"), [
      "class" => "logout",
      "accesskey" => "z",
    ]);
  }

  protected function printSuloginMenu(): void {
    $am = authz::manager();
    if ($this->SULOGIN_ALLOWED() && ($am->isSulogin() || authz::get()->isPerm("sulogin"))) {
      navbar::nav(["align" => "right"]);
      fo::start([
        "type" => "navbar",
        "method" => "post",
        "action" => $this->getMainUrl(),
        "class" => "navbar-form",
      ]);
      fo::text("!Chuser ", "su!", null, [
        "style" => "width: 5rem; padding: 6px;",
        "accesskey" => "x",
      ]);
      fo::end();
      navbar::endnav();
    }
  }

  protected function printLogoutMenu(): void {
    if (authz::is_auth()) {
      $user = authz::get();
      navbar::nav(["align" => "right"], [
        nb::menu(icon::user($user->getShortName()), [
          $this->getAuthzNbtext($user),
          $this->getLogoutNblink(),
        ]),
      ]);
    }
  }

  function beforePrintNavigation(): void {
    $menu = $this->MENU();
    if ($menu !== null && !is_array($menu)) $menu = config::k($menu);
    if ($menu) {
      $selectedId = $this->MENU_SELECT();
      if ($selectedId === null) $selectedId = static::class;

      $mm = navbar::menu_manager();
      $mm->init($menu, $selectedId);
      ["brand" => $brand, "navbar_items" => $navbarItems,
      ] = $mm->get();
      if ($brand) navbar::brand($brand);
      if ($navbarItems) navbar::nav(null, $navbarItems);
    }
  }

  function afterPrintNavigation(): void {
    if (static::MENU_LOGOUT) $this->printLogoutMenu();
    if (static::MENU_SULOGIN) $this->printSuloginMenu();
  }
}