<?php

namespace WP_AUX\Entities\PageTemplate;

use WP_AUX\Entities\Components\BreadcrumbsEntity;
use WP_AUX\Entities\PageTemplate\Traits\RenderTemplateTraits;
use WP_AUX\Entities\PageTemplateEntity;
use WP_AUX\Services\PageTemplate\LayoutService;

/**
 * Represents the page template layout entity with layout-specific behaviors and configurations.
 * This class integrates with a PageTemplateEntity that encapsulates template-related data.
 *
 * @since 1.0.0
 */
class LayoutEntity
{
  use RenderTemplateTraits;

  public PageTemplateEntity $pageTemplate;
  protected string $templateSlug = 'layout';

  protected string $type = 'standard';

  public function __construct(PageTemplateEntity $pageTemplate)
  {
    $this->pageTemplate = $pageTemplate;
  }

  /**
   * Automatically set the layout data based on page builder settings
   * @since 1.0.0
   */
  public function setFromRequest(): void
  {
    if (
      $this->pageTemplate->header->isType('hero') ||
      $this->pageTemplate->header->isType('featured')
    ) {
      return;
    }

    if ($type = $this->pageTemplate->getSetting('layout')) {
      $this->setType($type);
    }
  }

  public function getType(): string
  {
    return $this->type;
  }

  public function setType(string $type): self
  {
    $this->type = $type;

    return $this;
  }

  public function isType(string $type): bool
  {
    return $this->type === $type;
  }

  /**
   * Get the breadcrumbs for the current page
   *
   * If the breadcrumbs are being rendered in the header, the background color of
   * the header will be used to determine if the breadcrumbs should be displayed "on dark".
   *
   * @since 1.0.0
   */
  public function breadcrumbs(bool $inHeader = false): BreadcrumbsEntity
  {
    $breadcrumbs = LayoutService::breadcrumbs();

    if ($inHeader) {
      $header = $this->pageTemplate->header;

      if ($header->isDarkBackground()) {
        $breadcrumbs->addClasses(['breadcrumbs--on-dark']);
      }
    }

    return $breadcrumbs;
  }
}
