<?php
declare(strict_types=1);

namespace WP_AUX\Entities\AuxFramework\Partials;

use WP_AUX\Entities\AuxFramework\Partials\Traits\SectionTraits;
use WP_AUX\Services\AuxCF\BlocksService;
use WP_AUX\Services\HtmlService;

/**
 * AUX slab entity
 * @since 1.0.1
 */
class SlabEntity
{
  use SectionTraits;

  protected ?string $icon = '';
  protected ?string $heading = '';
  protected ?string $superheader = '';
  protected ?string $subheader = '';
  protected ?string $description = '';
  protected array $cta = [];
  protected bool $wrapHeading = false;

  /**
   * Set the icon for the slab
   * @since 1.0.1
   */
  public function setIcon(?string $icon): static
  {
    $this->icon = $icon;
    return $this;
  }

  /**
   * Set the CTA link array for the slab
   * @since 1.0.1
   */
  public function setCta(array $cta): static
  {
    $this->cta = $cta;
    return $this;
  }

  /**
   * Set whether or not to wrap the heading in a div with class "{$this->componentClass}__intro"
   * @since 1.0.1
   */
  public function setWrapHeading(bool $wrapHeading): static
  {
    $this->wrapHeading = $wrapHeading;
    return $this;
  }

  /**
   * Set heading text for the slab
   * @since 1.0.1
   */
  public function setHeading(?string $heading): static
  {
    $this->heading = $heading;
    return $this;
  }

  /**
   * Set superheader text for the slab
   * @since 1.0.1
   */
  public function setSuperheader(?string $superheader): static
  {
    $this->superheader = $superheader;
    return $this;
  }

  /**
   * Set subheader text for the slab
   * @since 1.0.1
   */
  public function setSubheader(?string $subheader): static
  {
    $this->subheader = $subheader;
    return $this;
  }

  /**
   * Set description text for the slab
   * @since 1.0.1
   */
  public function setDescription(?string $description): static
  {
    $this->description = $description;
    return $this;
  }

  public function __toString(): string
  {
    /**
     * If the section is empty, then don't render anything.
     */
    if (empty($this->content)) {
      return '';
    }

    /**
     * Prepare the section's attributes and add the base section classes and ID (if provided)
     */
    $sectionAttributes = clone $this->sectionAttributes;
    $sectionAttributes->addClass((string) $this->sectionClasses);

    if ($this->id) {
      $sectionAttributes->setAttribute('id', esc_attr($this->id));
    }

    /**
     * Prepare the component's attributes and add the base component classes
     */
    $componentAttributes = clone $this->componentAttributes;
    $componentAttributes->addClass((string) $this->componentClasses);

    $icon = ($this->icon) ? IconEntity::create($this->icon) : '';

    $ctaLink = '';
    if ($this->cta) {
      $ctaLink = sprintf('<a href="%s" class="btn btn--primary">%s</a>',
        $this->cta['url'],
        $this->cta['title']
      );
    }

    // Create heading group
    $headingGroup = HeadingGroupEntity::create($this->heading)
      ->setIsFeature(true)
      ->setSuperheader($this->superheader)
      ->setSubheader($this->subheader);

    $divider = $this->icon ? '' : '<span class="divider-hash gray-300 divider--mini"></span>';

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

    $heading = <<<HTML
      <div class="slab-heading">
        $icon
        $headingGroup
        $divider
        $description
        $ctaLink
      </div>
    HTML;

    if ($this->wrapHeading) {
      $heading = <<<HTML
        <div class="{$this->componentClass}__intro">
          $heading
        </div>
      HTML;
    }

    $output = <<<HTML
      <section {$sectionAttributes}>
        <div {$componentAttributes}>
          $heading
          {$this->content}
        </div>
      </section>
    HTML;

    return $output;
  }
}
