<?php
/**
 * Frontend output for the AUX Accordion Group component
 * AUX documentation: https://aggieux.tamu.edu/?path=/docs/components-accordion-group-about-accordion-groups--docs
 *
 * For debugging ACF fields: !d(get_fields());
 *
 * Outline:
 * 1. Get fields from the post, if they have values, sanitize and wrap in html
 * 2. Build the component html
 *
 * @author Jeremy Tarpley
 * @since 1.0.0
 */

use WP_AUX\Services\AuxCF\BlocksService;
use WP_AUX\Services\HtmlService;
use WP_AUX\Entities\AuxFramework\Partials\CaptionedMediaEntity;

// $auxBlockCount is a global variable set in template-parts/content-page.php
// Increment it each time accordion is called to create a unique ID for the accordion group.
global $auxBlockCount;
$auxBlockCount++;
$accordionGroup = "accordionGroup-$auxBlockCount";

$accordions = aux_get_field('accordion');

// Output variables
$accordionOutput = '';

// Loop through the accordion items. $count is the count for each loop iteration.
if ($accordions) {
  foreach ($accordions as $count=>$accordion) {
    $accordionContent = $accordion['accordion_wysiwyg'];

    $accordionSuperheader = HtmlService::formattedValue($accordion['accordion_superheader'], '<span class="superhead">%s</span>');
    $accordionHeading = HtmlService::formattedValue($accordion['accordion_heading'], '<span class="ns-h3">%s</span>');
    $accordionSubheader = HtmlService::formattedValue($accordion['accordion_subheader'], '<span class="subhead">%s</span>');

    // Skip the accordion if it has no content or title
    if (!$accordionContent || !$accordionHeading) {
      continue;
    }

    // setting captioned media
    $captionedMedia = CaptionedMediaEntity::create(
      $accordion['accordion_captioned_media_image'],
      'image',
      $accordion['accordion_captioned_media_caption'],
      $accordion['accordion_captioned_media_credit']
    );

    // setting the alignment based on user input
    $captionedMediaAlignment = $accordion['accordion_captioned_media_alignment'];

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

    // Each accordion item needs uniqe ID attributes
    $accordionID = "$accordionGroup--accordionItem-$count";
    $accordionTitleID = "$accordionGroup-title-$count";
    $accordionPanelID = "$accordionGroup-panel-$count";

    $accordionOutput .= <<<HTML
      <div class="aux-accordion" id="$accordionID">
        <span id="$accordionTitleID">
          <h3>
            <button
              aria-expanded="false"
              aria-label="Row Title Accordion Label"
              class="aux-accordion-trigger"
              aria-controls="$accordionPanelID">
              <span class="aux-accordion__title">
                $accordionSuperheader
                $accordionHeading
                $accordionSubheader
              </span>
            </button>
          </h3>
        </span>
        <div
          class="aux-accordion-panel"
          hidden=""
          role="region"
          aria-labelledby="$accordionTitleID"
          id="$accordionPanelID"
        >
          $accordionContent
        </div>
      </div>
    HTML;
  }

  $accordionOutput = HtmlService::formattedValue($accordionOutput, '<div class="accordion-container">%s</div>');
}

$section = BlocksService::getSection('accordion-group')
  ->setContent($accordionOutput);

echo $section;
