<?php
/**
 * Frontend output for the AUX Custom Content component
 * AUX documentation: https://aggieux.tamu.edu/?path=/docs/components-custom-content-about-custom-content--docs
 *
 * Outline:
 * 1. Set variables
 * 2. Set styles
 * 3. Create sidebar
 * 4. Build the component html
 * 5. Output
 *
 * @author Hailey Sanders
 * @since 1.0.0
 */

use WP_AUX\Services\AuxFramework\CustomContentSidebarService;
use WP_AUX\Services\AuxFramework\ElementClassesService;
use WP_AUX\Entities\AuxFramework\Partials\CaptionedMediaEntity;
use WP_AUX\Services\PageTemplateService;

// Set component variables from user input
$background = get_field('custom_content_background');
$customContent = aux_get_field('custom_content_wysiwyg');
$captionedMediaAlignment = get_field('captioned_media_alignment');
$captionedMediaType = get_field('captioned_media_type');

// Set classes
$sectionClasses = ElementClassesService::buildClasses('section-wrap', '', $background);
$componentClasses = ElementClassesService::buildClasses('custom-content', '', $sectionClasses);
$divider = $sectionClasses->hasDivider() ? '<span class="divider-dots gray-300"></span>' : '';

PageTemplateService::applyInternalPageClass($componentClasses);

// Captioned Media - Within Custom Content
$media = false;
if ('image' === $captionedMediaType) {
  $media = get_field('captioned_media_image');
} elseif ('video' === $captionedMediaType) {
  $media = get_field('captioned_media_video_link');
}

if ($media) {
  $captionedMedia = CaptionedMediaEntity::create(
    $media,
    $captionedMediaType,
    get_field('captioned_media_caption'),
    get_field('captioned_media_credit')
  )
    ->setAlign($captionedMediaAlignment);

  if ('bottom' === $captionedMediaAlignment) {
    $customContent .= $captionedMedia;
  } else {
    $customContent = $captionedMedia . $customContent;
  }
}

// Create sidebar, if applicable
$sidebar = CustomContentSidebarService::createSidebar();

// Output
$custom_content_output = <<<HTML
<section class="$sectionClasses">
  $divider
  <div class="$componentClasses">
    <div class="custom-content__wrapper">
      <div class="custom-content__content">
        $customContent
      </div>
    </div>
    $sidebar
  </div>
</section>
HTML;

echo $custom_content_output;
