<?php

namespace WP_AUX\Services\PageTemplate;

use WP_AUX\Entities\AuxFramework\ElementClassesEntity;
use WP_AUX\Entities\AuxFramework\Partials\CtaHeaderButtonGroupEntity;
use WP_AUX\Entities\AuxFramework\Partials\ButtonGroupEntity;
use WP_AUX\Entities\AuxFramework\Partials\PrimaryButtonGroupEntity;
use WP_AUX\Entities\PageTemplate\HeaderEntity;
use WP_AUX\Services\AuxFramework\ElementClassesService;

/**
 * Utility functions for working with page template headers
 * @since 1.0.0
 */
class HeaderService
{
  /**
   * Get the button group of CTAs for the header
   * @since 1.0.0
   */
  public static function ctas(null|string|array $headerCtas): ?ButtonGroupEntity
  {
    if (empty($headerCtas)) {
      return null;
    }

    if (is_front_page()) {
      $buttonGroup = PrimaryButtonGroupEntity::create();
    } else {
      $buttonGroup = CtaHeaderButtonGroupEntity::create();
    }

    $ctaCount = 0;
    foreach ($headerCtas as $headerCta) {
      if (empty($headerCta['aux_page_cta_link'])) {
        continue;
      }
      $buttonGroup->addButton($headerCta['aux_page_cta_link']);

      $ctaCount += 1;

      // only render up to 2 buttons on standard and landing page header
      if(!is_front_page() && (2 <= $ctaCount )) {
        break;
      }
    }

    return $buttonGroup;
  }

  /**
   * Get the header image HTML for the current page
   * @since 1.0.0
   */
  public static function pageImageHtml(int $postId, string $class): string
  {
    if (!has_post_thumbnail()) {
      return '';
    }

    $thumbnailId = get_post_thumbnail_id($postId);
    $thumbnailUrl = wp_get_attachment_url($thumbnailId);
    $thumbnailAlt = get_post_meta($thumbnailId, '_wp_attachment_image_alt', true);

    return sprintf('<div class="%s"><img src="%s" alt="%s"></div>', $class, esc_url($thumbnailUrl), esc_attr($thumbnailAlt));
  }

  /**
   * Get the header wrapper classes
   * @since 1.0.0
   */
  public static function getWrapperClasses(HeaderEntity $header): ElementClassesEntity
  {
    $headerClasses = ElementClassesService::buildClasses($header->getBaseClass());

    if ($headerStyle = $header->getHeaderStyle()) {
      $headerClasses->addModifier($headerStyle);
    }

    if ($mediaHeight = $header->getMediaHeight()) {
      $headerClasses->addModifier($mediaHeight);
    }

    switch ($header->getBackgroundColor()) {
      case 'maroon':
        $headerClasses->addModifier('primary-brand');
        break;
      case 'light-gray':
        $headerClasses->addModifier('gray-100');
        break;
      case 'charcoal':
        $headerClasses->addModifier('gray-900');
        break;
    }

    if ($header->getType() === 'hero') {
      if (is_front_page()) {
        $headerClasses->addModifier('main');
      }
    }

    return $headerClasses;
  }

  /**
   * Get the divider string, conditional on the header type and style
   * @since 1.0.0
   */
  public static function getDivider(HeaderEntity $header): string
  {
    $divider = '';

    if ($header->isType('hero')) {
      $doDivider = is_front_page() && ('bold' === $header->getHeaderStyle());
    } else {
      $doDivider = 'none' === $header->getMediaHeight() && 'none' === $header->getBackgroundColor();
    }

    if ($doDivider) {
      $divider = '<span class="divider-dots gray-300"></span>';
    }

    return $divider;
  }
}
