<?php
declare(strict_types=1);

namespace WP_AUX\Entities\AuxFramework\Partials;

use WHOOP\Entities\Elements\Div;
use WP_AUX\Services\HtmlService;
use WP_AUX\Services\AuxFramework\ElementClassesService;

/**
 * The AUX section intro section intro
 * @since 1.0.0
 */
class SectionIntroEntity
{
  protected ?HeadingGroupEntity $headingGroup = null;
  protected ?string $description = null;
  protected CtaButtonGroupEntity $ctaButtonGroup;
  protected ?string $style = null;

  public function __construct() {
    $this->ctaButtonGroup = CtaButtonGroupEntity::create();
  }

  public function setDescription(?string $description): static
  {
    $this->description = $description;
    return $this;
  }

  public function setStyle(?string $style): static
  {
    $this->style = $style;
    return $this;
  }

  /**
   * Add a header to the section intro
   *
   * Heading groups will not display without header text content. Section intros
   * designed to have a heading will not display without one.
   *
   * @since 1.0.0
   */
  public function addHeader(?string $headerText, ?string $headerUrl = null, ?string $superheader = null, ?string $subheader = null): HeadingGroupEntity
  {
    $this->headingGroup = HeadingGroupEntity::create(
      $headerText,
      $headerUrl
    );

    if ($superheader) {
      $this->headingGroup->setSuperheader($superheader);
    }

    if ($subheader) {
      $this->headingGroup->setSubheader($subheader);
    }

    return $this->headingGroup;
  }

  /**
   * Get if the section intro has the required content to render
   * @since 1.0.1
   */
  public function willRender(): bool
  {
    return !empty($this->headingGroup->willRender());
  }

  /**
   * Add a CTA to the section intro
   * @since 1.0.0
   */
  public function addCta(array|string $link): self
  {
    if (is_string($link) && !empty($link)) {
      $link = [
        'url' => $link,
      ];
    }

    if (is_array($link)) {
      $this->ctaButtonGroup->addButton($link);
    }

    return $this;
  }

  public function __toString(): string
  {
    $headingGroupContent = '';

    if ($this->headingGroup) {
      /**
       * If the heading group won't be rendering, then there is no section intro.
       * Heading groups will not render if there is no main header content.
       */
      if (!$this->headingGroup->willRender()) {
        return '';
      }

      $headingGroupContent = (string) $this->headingGroup;
    }

    $ctaContent = HtmlService::formattedValue((string) $this->ctaButtonGroup, '<div class="section-intro__cta">%s</div>');

    $descriptionContent = HtmlService::formattedValue($this->description, '<p class="section-intro__description">%s</p>');

    $sectionClasses = ElementClassesService::buildClasses('section-intro');
    if ($this->style && 'default' !== $this->style) {
      $sectionClasses->addModifier($this->style);
    }
    if (empty($ctaContent) && empty($descriptionContent)) {
      $sectionClasses->addModifier('center');
    }

    $html = <<<HTML
    <div class="$sectionClasses">
      <div class="section-intro__container">
        <div class="section-intro__content">
          $headingGroupContent
          $descriptionContent
        </div>
        $ctaContent
      </div>
    </div>
HTML;


    return $html;
  }

  /**
   * Create an AUX section intro
   * @since 1.0.0
   */
  static function create(): static
  {
    $sectionIntro = new static();

    return $sectionIntro;
  }
}
