<?php
/**
 * Frontend output for the AUX Collection Feature component
 * AUX documentation: https://aggieux.tamu.edu/?path=/docs/components-collection-features-about-collection-features--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\Enum\SocialMediaProfilesEnum;
use WP_AUX\Services\AuxCF\BlocksService;
use WP_AUX\Entities\AuxFramework\Partials\IconEntity;
use WP_AUX\Services\AuxFramework\ElementClassesService;
use WP_AUX\Services\HtmlService;
use WP_AUX\Entities\AuxFramework\Partials\DescriptionListEntity;
use WP_AUX\Entities\AuxFramework\Partials\LinkListEntity;
use WP_AUX\Entities\AuxFramework\Partials\IconListEntity;


$featureType = aux_get_field('collection_feature_type');

$collectionHeading = HtmlService::formattedValue(aux_get_field('collection_heading'), '<h3 class="feature__heading">%s</h3>');

$theFeature = null;
if ('desc-list' === $featureType) {
  $descListItems = aux_get_field('collection_feature_desc_list');
  $theFeature = DescriptionListEntity::create();

  foreach ($descListItems as $descListItem) {
    $theFeature->addItem($descListItem['collection_feature_desc_list_term'], $descListItem['collection_feature_desc_list_desc']);
  }
} elseif ('link-list' === $featureType) {
  $linkListItems = aux_get_field('collection_feature_link_list');
  $theFeature = LinkListEntity::create()->setColumns(2);

  foreach ($linkListItems as $link) {
    $theFeature->addLink($link['collection_feature_link_list_link']);
  }
} elseif ('icon-list' === $featureType) {
  $iconListItems = aux_get_field('collection_feature_icon_list');
  $theFeature = IconListEntity::create();

  foreach ($iconListItems as $iconListItem) {
    $theFeature->addItem(
      $iconListItem['collection_feature_icon_list_icon'],
      $iconListItem['collection_feature_icon_list_item_text']
    );
  }
} elseif (in_array($featureType, ['standard-factoid', 'inline-factoid'])) {
  $factoidsClass = ElementClassesService::buildClasses('factoid-collection');
  $footnoteClass = ElementClassesService::buildClasses('footnote');

  if ('standard-factoid' === $featureType) {
    $factoids = aux_get_field('collection_feature_standard_factoid');
    $footnoteClass->addModifier('centered');
  } else {
    $factoids = aux_get_field('collection_feature_inline_factoid');
    $factoidsClass->addModifier('inline');
  }

  $factoidsContent = '';
  foreach ($factoids as $factoid) {
    $factoidIcon = '';
    $factoidHeadingGroup = '';

    if ('standard-factoid' === $featureType) {
      if ($factoid['collection_feature_factoid_icon']) {
        $factoidIcon = IconEntity::create($factoid['collection_feature_factoid_icon']);
      }
    }

    if (!empty($factoid['collection_feature_factoid_heading'])) {
      $factoidHeadingGroup =
        sprintf('<div class="factoid__heading">
                    %s
                    %s
                    %s
                  </div>',
        HtmlService::formattedValue($factoid['collection_feature_factoid_superheader'], '<span class="superhead">%s</span>'),
        HtmlService::formattedValue($factoid['collection_feature_factoid_heading'], '<span class="heading">%s</span>'),
        HtmlService::formattedValue($factoid['collection_feature_factoid_subheader'], '<span class="subhead">%s</span>')
      );
    }

    $factoidContent = HtmlService::formattedValue($factoid['collection_feature_factoid_content'], '<span class="factoid__text">%s</span> ');

    $factoidFootnote = HtmlService::formattedValue(
      $factoid['collection_feature_factoid_citation'],
      '<div class="%2$s"><span>%1$s</span></div>',
      $footnoteClass
    );

    $factoidsContent .= <<< HTML
        <div class="factoid">
            $factoidIcon
            $factoidHeadingGroup
            <div class="factoid__content">
                $factoidContent
                $factoidFootnote
            </div>
        </div>
    HTML;
  }

  $theFeature = <<< HTML
      <div class="$factoidsClass">
          <div class="factoid-collection__wrapper">
              $factoidsContent
          </div>
      </div>
  HTML;
} elseif ('social-links' === $featureType) {
  $socialLinks = aux_get_field('collection_feature_social_links');

  if ($socialLinks) {
    $socialListItems = '';

    foreach ($socialLinks as $socialLink) {
      $platform = SocialMediaProfilesEnum::tryFrom($socialLink['collection_feature_social_links_platform']);

      if (empty($platform)) {
        continue;
      }

      $handle = @$socialLink['collection_feature_social_links_handle'];
      $link = @$socialLink['collection_feature_social_links_link'];

      if (SocialMediaProfilesEnum::LinkedIn === $platform) {
        $handle = @$link['url'];
        $text = @$link['title'];
      } else {
        $text = '@' . $handle;
      }

      $url = $platform->url($handle);

      if (empty($url)) {
        continue;
      }

      $socialListItems .= sprintf(
        "<li class=\"social-icon\">%s<a href=\"%s\">%s</a></li>\n",
        (string) IconEntity::create($platform->icon(), false),
        $url,
        $text
      );
    }

    $theFeature = sprintf('<ul class="social-icons">
                             %s
                           </ul>',
                           $socialListItems);
  }
}

$slab = BlocksService::getSlab('collection');
$slab->setWrapHeading(true);
$slab->setContent(<<<HTML
  <div class="collection__feature-container">
      <div class="collection__feature">
          $collectionHeading
          <div class="feature__content">
              $theFeature
          </div>
      </div>
  </div>
HTML);

echo $slab;
