<?php
declare(strict_types=1);

namespace WP_AUX\Services\PageTemplate;

use WP_AUX\Entities\PageTemplate\HeaderEntity;
use WP_AUX\Services\AuxFramework\PageComponentsService;
use WP_AUX\Services\HtmlService;
use WP_AUX\Services\PostService;
use WP_AUX\Services\StoryService;

class StoryHeaderService
{
  public static function renderHeaderTitle(HeaderEntity $header): void
  {
    ?>
    <div class="news-page-header__title">
      <?php
        echo PageComponentsService::getCategoriesTagsCollection();
        echo the_title('<h1>', '</h1>', false);
        echo HtmlService::paragraph($header->getExcerpt(), 'title__subhead');
      ?>
    </div>
    <?php
  }

  public static function getHeaderByline(HeaderEntity $header): string
  {
    $source = $header->getSource();

    $sourceLink = '';
    if (!empty($source['link'])) {
      $sourceLink = HtmlService::link($source['link']['title'], $source['link']);
    }

    $date = StoryService::getOriginalDate();
    $authors = StoryService::getBylineAuthors();

    $byline = HtmlService::formattedValue($date, '<span class="date">%s</span>');
    $byline .= $authors;

    if ($authors && !empty((string) $sourceLink)) {
      $byline .= ' • ';
    }

    $byline .= $sourceLink;

    return $byline;
  }

  public static function renderHeaderByline(HeaderEntity $header): void
  {
    ?>
    <div class="news-page-header__byline">
      <p class="date-author">
        <?php echo self::getHeaderByline($header); ?>
      </p>
      <p class="read-time">
        <svg><use href="#aux_clock"></use></svg>
        <?php echo PostService::pageReadTime(); ?>
      </p>
    </div>
    <?php
  }
}
