<?php
/**
 * Customize the Theme Customizer
 * Remove items we don't need, add controls for the header and footer.
 *
 * @package MarComm-AUX
 * @since 1.0.0
 */

use WP_AUX\Enum\SiteHeaderVariationEnum;
use WP_AUX\Services\HeadService;

/**
 * Only show the college/parent organization fields if the header variation is set to 'department'
 * @since 1.0.0
 */
function aux_header_college_active_callback(): bool
{
  return @aux_header_settings()['header_variation'] === 'department';
}

/**
 * Only show the footer heading fields if the footer is option B
 * @since 1.0.0
 */
function aux_header_footer_option_b(): bool
{
  return get_theme_mod('footer_option') === 'optionB';
}

/**
 * Add postMessage support for site title and footer in the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 * @since 1.0.0
 */
function aux_customize_register($wp_customize): void
{
	$wp_customize->get_setting('blogname')->transport = 'refresh';

  // Remove the tagline and site icon controls from the Site Identity menu
  $wp_customize->remove_control('blogdescription');
  $wp_customize->remove_control('site_icon');

  // Add a description to the Site Title
  $wp_customize->get_control('blogname')->description =
    __('Enter the name of your site. The site name and page title are combined to create the
    title that is displayed in search engine results.', 'marcomm-aux');

  $wp_customize->add_section('aux_header_settings', array(
    'title'    => 'AUX Header',
    'priority' => 10,
  ));

  $siteHeaderVariationOptions = SiteHeaderVariationEnum::getIndexedOptions();
  if (!\WP_AUX::theme()->getIsUniversity()) {
    unset($siteHeaderVariationOptions[SiteHeaderVariationEnum::University->value]);
    unset($siteHeaderVariationOptions[SiteHeaderVariationEnum::UniversityProgram->value]);
  }

  $wp_customize->add_setting('aux_header[header_variation]', array(
    'default'        => SiteHeaderVariationEnum::DEFAULT->value,
    'capability'     => 'edit_theme_options',
    'type' => 'option',
  ));
  $wp_customize->add_control('aux_header_variation', array(
    'label'       => 'Header Variation',
    'description' => '',
    'section'     => 'aux_header_settings',
    'settings'    => 'aux_header[header_variation]',
    'type'        => 'select',
    'choices' => $siteHeaderVariationOptions
  ));

  $wp_customize->add_setting('aux_header[lockup_name]', array(
    'default'        => '',
    'capability'     => 'edit_theme_options',
    'type' => 'option',
    'sanitize_callback' => 'sanitize_text_field',
  ));
  $wp_customize->add_control('aux_header_lockup_name', array(
    'label'       => 'Site Identity',
    'description' => 'This will appear as your unit identity lockup, if applicable.',
    'section'     => 'aux_header_settings',
    'settings'    => 'aux_header[lockup_name]',
    'type'        => 'text',
  ));

  $wp_customize->add_setting('aux_header[lockup_name_additional]', array(
    'default'        => '',
    'capability'     => 'edit_theme_options',
    'type' => 'option',
    'sanitize_callback' => 'sanitize_text_field',
  ));
  $wp_customize->add_control('aux_header_lockup_name_additional', array(
    'label'       => 'Site Identity – Line Two (optional)',
    'description' => 'For longer unit identities, enter the second line of your unit identity lockup. This allows you to control where the line break occurs.',
    'section'     => 'aux_header_settings',
    'settings'    => 'aux_header[lockup_name_additional]',
    'type'        => 'text',
  ));

  $wp_customize->add_setting('aux_header[college_name]', array(
    'default'        => '',
    'capability'     => 'edit_theme_options',
    'type'           => 'text',
    'sanitize_callback' => 'sanitize_text_field',
    'type' => 'option',
  ));
  $wp_customize->add_control('aux_header_college_name', array(
    'label'       => 'Parent Organization (optional)',
    'description' => 'Provide link text for the parent organization. This will be displayed in the utility navigation.',
    'section'     => 'aux_header_settings',
    'settings'    => 'aux_header[college_name]',
    'type'        => 'text',
    'active_callback' => 'aux_header_college_active_callback',
  ));

  $wp_customize->add_setting('aux_header[college_url]', array(
    'default'        => '',
    'capability'     => 'edit_theme_options',
    'type'           => 'text',
    'sanitize_callback' => 'sanitize_text_field',
    'type' => 'option',
  ));
  $wp_customize->add_control('aux_header_college_url', array(
    'label'       => 'Parent Organization URL (optional)',
    'description' => 'Provide the link destination for the parent organization entered above. Ex. https://domain.tamu.edu',
    'section'     => 'aux_header_settings',
    'settings'    => 'aux_header[college_url]',
    'type'        => 'url',
    'active_callback' => 'aux_header_college_active_callback',
  ));

  $wp_customize->add_setting('aux_header[hide_search]', array(
    'default'        => false,
    'capability'     => 'edit_theme_options',
    'type' => 'option',
  ));
  $wp_customize->add_control('aux_header_hide_search', array(
    'label'       => 'Hide Search',
    'description' => 'Do not display the site search form in the header.',
    'section'     => 'aux_header_settings',
    'settings'    => 'aux_header[hide_search]',
    'type'        => 'checkbox',
  ));

  $wp_customize->add_setting('aux_header[basic_navigation]', array(
    'default'        => false,
    'capability'     => 'edit_theme_options',
    'type' => 'option',
  ));
  $wp_customize->add_control('aux_header_basic_navigation', array(
    'label'       => 'Basic Navigation',
    'description' => 'Limit main navigation menu display to one level',
    'section'     => 'aux_header_settings',
    'settings'    => 'aux_header[basic_navigation]',
    'type'        => 'checkbox',
  ));

  // Add a setting for the Footer Option
  $wp_customize->add_setting('footer_option', array(
    'default'           => 'optionA',
    'sanitize_callback' => 'sanitize_text_field',
  ));

  // Add a control for the Footer Option
  $wp_customize->add_control('footer_option_control', array(
    'label'    => 'Footer Variation',
    'description' => 'Choose a slimmer footer or a larger one for more content. <a href="https://aggieux.tamu.edu/?path=/docs/navigation-footers-about-footers--docs">AUX Footer Documentation</a>',
    'section' => 'footer-settings-section',
    'settings' => 'footer_option',
    'type'     => 'radio',
    'choices' => array(
      'optionA' => 'Footer Option A - slim footer', // Default
      'optionB' => 'Footer Option B - more options, better for more content',
    ),
  ));

  $wp_customize->add_section('footer-settings-section', array(
    'title'       => __('Footer Settings', 'marcomm-aux'),
    'priority'    => 20,
    'capability' => 'edit_theme_options',
  ));

  // Add a setting for the Footer Superheader text field
  $wp_customize->add_setting('footer_superheader', array(
    'default'           => '',
    'sanitize_callback' => 'sanitize_text_field',
  ));

  // Add a control for the Footer Superheader text field
  $wp_customize->add_control('footer_superheader_control', array(
    'label'    => __('Footer Superheader', 'marcomm-aux'),
    'description' => __('Add an optional superheader to be displayed above the Footer Heading.
                        (<strong>Note</strong>: This will not display if the Footer Heading is empty.)', 'marcomm-aux'),
    'section' => 'footer-settings-section',
    'settings' => 'footer_superheader',
    'type'     => 'text',
    'active_callback' => 'aux_header_footer_option_b',
  ));

  // Add a setting for the Footer Heading text field
  $wp_customize->add_setting('footer_heading', array(
    'default'           => '',
    'sanitize_callback' => 'sanitize_text_field',
  ));

  // Add a control for the Footer Heading text field
  $wp_customize->add_control('footer_heading_control', array(
    'label'    => __('Footer Heading', 'marcomm-aux'),
    'description' => __('Add an optional heading to be displayed above the footer content.', 'marcomm-aux'),
    'section' => 'footer-settings-section',
    'settings' => 'footer_heading',
    'type'     => 'text',
    'active_callback' => 'aux_header_footer_option_b',
  ));

  // Add a setting for the Footer Subheader text field
  $wp_customize->add_setting('footer_subheader', array(
    'default'           => '',
    'sanitize_callback' => 'sanitize_text_field',
  ));

  // Add a control for the Footer Subheader text field
  $wp_customize->add_control('footer_subheader_control', array(
    'label'    => __('Footer Subheader', 'marcomm-aux'),
    'description' => __('Add an optional subheader to be displayed below the Footer Heading.
                        (<strong>Note</strong>: This will not display if the Footer Heading is empty.)', 'marcomm-aux'),
    'section' => 'footer-settings-section',
    'settings' => 'footer_subheader',
    'type'     => 'text',
    'active_callback' => 'aux_header_footer_option_b',
  ));
}
add_action('customize_register', 'aux_customize_register');


/**
 * Move the footer widgets to the footer panel
 *
 * @param array $section_args
 * @param string $section_id
 * @param string $sidebar_id
 * @return array
 * @since 1.0.0
 */
function aux_move_footer_widgets($section_args, $section_id, $sidebar_id) {
  if( $sidebar_id === 'footer_address' || $sidebar_id === 'footer_content_1' || $sidebar_id === 'footer_content_2' ) {
    $section_args['panel'] = 'footer_panel';
    $section_args['priority'] = 2;
  }

  return $section_args;
}
add_filter('customizer_widgets_section_args', 'aux_move_footer_widgets', 10, 3);


/**
 * Render the site title for the selective refresh partial.
 *
 * @return void
 */
function aux_customize_partial_blogname() {
	bloginfo('name');
}
