<?php
/**
 * Frontend output for the AUX Video Embed component
 * AUX documentation: https://aggieux.tamu.edu/?path=/docs/components-video-embed-about-video-embeds--docs
 *
 * 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 WP_AUX\Entities\AuxFramework\Partials\SocialLinkCollectionEntity;
use WP_AUX\Entities\Embeds\VideoEmbedEntity;
use WP_AUX\Services\AuxFramework\ElementClassesService;
use WP_AUX\Services\HtmlService;
use WP_AUX\Services\PageTemplateService;

// Set variables to null
$theCta = null;

// Set component variables from user input
$videoEmbedHeadingText = aux_get_field('video_embed_heading');
$videoEmbedDescText = aux_get_field('video_embed_desc');
$videoEmbedCtaLink = aux_get_field('video_embed_cta_link');
$videoEmbedCtaType = aux_get_field('video_embed_cta_type');
$videoEmbedShare = aux_get_field('video_embed_share_switch');
$background = aux_get_field('section_background');
$style = aux_get_field('section_style');
$video = aux_get_field_array('video');

$videoEmbed = VideoEmbedEntity::create(
  $video,
  [
    'width' => '100%',
  ],
);

$videoEmbedHeading = HtmlService::formattedValue($videoEmbedHeadingText, '<h2>%s</h2>');
$videoEmbedDesc = HtmlService::formattedValue($videoEmbedDescText, '<p>%s</p>');

// Setting the section and component styles
$sectionClasses = ElementClassesService::buildDynamicClasses('section-wrap', '', $background);
$componentClasses = ElementClassesService::buildClasses('video-embed', $style, $sectionClasses);
//return;

PageTemplateService::applyInternalPageClass($componentClasses);

// Setting CTA
if ($videoEmbedCtaLink) {
  if ('link' === $videoEmbedCtaType) {
    $theCta = sprintf('<p><a href="%s" class="link--cta">%s</a></p>', $videoEmbedCtaLink['url'], $videoEmbedCtaLink['title']);
  } elseif ('button' === $videoEmbedCtaType) {
    $theCta = sprintf('<p><a href="%s" class="btn btn--primary">%s</a></p>', $videoEmbedCtaLink['url'], $videoEmbedCtaLink['title']);
  }
}

// Setting the share buttons
$socialShare = '';
if ($videoEmbedShare) {
  $socialShare = SocialLinkCollectionEntity::createRightBlock(
    'Share this Video',
    $videoEmbed->getShareUrl(),
    $videoEmbed->getTitle()
  );
}

$videoEmbedInfo = HtmlService::formattedValue(
  $videoEmbedHeading . $videoEmbedDesc . $theCta,
  '<div class="info">%s</div>'
);

$videoEmbedContent = HtmlService::formattedValue(
  $videoEmbedInfo . $socialShare,
  '<div class="video-embed__content">%s</div>'
);

// Output
$output = <<<HTML
<section class="$sectionClasses">
  <div class="$componentClasses">
    $videoEmbed
    $videoEmbedContent
  </div>
</section>
HTML;

echo $output;
