<?php
declare(strict_types=1);

namespace WP_AUX\Entities\AuxFramework\Partials;

use WP_AUX\Services\AuxFramework\ElementClassesService;
use WP_AUX\Services\HtmlService;

class QuickFactEntity
{
  protected ?string $heading;
  protected ?string $superheader = null;
  protected ?string $subheader = null;
  protected ?string $content;
  protected ?string $icon = null;
  protected ?array $link = null;
  protected ?string $citation = null;
  protected ?array $footnote = null;
  protected bool $onDark = false;

  public function __construct(?string $heading, ?string $content) {
    $this->heading = $heading;
    $this->content = $content;
  }

  public function setSuperheader(?string $superheader): self
  {
    $this->superheader = $superheader;
    return $this;
  }

  public function setSubheader(?string $subheader): self
  {
    $this->subheader = $subheader;
    return $this;
  }

  public function setIcon(?string $icon): self
  {
    $this->icon = $icon;
    return $this;
  }

  public function setCitation(?string $citation): self
  {
    $this->citation = $citation;
    return $this;
  }

  public function setFootnote(?array $footnote): self
  {
    $this->footnote = $footnote;
    return $this;
  }

  public function setOnDark(bool $onDark): self
  {
    $this->onDark = $onDark;
    return $this;
  }

  protected function footnoteContent(): string
  {
    $footnote = '';
    $footnoteContent = '';

    if ($this->citation) {
      $footnoteContent = $this->citation;
    } elseif ($this->footnote) {
      // don't try to render the footnote if the link field is empty
      if (empty($this->footnote['link']['url'])) {
        $footnoteContent = '';
      } else {
        $footnoteContent = sprintf('<a href="%s">%s</a>',
        $this->footnote['link']['url'],
        $this->footnote['link']['title'],
        );
      }

      if (!empty($this->footnote['year'])) {
        $footnoteContent .= ', ' . $this->footnote['year'];
      }

    }

    if ($footnoteContent) {
      $footnote = sprintf('<div class="footnote"><span>%s</span></div>', $footnoteContent);
    }

    return $footnote;
  }

  public function __toString(): string
  {
    $superheader = HtmlService::formattedValue($this->superheader, '<span class="superhead">%s</span>');
    $subheader = HtmlService::formattedValue($this->subheader, '<span class="subhead">%s</span>');
    $content = HtmlService::paragraph($this->content);

    $icon = '';
    if ($this->icon) {
      $icon = IconEntity::create($this->icon, false);
    }

    $footnote = $this->footnoteContent();

    $classes = ElementClassesService::buildClasses('standalone-factoid');
    if ($this->onDark) {
      $classes->addModifier('on-dark');
    }

    $string = <<<HTML
      <div class="standalone-factoid">
        <div class="quick-fact__heading">
          $superheader
          <span class="heading">{$this->heading} $icon</span>
          $subheader
        </div>
        $content
        $footnote
      </div>
    HTML;

    return $string;
  }

  public static function create(?string $heading, ?string $content): static
  {
    $quickFact = new static($heading, $content);

    return $quickFact;
  }
}
