<?php
declare(strict_types=1);

namespace WP_AUX\Entities\AuxFramework\Partials\Traits;

use WHOOP\Entities\ElementAttributes;
use WP_AUX\Entities\AuxFramework\ElementClassesEntity;
use WP_AUX\Services\AuxFramework\ElementClassesService;
use WP_AUX\Services\PageTemplateService;

/**
 * Shared AUX section and slab properties and methods
 * @since 1.0.1
 */
trait SectionTraits
{
  protected ?string $id = null;
  protected ?string $content = '';
  protected string $componentClass = '';
  protected ?ElementClassesEntity $sectionClasses = null;
  protected ?ElementClassesEntity $componentClasses = null;
  protected ElementAttributes $sectionAttributes;
  protected ElementAttributes $componentAttributes;

  public function __construct(string $componentClass = '', ?string $background = '', ?string $style = '')
  {
    $this->componentClass = $componentClass;
    $this->sectionClasses = ElementClassesService::buildClasses('section-wrap', '', $background);
    $this->componentClasses = ElementClassesService::buildClasses($componentClass, $style, $this->sectionClasses);

    $this->sectionAttributes = ElementAttributes::create(null, null, ['section' => 'section-wrap']);
    $this->componentAttributes = ElementAttributes::create();

    /**
     * Pass the component classes through to add internal classes if the section is on an internal page
     */
    if ($this->componentClass) {
      PageTemplateService::applyInternalPageClass($this->componentClasses);
    }
  }

  /**
   * Set the ID of the section, to be added to the <section> on rendering
   * @since 1.0.1
   */
  public function setId(?string $id): static
  {
    $this->id = $id;
    return $this;
  }

  /**
   * Set the content HTML for the section
   * @since 1.0.1
   */
  public function setContent(?string $content): static
  {
    $this->content = $content;
    return $this;
  }

  /**
   * Get the primary component classes for the section
   * @since 1.0.1
   */
  public function getComponentClasses(): ElementClassesEntity
  {
    return $this->componentClasses;
  }

  /**
   * Add a modifier to the component portion of the section
   * @since 1.0.1
   */
  public function addComponentModifier(?string $modifier): static
  {
    if (!empty($modifier)) {
      $this->componentClasses->addModifier($modifier);
    }

    return $this;
  }

  /**
   * Add an additional class to the <section>
   * @since 1.0.1
   */
  public function addSectionClass(string $class): static
  {
    $this->sectionAttributes->addClass($class);
    return $this;
  }

  /**
   * Add an additional attribute to the <section>
   * @since 1.0.1
   */
  public function addSectionAttribute(string $name, string $value): static
  {
    $this->sectionAttributes->setAttribute($name, $value);
    return $this;
  }

  /**
   * Add an additional data value to the <section>
   * @since 1.0.1
   */
  public function addSectionDataValue(string $name, string $value): static
  {
    $this->sectionAttributes->setDataValue($name, $value);
    return $this;
  }

  /**
   * Add an additional class to the <section>'s component <div>
   * @since 1.0.1
   */
  public function addComponentClass(string $class): static
  {
    $this->componentAttributes->addClass($class);
    return $this;
  }

  /**
   * Add an additional attribute to the <section>'s component <div>
   * @since 1.0.1
   */
  public function addComponentAttribute(string $name, string $value): static
  {
    $this->componentAttributes->setAttribute($name, $value);
    return $this;
  }

  /**
   * Add an additional data value to the <section>'s component <div>
   * @since 1.0.1
   */
  public function addComponentDataValue(string $name, string $value): static
  {
    $this->componentAttributes->setDataValue($name, $value);
    return $this;
  }

  /**
   * Create a new section or slab with the base component class and optional background and style
   * @since 1.0.1
   */
  public static function create(
    string $componentClass = '',
    ?string $background = '',
    ?string $style = ''
  ): static
  {
    $section = new static($componentClass, $background, $style);

    return $section;
  }
}
