<?php

namespace WP_AUX\Entities\PageTemplate;

use WHOOP\Entities\Elements\P;
use WP_AUX\Entities\AuxFramework\ElementClassesEntity;
use WP_AUX\Entities\AuxFramework\Partials\CtaHeaderButtonGroupEntity;
use WP_AUX\Entities\AuxFramework\Partials\ButtonGroupEntity;
use WP_AUX\Entities\PageTemplate\Traits\RenderTemplateTraits;
use WP_AUX\Entities\PageTemplateEntity;
use WP_AUX\Services\AuxFramework\ElementClassesService;
use WP_AUX\Services\HtmlService;
use WP_AUX\Services\PageTemplate\HeaderService;
use WP_AUX\Services\PageTemplate\StoryHeaderService;
use WP_AUX\Services\HeadService;

class HeaderEntity
{
  use RenderTemplateTraits;

  public PageTemplateEntity $pageTemplate;
  protected string $templateSlug = 'header';

  protected string $type = 'standard';
  protected ?string $superhead = '';
  protected ?string $subhead = '';
  protected ?string $excerpt = '';
  protected ?string $backgroundColor = '';
  protected ?string $mediaHeight = '';
  protected ?string $mediaType = '';
  protected ?string $headerStyle = '';
  protected ?array $videoOptions = [];
  protected ?string $videoFileUrl = '';
  protected ?string $videoTranscriptUrl = '';
  protected ?string $videoPoster = '';
  protected ?array $factoid = [];
  protected array $ctas = [];
  protected array $source = [];

  public function __construct(PageTemplateEntity $pageTemplate)
  {
    $this->pageTemplate = $pageTemplate;
  }

  public function setFromRequest(): void
  {
    if ($type = $this->pageTemplate->getSetting('header_type')) {
      $this->type = $type;
    }

    $this->backgroundColor = $this->pageTemplate->getSetting('background');
    $this->mediaHeight = $this->pageTemplate->getSetting('media_height');
    $this->superhead = $this->pageTemplate->getSetting('superheader');
    $this->subhead = $this->pageTemplate->getSetting('subheader');
    $this->excerpt = $this->pageTemplate->getSetting('excerpt');
    $this->mediaType = $this->pageTemplate->getSetting('media_type');
    $this->headerStyle = $this->pageTemplate->getSetting('header_style');
    $this->videoOptions = $this->pageTemplate->getSetting('aux_hero_page_header_video_options');

    if ($source = $this->pageTemplate->getSetting('source')) {
      $this->source = $source;
    }

    if ($ctas = $this->pageTemplate->getSetting('ctas')) {
      $this->ctas = $ctas;
    }

    if ($this->isMediaType('factoid')) {
      $this->factoid = $this->pageTemplate->getSetting('factoid');
    }
  }

  public function setType(string $type): self
  {
    $this->type = $type;

    return $this;
  }

  public function setSuperhead(string $superhead): self
  {
    $this->superhead = $superhead;

    return $this;
  }

  public function setSubhead(string $subhead): self
  {
    $this->subhead = $subhead;

    return $this;
  }

  public function setExcerpt(string $excerpt): self
  {
    $this->excerpt = $excerpt;

    return $this;
  }

  public function getBackgroundColor(): ?string
  {
    return $this->backgroundColor;
  }

  public function setBackgroundColor(string $backgroundColor): self
  {
    $this->backgroundColor = $backgroundColor;

    return $this;
  }

  public function getMediaHeight(): ?string
  {
    return $this->mediaHeight;
  }

  public function getHeaderStyle(): ?string
  {
    return $this->headerStyle;
  }

  public function setMediaType(string $mediaType): self
  {
    $this->mediaType = $mediaType;

    return $this;
  }

  public function getType(): string
  {
    return $this->type;
  }

  public function getMediaType(): ?string
  {
    return $this->mediaType;
  }

  public function getSource(): array
  {
    return $this->source;
  }

  public function getSuperhead(): ?string
  {
    return $this->superhead;
  }

  public function getSubhead(): ?string
  {
    return $this->subhead;
  }

  public function getExcerpt(): ?string
  {
    return $this->excerpt;
  }

  public function setFactoid(array $factoid): self
  {
    $this->factoid = $factoid;

    return $this;
  }

