<?php
declare(strict_types=1);

/**
 * Template for displaying the footer
 *
 * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
 * @link https://aggieux.tamu.edu/?path=/docs/navigation-footers-about-footers--docs
 *
 * @package MarComm-AUX
 * @since 1.0.0
 */

use WP_AUX\Services\HtmlService;

// Retrieve the footer option setting value
$footerOption = get_theme_mod('footer_option', 'optionA'); // 'optionA' is the default value
$year = date('Y');

$footerIdentity = '';
$footerComplianceLogo = '';
$footerColumnsContent = '';

// Dynamic sidebar echos out the content. Using output buffering to store in a variable.
ob_start();

// Get dynamic_sidebars for the footer columns
if (is_active_sidebar('footer_content_1')) {
  dynamic_sidebar('footer_content_1');
}

if (is_active_sidebar('footer_content_2')) {
  dynamic_sidebar('footer_content_2');
}

if (is_active_sidebar('footer_content_3')) {
  dynamic_sidebar('footer_content_3');
}

// Store the footer sidebar columns in a variable
$footerColumnsContent .= ob_get_clean();

// Count number of footer columns by class name
// Currently, multiple column components can be added to every footer column area
$footerColumnsCount = substr_count($footerColumnsContent, 'footer__column');

if ('optionA' === $footerOption) {
  $footerIdentityColumn = sprintf(
    '<div class="footer__column footer__column--identity">%s</div>',
    aux_identity_lockup($footerColumnsCount >= 2)
  );

  $footerColumnsContent = $footerIdentityColumn . $footerColumnsContent;
}
elseif ('optionB' === $footerOption) {
  $footerHeading = esc_html(get_theme_mod('footer_heading'));

  // Get the footer superheader and heading if footer heading has text
  if ($footerHeading) {
    $footerComplianceLogo = <<<HTML
      <a href="https://www.tamu.edu/">
        <img src="https://aux.tamu.edu/logos/primaryTAM-white.svg" alt="Texas A&amp;M University" />
      </a>
    HTML;

    $footerSuperheader = HtmlService::formattedValue(
      esc_html(get_theme_mod('footer_superheader')),
      '<span class="superhead">%s</span>'
    );

    $footerSubheader = HtmlService::formattedValue(
      esc_html(get_theme_mod('footer_subheader')),
      '<span class="subhead">%s</span>'
    );

    $footerIdentity = <<<HTML
      <div class="footer__identity">
        <div class="identity__container">
          <div class="heading-group">
            $footerSuperheader
            <h2>$footerHeading</h2>
            $footerSubheader
          </div>
        </div>
      </div>
    HTML;
  }
}

echo <<<HTML
    <footer class="footer--slim">
      <div class="footer__container">
        $footerIdentity
        <div class="footer__columns">
          $footerColumnsContent
        </div>
      </div>
      <div class="footer__compliance-wrapper">
        <div class="footer__compliance">
          $footerComplianceLogo
          <ul class="compliance__list">
            <li>
              © $year
              <a href="https://www.tamu.edu/">Texas A&amp;M University</a>
            </li>
            <li><a href="https://www.tamu.edu/statements/index.html">Site Policies</a></li>
            <li><a href="https://itaccessibility.tamu.edu/">Accessibility</a></li>
          </ul>
        </div>
      </div>
    </footer>
  </div><!-- #page -->
HTML;

// Output the footer scripts
wp_footer();
?>

</body>
</html>
