<?php
declare(strict_types=1);

namespace WP_AUX\Entities\AuxFramework\Partials;

use WHOOP\Entities\Elements\Input;
use WP_AUX\Entities\AuxFramework\ElementClassesEntity;
use WP_AUX\Enum\SiteHeaderVariationEnum;
use WP_AUX\Services\AuxFramework\ElementClassesService;

/**
 * AUX header search form
 * @since 1.0.0
 */
class HeaderSearchEntity
{
  protected string $location = '';
  protected string $variation = 'division';
  protected ElementClassesEntity $classes;
  protected ElementClassesEntity $buttonClasses;

  public function __construct(string $location, ?string $variation = null) {
    $this->location = $location;

    if (is_null($variation)) {
      $variation = aux_header_variation()->value;
    }

    $this->variation = $variation;

    $this->classes = ElementClassesService::buildClasses('search');
    $this->buttonClasses = ElementClassesService::buildClasses('btn')
      ->addModifier('icon')
      ->addModifier('primary');
  }

  /**
   * Is the search form variation a university-level variation?
   * @since 1.0.0
   */
  public function getIsUniversity(): bool
  {
    return in_array($this->variation, [
      SiteHeaderVariationEnum::University->value,
      SiteHeaderVariationEnum::UniversityProgram->value,
    ]);
  }

  /**
   * Add a class to the search form container
   * @since 1.0.0
   */
  public function addClass(string $class): self
  {
    $this->classes->addClass($class);
    return $this;
  }

  /**
   * Add a class to the search form submit button
   * @since 1.0.0
   */
  public function addButtonClass(string $class): self
  {
    $this->buttonClasses->addClass($class);
    return $this;
  }

  public function __toString(): string
  {
    if ('utility' === $this->location && !$this->getIsUniversity() && 'program' !== $this->variation) {
      return '';
    }

    if ('identity' === $this->location && $this->getIsUniversity() && 'department' !== $this->variation) {
      return '';
    }

    $searchClasses = clone $this->classes;
    $buttonClasses = clone $this->buttonClasses;

    $buttonContent = 'Search <svg><use href="#aux_magnifying-glass"></use></svg>';

    $url = get_home_url();

    $beforeForm = '';

    $searchInput = Input::createSearch([
      'name' => aux_search_field_name(),
      'placeholder' => '',
      'id' => 'search',
      'class' => 'search__input',
    ]);

    if ($this->location === 'utility') {
      $searchClasses
        ->addModifier('on-dark')
        ->addModifier('slim');

      $buttonClasses->addModifier('primary-dark');
    }

    if ($this->variation === 'university') {
      $beforeForm = '<span class="search__label" id="search-label">Search this Site</span>';
      $searchInput->setAttribute('aria-labelledby', 'search-label');
      $buttonClasses = 'search__submit';
      $buttonContent = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M416 208c0 45.9-14.9 88.3-40 122.7l126.6 126.7c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L330.7 376c-34.4 25.2-76.8 40-122.7 40C93.1 416 0 322.9 0 208S93.1 0 208 0s208 93.1 208 208zM208 352a144 144 0 1 0 0-288 144 144 0 1 0 0 288z"/></svg>';
    }

    return <<<HTML
        {$beforeForm}
        <div class="{$searchClasses}">
          <form class="search__form" action="{$url}">
            <label for="search" class="sr-only">Search</label>
            {$searchInput}
            <button type="submit" class="{$buttonClasses}">
              {$buttonContent}
            </button>
          </form>
        </div>
    HTML;
  }

  /**
   * Create a header search form with location (utility or identity) and variation
   * @since 1.0.0
   */
  public static function create(string $location, ?string $variation = null): static
  {
    return new static($location, $variation);
  }
}
