<?php
/**
 * Frontend output for the AUX Media Feature component
 * AUX documentation: https://aggieux.tamu.edu/?path=/story/components-media-feature--example-media-feature
 *
 * Outline:
 * 1. Set variables to null
 * 2. Get fields from the post, if they have values, wrap in html
 * 3. Build the component html
 * 4. Sanitize and output
 *
 * @author Hailey Sanders
 * @since 1.0.0
 */

use WHOOP\Entities\Elements\A;
use WP_AUX\Entities\AuxCF\Fields\MediaFeature\MediaFeatureVideoFileFieldEntity;
use WP_AUX\Entities\AuxCF\Fields\MediaFeature\MediaFeatureVideoLinkFieldEntity;
use WP_AUX\Entities\AuxCF\Fields\MediaFeature\MediaFeatureVideoSwitchFieldEntity;
use WP_AUX\Entities\AuxFramework\Partials\HeadingGroupEntity;
 use WP_AUX\Entities\AuxFramework\Partials\ImageEntity;
use WP_AUX\Entities\Embeds\VideoEmbedEntity;
use WP_AUX\Services\AuxFramework\ElementClassesService;
use WP_AUX\Services\HtmlService;

// Set variables to null
$styleVariable = '';
$backgroundVariable = '';
$mediaFeatureCta = '';
$media = '';
$mediaFeatureCtaLink = [];
$media_feature_output = '';

// Set component variables from user input
$mediaFeatureBackground = get_field('media_feature_background');
$mediaFeatureStyle = get_field('media_feature_style');
$mediaFeatureCtaType = get_field('media_feature_cta');
$mediaFeatureMedia = get_field('media_feature_media_switch');
$mediaFeatureHeading = get_field('media_feature_heading');
$mediaFeatureSuperheader = get_field('media_feature_superheader');
$mediaFeatureSubheader = get_field('media_feature_subheader');
$mediaFeatureDescription = get_field('media_feature_description');
$mediaFeatureCtaLink = get_field('media_feature_cta_link');
$mediaFeatureImage = get_field('media_feature_image_file');
$mediaFeatureAlignment = get_field('media_feature_alignment');

// Setting background, style, and video classes
$componentClasses = ElementClassesService::buildClasses('media-feature', $mediaFeatureStyle, $mediaFeatureBackground);
if ('video' === $mediaFeatureMedia) {
  $componentClasses->addModifier('video');
}
if ('right' === $mediaFeatureAlignment) {
  $componentClasses->addModifier('right');
}

// Setting heading group
$headingGroup = HeadingGroupEntity::create(
  $mediaFeatureHeading,
);
if ('linked-heading' === $mediaFeatureCtaType) {
  $headingGroup->setLink($mediaFeatureCtaLink);
}
$headingGroup
  ->setIsFeature(true)
  ->setSuperheader($mediaFeatureSuperheader)
  ->setSubheader($mediaFeatureSubheader);

// Setting description
$mediaFeatureDescription = HtmlService::formattedValue($mediaFeatureDescription, '<p class="media-feature__description">%s</p>');

// Setting CTAs
if (!empty($mediaFeatureCtaLink)) {
  $mediaFeatureCtaClass = false;

  if ('link' === $mediaFeatureCtaType) {
    $mediaFeatureCtaClass = 'link--cta';
  } elseif ('button' === $mediaFeatureCtaType) {
    $mediaFeatureCtaClass = 'btn btn--primary';
  }

  $mediaFeatureCta = A::create($mediaFeatureCtaLink['title'], [
    'href' => $mediaFeatureCtaLink['url'],
    'class' => $mediaFeatureCtaClass,
    'target' => $mediaFeatureCtaLink['target'],
  ]);
}

// Setting media
if ('image' === $mediaFeatureMedia) {
  $media = ImageEntity::create($mediaFeatureImage);
} elseif ('video' === $mediaFeatureMedia) {
  switch (get_field(MediaFeatureVideoSwitchFieldEntity::$name)) {
    case 'internal':
      $video = aux_get_field(MediaFeatureVideoFileFieldEntity::$name);
      break;
    case 'external':
    default:
      $video = aux_get_field(MediaFeatureVideoLinkFieldEntity::$name);
      break;
  }

  $media = VideoEmbedEntity::create(
    $video,
    [
      'width' => 560,
      'height' => 315,
    ],
  );
};


if ('left' === $mediaFeatureAlignment) {
  $media_feature_output = <<<HTML
    <div class="$componentClasses">
      <div class="media-feature__media">
        $media
      </div>
      <div class="media-feature__content">
        $headingGroup
        $mediaFeatureDescription
        $mediaFeatureCta
      </div>
    </div>
  HTML;
} else if ('right' === $mediaFeatureAlignment) {
  $media_feature_output = <<<HTML
    <div class="$componentClasses">
      <div class="media-feature__content">
        $headingGroup
        $mediaFeatureDescription
        $mediaFeatureCta
      </div>
      <div class="media-feature__media">
        $media
      </div>
    </div>
  HTML;
}

echo $media_feature_output;
