<?php
/**
 * AUX Card Group template
 */

use WHOOP\Entities\Elements\A;
use WP_AUX\Entities\AuxFramework\Partials\PrimaryAltButtonGroupEntity;
use WP_AUX\Services\AuxFramework\ElementClassesService;
use WP_AUX\Entities\AuxFramework\Partials\HeadingGroupEntity;
use WP_AUX\Entities\AuxFramework\Partials\IconEntity;
use WP_AUX\Entities\AuxFramework\Partials\ImageEntity;
use WP_AUX\Entities\AuxFramework\Partials\LinkListEntity;
use WP_AUX\Services\HtmlService;
use WP_AUX\Services\PageTemplateService;

// Set component variables from user input
$background = get_field('callout_background');
$style = get_field('callout_style');
$calloutHeading = get_field('callout_heading');
$calloutSuperheader = get_field('callout_superheader');
$calloutSubheader = get_field('callout_subheader');
$calloutImage = get_field('callout_image');
$calloutIcon = get_field('callout_icon');
$calloutCtaType = get_field('callout_cta_type');
$calloutCtas = get_field('callout_ctas');
$calloutDescription = get_field('callout_description');
$calloutImageAlignment = get_field('callout_image_alignment');

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

PageTemplateService::applyInternalPageClass($componentClasses);

// Create media content (image or icon)
$mediaContent = '';
if ($calloutImage) {
  $mediaContent = ImageEntity::create($calloutImage)->wrap('callout__image');
} elseif ($calloutIcon) {
  $mediaContent = IconEntity::create($calloutIcon);
}

// only render the heading link if the cta type is set to linked heading
$calloutHeadingUrl = '';
if ('linked-heading' === $calloutCtaType) {
  $calloutHeadingUrl = aux_get_field('callout_cta');
}

// Create heading group
$headingGroup = HeadingGroupEntity::create(
  $calloutHeading,
  $calloutHeadingUrl,
);
$headingGroup->setIsFeature(true)->setSuperheader($calloutSuperheader)->setSubheader($calloutSubheader);

// Setting description
$calloutDescription = HtmlService::formattedValue($calloutDescription, '<p>%s</p>');

// Setting CTAs
$theCta = '';
if ('links' === $calloutCtaType) {
  $theCta = LinkListEntity::create();
  foreach ($calloutCtas as $cta) {
    $theCta->addLink($cta['callout_link']);
  }
} elseif ('buttons' === $calloutCtaType) {
  $theCta = PrimaryAltButtonGroupEntity::create();
  foreach ($calloutCtas as $cta) {
    $theCta->addButton($cta['callout_link']);
  }
} elseif (in_array($calloutCtaType, ['link', 'button'])) {
  $calloutSingleCta = aux_get_field('callout_cta');

  if ($calloutSingleCta) {
    $linkClass = ('link' === $calloutCtaType) ? 'link--cta' : 'btn btn--primary';

    $theCta = A::create($calloutSingleCta['title'], [
      'href' => $calloutSingleCta['url'],
      'class' => $linkClass,
      'target' => $calloutSingleCta['target'],
    ]);
  }
}

$calloutDescription = HtmlService::formattedValue($calloutDescription, '<p>%s</p>');

// adding class if media = image and orientation = right
if ('right' === $calloutImageAlignment) {
  $componentClasses->addModifier('right');
}

$html = <<<HTML
<section class="$sectionClasses">
  $divider
  <div class="$componentClasses">
    <div class="callout__container">
      $mediaContent
      <div class="callout__content">
        $headingGroup
        $calloutDescription
        $theCta
      </div>
    </div>
  </div>
</section>
HTML;

echo $html;
