<?php
declare(strict_types=1);

namespace WP_AUX\Entities\AuxFramework\Partials;

use WP_AUX\Enum\IconsEnum;
use WP_AUX\Enum\SharePlatformsEnum;
use WP_AUX\Enum\SocialLinkCollectionAlignmentEnum;
use WP_AUX\Enum\SocialLinkCollectionLayoutEnum;
use WP_AUX\Services\AuxFramework\ElementClassesService;use WP_AUX\Services\HtmlService;
use WP_AUX\Services\ShareService;

class SocialLinkCollectionEntity
{
  public static array $defaultPlaforms = [
    SharePlatformsEnum::Facebook,
    SharePlatformsEnum::X,
    SharePlatformsEnum::LinkedIn,
    SharePlatformsEnum::Email,
  ];

  protected ?string $heading = null;
  protected ?string $subjectUrl = null;
  protected ?string $subjectTitle = null;
  /**
   * @var SharePlatformsEnum[]
   */
  protected $platforms = [];
  protected bool $hasDivider = false;
  protected bool $isSlim = false;
  protected SocialLinkCollectionAlignmentEnum $alignment = SocialLinkCollectionAlignmentEnum::Standard;
  protected SocialLinkCollectionLayoutEnum $layout = SocialLinkCollectionLayoutEnum::Standard;

  public function __construct(?string $heading = null, ?string $url = null, ?string $title = null) {
    $this->heading = $heading;
    $this->subjectUrl = $url;
    $this->subjectTitle = $title;
  }

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

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

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

  public function addPlatform(string|SharePlatformsEnum $platform): self
  {
    if (is_string($platform)) {
      $platform = SharePlatformsEnum::tryFrom($platform);
    }

    $this->platforms[$platform->value] = $platform;

    return $this;
  }

  /**
   * @param string[]|SharePlatformsEnum[] $platforms
   */
  public function setPlatforms($platforms): self
  {
    $this->platforms = [];

    foreach ($platforms as $platform) {
      $this->addPlatform($platform);
    }

    return $this;
  }

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

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

  public function setAlignment(string|SocialLinkCollectionAlignmentEnum $alignment): self
  {
    if (is_string($alignment)) {
      $alignment = SocialLinkCollectionAlignmentEnum::tryFrom($alignment);
    }

    $this->alignment = $alignment;

    return $this;
  }

  public function setLayout(string|SocialLinkCollectionLayoutEnum $layout): self
  {
    if (is_string($layout)) {
      $layout = SocialLinkCollectionLayoutEnum::tryFrom($layout);
    }

    $this->layout = $layout;

    return $this;
  }

  public function __toString(): string
  {
    $collectionClasses = ElementClassesService::buildClasses('social-collection');

    if ($this->hasDivider) {
      $collectionClasses->addModifier('divider');
    }

    if ($this->isSlim) {
      $collectionClasses->addModifier('slim');
    }

    if ($this->alignment) {
      $collectionClasses->addModifier($this->alignment->value);
    }

    if ($this->layout) {
      $collectionClasses->addModifier($this->layout->value);
    }

    ob_start();
    ?>
    <div class="<?php echo $collectionClasses; ?>">
      <?php
      echo HtmlService::formattedValue(
        $this->heading,
        '<span class="social-collection__heading">%s</span>'
      );
      ?>
      <ul class="social-collection__list">
        <?php
        foreach ($this->platforms as $platform) {
          echo sprintf(
            '<li>%s</li>',
            ShareService::getShareLink(
              $platform,
              $this->subjectUrl,
              $this->subjectTitle
            )
          );
        }
        ?>
      </ul>
    </div>
    <?php
    return ob_get_clean();
  }

  public static function create(?string $heading = null, ?string $url = null, ?string $title = null): static
  {
    $collection = new static($heading, $url, $title);
    $collection->setPlatforms(static::$defaultPlaforms);

    return $collection;
  }

  public static function createLeftBlock(?string $heading = null, ?string $url = null, ?string $title = null): static
  {
    if (func_num_args() === 1) {
      $url = get_post_permalink();
      $title = get_the_title();
    }

    $collection = new static($heading, $url, $title);
    $collection
      ->setAlignment('left')
      ->setLayout('block')
      ->setPlatforms(static::$defaultPlaforms);

    return $collection;
  }

  public static function createRightBlock(?string $heading = null, ?string $url = null, ?string $title = null): static
  {
    $collection = new static($heading, $url, $title);
    $collection
      ->setAlignment('right')
      ->setLayout('block')
      ->setPlatforms(static::$defaultPlaforms);

    return $collection;
  }
}
