<?php
namespace nur\v\html5;

use nur\A;
use nur\co;
use nur\session;
use nur\v\ly;
use nur\v\model\IBasicPage;
use nur\v\model\IComponent;
use nur\v\model\IPage;

class Html5BasicPageContainer extends Html5VanillaPageContainer {
  /** @return bool faut-il fermer automatiquement la session avant l'affichage? */
  protected function AUTOCLOSE_SESSION(): bool {
    return static::AUTOCLOSE_SESSION;
  } const AUTOCLOSE_SESSION = true;

  function initConfig(): void {
    parent::initConfig();
    A::update_nx($this->config, [
      "autoclose_session" => $this->AUTOCLOSE_SESSION(),
    ]);
  }

  function isAutocloseSession(): bool { return $this->config["autoclose_session"]; }

  protected function beforePrint(?IComponent $component): bool {
    if ($this->isAutocloseSession()) session::close();
    return parent::beforePrint($component);
  }

  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();
      $this->printContent();
      $this->printEndBody();
      $this->printEndHtml();
    } else {
      # si ce n'est pas une instance de IBasicPage, l'imprimer tel quel
      $this->haveOutput = true;
      co::_write([$page]);
    }
  }

  function printStartHtml(): void {
    $page = $this->page;
    $page->beforePrintStartHtml();
    parent::printStartHtml();
    $page->afterPrintStartHtml();
  }
  
  function printStartHead(): void {
    $page = $this->page;
    $page->beforePrintStartHead();
    parent::printStartHead();
    $page->afterPrintStartHead();
  }

  function printCss(): void {
    $page = $this->page;
    $page->beforePrintCssLinks();
    parent::printCssLinks();
    $page->afterPrintCssLinks();
    $page->beforePrintCss();
    parent::printCss();
    # $page est une instance de IPlugin et est traitée dans la méthode précédente
    #$page->printCss();
    $page->afterPrintCss();
  }

  function printJs(): void {
    $page = $this->page;
    $page->beforePrintJsLinks();
    parent::printJsLinks();
    $page->afterPrintJsLinks();
    $page->beforePrintJs();
    parent::printJs();
    # $page est une instance de IPlugin et est traitée dans la méthode précédente
    #$page->printJs();
    $page->afterPrintJs();
  }

  function printScript(): void {
    $page = $this->page;
    $page->beforePrintScript();
    $this->resolvePluginsScripts();
    # $page est une instance de IPlugin et est traitée dans la méthode précédente
    # cf Html5VanillaPageContainer#overrideSetup()
    #$this->beforeCapture();
    #$page->printScript();
    #$this->captureScript();
    #$this->beforeCapture();
    #$page->printJquery();
    #$this->captureJquery();
    $this->printMergedScripts();
    $page->afterPrintScript();
  }

  function printHeadTitle(): void {
    $page = $this->page;
    $page->beforePrintHeadTitle();
    $page->printHeadTitle();
    $page->afterPrintHeadTitle();
  }

  function printEndHead(): void {
    $page = $this->page;
    $page->beforePrintEndHead();
    parent::printEndHead();
    $page->afterPrintEndHead();
  }

  function printStartBody(): void {
    $page = $this->page;
    $page->beforePrintStartBody();
    parent::printStartBody();
    $page->afterPrintStartBody();
  }

  function printContent(): void {
    $page = $this->page;
    $page->beforePrintContent();
    $page->printContent();
    # s'assurer que le layout est correctement fermé
    ly::end();
    $page->afterPrintContent();
  }

  function printEndBody(): void {
    $page = $this->page;
    $page->beforePrintEndBody();
    parent::printEndBody();
    $page->afterPrintEndBody();
  }

  function printEndHtml(): void {
    $page = $this->page;
    $page->beforePrintEndHtml();
    parent::printEndHtml();
    $page->afterPrintEndHtml();
  }
}