  public function isType(string $type): bool
  {
    return $this->type === $type;
  }

  public function isMediaType(string $mediaType): bool
  {
    return $this->mediaType === $mediaType;
  }

  public function getFactoid(): array
  {
    return $this->factoid;
  }

  public function getBaseClass(): string
  {
    switch ($this->type) {
      case 'hero':
      case 'landing':
        return 'hero';
      default:
        return 'page-header';
    }
  }

  public function getWrapperClasses(): ElementClassesEntity
  {
    return HeaderService::getWrapperClasses($this);
  }

  public function getDivider(): string
  {
    return HeaderService::getDivider($this);
  }

  public function isDarkBackground(): bool
  {
    return in_array($this->backgroundColor, ['maroon', 'charcoal']);
  }

  public function pageImageHtml(string $forceClass = ''): string
  {
    if (!has_post_thumbnail()) {
      return '';
    }

    if ($forceClass) {
      $class = $forceClass;
    } else {
      $class = $this->getBaseClass() . '__image';
    }

    return HeaderService::pageImageHtml($this->pageTemplate->getPostID(), $class);
  }

  public function heroMediaHtml(): string
  {

    // if video file field is empty, act like pageImageHtml above
    if(empty($this->videoOptions['page_hero_video_file'])) {
      if (!has_post_thumbnail()) {
        return '';
      } else {
        return HeaderService::pageImageHtml($this->pageTemplate->getPostID(), 'hero__image');
      }
    } else {
      // only load the JS if there's a video header
      HeadService::enqueueLocalScript('aux-video-page-header', 'js/aux-video-page-header.js');

      if(empty($this->videoOptions['page_hero_video_file'])) {
        // if no video file is uploaded, do nothing
        return '';
      } else {
        // if the video file field is not empty, render the video file's path and the transcript file's path with the following markup
        $this->videoFileUrl = $this->videoOptions['page_hero_video_file']['url'];

        if(empty($this->videoOptions['page_hero_video_transcript'])) {
          $this->videoTranscriptUrl = '';
        } else {
          $this->videoTranscriptUrl = $this->videoOptions['page_hero_video_transcript']['url'];
        }

        // if the user has provided a featured image, render as the video poster
        if(!has_post_thumbnail()) {
          $this->videoPoster = '';
        } else {
          $this->videoPoster = get_the_post_thumbnail_url();
        }


        $theHtml = '<div class="hero__video">
          <video aria-describedby="video-txt" autoplay="autoplay" height="549" id="myVideo" loop="loop" muted="muted" playsinline="" poster="' . $this->videoPoster . '" style="width: 100%; height: 100%;" width="1280">
            <source src="' . $this->videoFileUrl . '" />
              <div class="sr-only">
                <a href="' . $this->videoTranscriptUrl . '" id="video-txt">video contains no sound</a>
              </div>
              <p>Your browser does not support HTML video. Here is a <a href="' . $this->videoFileUrl . '">link to the video</a>.</p>
          </video>
        </div>
        <button aria-label="Pause video" class="hero__video-controls hero__video-controls--pause" id="toggleButton" onclick="togglePlayPause()" type="button"></button>';

        return $theHtml;
      }
    }
  }

  public function headingGroup(): string
  {
    $headingGroup = sprintf(
      '<div class="heading-group heading-group--display">%s</div>',
      HtmlService::formattedValue($this->superhead, '<span class="superhead">%s</span>') .
      the_title('<h1 class="entry-title">', '</h1>', false) .
      HtmlService::formattedValue($this->subhead, '<span class="subhead">%s</span>')
    );

    $headingGroup .= $this->headingExcerpt();

    return $headingGroup;
  }

  public function ctas(): ?ButtonGroupEntity
  {
    return HeaderService::ctas($this->ctas);
  }

  public function headingExcerpt(string $forceClass = ''): P|string
  {
    if ($forceClass) {
      $class = $forceClass;
    } else {
      $class = $this->getBaseClass() . '__description';
    }

    $paragraph = HtmlService::paragraph(
      $this->excerpt,
      $class,
    );

    return $paragraph;
  }

  public function renderFactoid(): void
  {
    get_template_part('template-parts/main-content/header-'.$this->type.'-factoid', $this->pageTemplate->getPostType(), ['header' => $this]);
  }
}
