<?php
declare(strict_types=1);

namespace WP_AUX\Entities\AuxFramework\Partials;

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

class QuoteEntity
{
  protected string $content = '';
  protected string $type = 'blockquote';
  protected ?string $citeName = null;
  protected ?string $citeTitle = null;
  protected ?string $citeOrganization = null;
  protected ?bool $isLong = null;

  public function __construct(string $content) {
    $this->setContent($content);
  }

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

  /**
   * @param string $type 'blockquote' or 'pullquote'
   * @return $this
   */
  public function setType(string $type): self
  {
    $this->type = $type;
    return $this;
  }

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

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

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

  public function setCitation(array $citation): self
  {
    $this->setCiteName(@$citation['name'])
      ->setCiteTitle(@$citation['title'])
      ->setCiteOrganization(@$citation['organization']);

    return $this;
  }

  public function setIsLong(bool|int|string|null $isLong): self
  {
    $this->isLong = filter_var($isLong, FILTER_VALIDATE_BOOLEAN);
    return $this;
  }

  public function getContentParagraphs(): array
  {
    $content = str_replace('<br />', "\n", $this->content);
    $contentLines = explode("\n", $content);

    $paragraphs = [];
    foreach ($contentLines as $contentLine) {
      if (empty($contentLine)) {
        continue;
      }

      $paragraphs[] = '<p>' . $contentLine . '</p>';
    }

    return $paragraphs;
  }

  public function __toString(): string
  {
    if (empty($this->content)) {
      return '';
    }

    $figureClasses = ElementClassesService::buildClasses($this->type);

    $paragraphs = $this->getContentParagraphs();

    if (!isset($this->isLong)) {
      $long = count($paragraphs) > 1;
    } else {
      $long = $this->isLong;
    }

    if ($long) {
      $figureClasses->addModifier('long');
    }

    $citation = join([
      HtmlService::formattedValue($this->citeName, '<span class="name">%s</span>'),
      HtmlService::formattedValue($this->citeTitle, '<span class="title">%s</span>'),
      HtmlService::formattedValue($this->citeOrganization, '<span class="org">%s</span>'),
    ]);

    $caption = '';
    if (!empty($citation)) {
      $caption = sprintf('<figcaption class="%s__cite">%s</figcaption>', $this->type, $citation);
      $figureClasses->addModifier('has-cite');
    }

    ob_start();
    ?>
    <figure class="<?php echo $figureClasses; ?>">
      <blockquote class="<?php echo $this->type; ?>__content">
        <?php echo join($paragraphs); ?>
      </blockquote>
      <?php echo $caption; ?>
    </figure>
    <?php
    return ob_get_clean();
  }

  public static function createPullquote(string $content): static
  {
    return (new static($content))
      ->setType('pullquote');
  }

  public static function createBlockquote(string $content): static
  {
    return (new static($content))
      ->setType('blockquote');
  }
}
