<?php
declare(strict_types=1);

namespace WP_AUX\Entities\AuxFramework\Partials;

use WP_AUX\Services\HtmlService;

class LinkedCardEntity
{
  protected ?string $heading = null;
  protected ?string $superheader = null;
  protected ?string $subheader = null;
  protected ?string $content = null;
  protected ?array $link = null;
  protected ?string $icon = null;
  protected ?array $image = null;

  public function __construct(array $link, ?string $heading, ?string $content = null) {
    $this->link = $link;
    $this->heading = $heading;
    $this->content = $content;
  }

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

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

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

  public function setImage(?array $image): self
  {
    $this->image = $image;
    return $this;
  }

  public function __toString(): string
  {
    $content = HtmlService::formattedValue($this->content, '<p>%s</p>');

// Setting card media
    if ($this->image) {
      $media = ImageEntity::create($this->image)->wrap('card__image');
    } elseif ($this->icon) {
      $media = IconEntity::create($this->icon);
    }

    $headingGroup = HeadingGroupEntity::create(
      $this->heading
    )
      ->setSuperheader($this->superheader)
      ->setSubheader($this->subheader);

    $string = <<<HTML
      <div class="sidebar-item">
          <div class="linked-card">
              <a href="{$this->link['url']}" alt="{$this->link['title']}">
                  <div class="link-arrow">
                      <svg><use href="#aux_arrow-up-right"></use></svg>
                  </div>
                  $media
                  <div class="content-wrapper">
                      $headingGroup
                      $content
                  </div>
              </a>
          </div>
      </div>
      HTML;

    return $string;
  }

  public static function create(?array $link, ?string $heading, ?string $content) {
    $linkedCard = new static($link, $heading, $content);

    return $linkedCard;
  }
}
