<?php
namespace nur\v\html5;

use nur\co;
use nur\v\ly;
use nur\v\model\IBasicPage;
use nur\v\model\INavigablePage;
use nur\v\model\IPage;
use nur\v\navbar;
use nur\v\v;

class Html5NavigablePageContainer extends Html5BasicPageContainer {
  protected function overridePrint(IPage $page): void {
    if (!$this->beforePrint($page)) return;
    if ($page instanceof IBasicPage) {
      $this->doResolveConfig();
      $this->haveOutput = true;
      $this->printStartHtml();
      $this->printStartHead();
      $this->printCss();
      $this->printJs();
      $this->printScript();
      $this->printHeadTitle();
      $this->printEndHead();
      $this->printStartBody();
      if ($page instanceof INavigablePage) {
        $showNavigation = $page->navigationShowNavigation();
        $implementsOwnLayout = $page->navigationImplementsOwnLayout();
        $containerOptions = $page->CONTAINER_OPTIONS();
      } else {
        $showNavigation = false;
        $containerOptions = null;
      }
      if ($showNavigation) {
        $page->beforePrintStartNavigation();
        if ($implementsOwnLayout) $page->printStartNavigation();
        else $this->printStartNavigation($page->NAVBAR_OPTIONS());
        $page->afterPrintStartNavigation();

        $page->beforePrintNavigation();
        $page->printNavigation();
        $page->afterPrintNavigation();

        $page->beforePrintEndNavigation();
        if ($implementsOwnLayout) $page->printEndNavigation();
        else $this->printEndNavigation();
        $page->afterPrintEndNavigation();

        $page->beforePrintStartContainer();
        if ($implementsOwnLayout) $page->printStartContainer();
        else $this->printStartContainer($containerOptions);
        $page->afterPrintStartContainer();
      } else {
        $this->printStartContainer($containerOptions);
      }
      $this->printContent();
      # s'assurer que le layout est correctement fermé
      ly::end();
      if ($showNavigation) {
        $page->beforePrintEndContainer();
        if ($implementsOwnLayout) $page->printEndContainer();
        else $this->printEndContainer();
        $page->afterPrintEndContainer();
      } else {
        $this->printEndContainer();
      }
      $this->printEndBody();
      $this->printEndHtml();
    } else {
      # si ce n'est pas une instance de IBasicPage, l'imprimer tel quel
      $this->haveOutput = true;
      co::_write([$this->page]);
    }
  }

  function printStartNavigation(?array $options): void {
    navbar::start($options);
  }

  function printEndNavigation(): void {
    navbar::end();
  }

  function printStartContainer(?array $options): void {
    co::_print(v::sdiv(["class" => ["container"]]));
  }

  function printEndContainer(): void {
    co::_print(v::ediv());
  }
}
