<?php
namespace nur\v\plugins;

use nur\v\BasePlugin;
use nur\v\v;
use nur\v\vo;

class showmorePlugin extends BasePlugin {
  const HAVE_JQUERY = true;

  /** @var string classe du conteneur */
  const CONTAINER_CLASS = "showmore-container";
  /** @var string classe de l'invite à en savoir plus */
  const INVITE_CLASS = "showmore-invite";
  /** @var string classe du panneau caché par défaut */
  const INVITE_CONTENT = "En savoir plus...";
  const PANEL_CLASS = "showmore-panel";

  function __construct(?array $params=null) {
    $this->containerClass = $params["container_class"] ?? static::CONTAINER_CLASS;
    $this->inviteClass = $params["invite_class"] ?? self::INVITE_CLASS;
    $this->inviteContent = $params["invite_content"] ?? self::INVITE_CONTENT;
    $this->panelClass = $params["panel_class"] ?? self::PANEL_CLASS;
    $this->onShow = $params["on_show"] ?? null;
  }

  protected string $containerClass;

  protected string $inviteClass, $inviteContent;

  protected string $panelClass;

  protected ?string $onShow;

  function startc(): array {
    return v::sdiv(["class" => $this->containerClass]);
  }
  
  function invite($vs=null): array {
    $vs ??= $this->inviteContent;
    return v::a([
      "class" => $this->inviteClass,
      "href" => "#",
      $vs,
    ]);
  }
  
  function startp(): array {
    return v::sdiv([
      "class" => [$this->panelClass, "hidden"],
    ]);
  }
  
  function endp(): array {
    return ["</div>"];
  }

  function endc(): array {
    return ["</div>"];
  }
  
  function end(): array {
    return ["</div></div>"];
  }

  function printStartc(): void {
    vo::print($this->startc());
  }

  function printInvite($vs=null): void {
    vo::print($this->invite($vs));
  }

  function printStartp(): void {
    vo::print($this->startp());
  }

  function printEndp(): void {
    vo::print($this->endp());
  }

  function printEndc(): void {
    vo::print($this->endc());
  }

  function printEnd(): void {
    vo::print($this->end());
  }

  function printJquery(): void {
    ?>
<script type="text/javascript">
jQuery.noConflict()(function($) {
  $(".<?=self::INVITE_CLASS?>").click(function() {
    let $this = $(this);
    $this.addClass("hidden");
    let $container = $this.closest(".<?=self::CONTAINER_CLASS?>");
    let $panel = $container.find(".<?=self::PANEL_CLASS?>");
    $panel.removeClass("hidden");
    <?=$this->onShow?>
    return false;
  });
});
</script>
<?php
  }
}