<?php
declare( strict_types=1 );

namespace WP_AUX\Entities\AuxFramework\Partials;

use WHOOP\Entities\Elements\Img;
use WP_AUX\Enum\CardsLayoutEnum;

/**
 * Entity handling the set(s) of cards within a card group
 *
 * Most card groups only contain one set. Optionally, a card group can contain
 * more than one set. Sets are defined by an 'identifier' which is appended
 * to the 'card-container' class if defined.
 *
 * @since 1.0.0
 */
class CardGroupSetEntity {
  /**
   * @var CardGroupEntity The parent card group of the set
   */
  protected CardGroupEntity $cardGroup;
  protected string $identifier = '';
  /**
   * @var CardEntity[]
   */
  protected array $cards = [];

  public function __construct(CardGroupEntity $cardGroup) {
    $this->cardGroup = $cardGroup;
  }

  /**
   * Set an identifier to distinguish a set within a group. The identifier is
   * appended to the 'card-container' class of the set <div>
   *
   * @since 1.0.0
   */
  public function setIdentifier(string $identifier): self
  {
    $this->identifier = $identifier;
    return $this;
  }

  /**
   * Get a count of the cards in the set
   * @since 1.0.0
   */
  public function cardsCount(): int
  {
    return count($this->cards);
  }

  /**
   * Add a fully-formed card entity to the set
   * @since 1.0.0
   */
  public function addCardEntity(CardEntity $cardEntity): self
  {
    $this->cards[] = $cardEntity;
    return $this;
  }

  /**
   * Add a card to the set
   * @since 1.0.0
   */
  public function addCard(
    ?string $heading,
    ?string $content,
    ?string $superheader,
    ?string $subheader,
    ?string $icon,
    array|null|string|Img $image = []
  ): ?CardEntity
  {
    $cardsCtaType = $this->cardGroup->getCardsCtaType();
    $cardsLayout = $this->cardGroup->getCardsLayout();

    if ('linked' === $cardsLayout->value) {
      $cardsCtaType = 'link';
    }

    $cardEntity = CardEntity::create($heading, $content)
      ->setType($cardsLayout->value)
      ->setSuperheader($superheader)
      ->setSubheader($subheader)
      ->setIcon($icon)
      ->setImage($image)
      ->setCtaType($cardsCtaType);

    $this->cards[] = $cardEntity;

    return $cardEntity;
  }

  /**
   * The set as an HTML string
   * @since 1.0.0
   */
  public function __toString(): string
  {
    if (empty($this->cards)) {
      return '';
    }

    $cardsCtaType = $this->cardGroup->getCardsCtaType();
    $cardsLayout = $this->cardGroup->getCardsLayout();
    $hasSectionIntro = !empty($this->cardGroup->getSectionIntro());

    $cardsContent = '';
    foreach ($this->cards as $card) {
      if (CardsLayoutEnum::Linked === $cardsLayout) {
        $cardsCtaType = 'link';
      }

      $card
        ->setHasIntro($hasSectionIntro)
        ->setCtaType($cardsCtaType);

      $cardsContent .= (string) $card;
    }

    $cardsCount = count($this->cards);

    $cardSetClass = 'card-container';
    if ($this->identifier) {
      $cardSetClass .= '__' . $this->identifier;
    }

    $output = sprintf(
      '<div class="%s" data-cards="%s">%s</div>',
      $cardSetClass,
      $cardsCount,
      $cardsContent
    );

    return $output;
  }
}
