<?php
/**
 *  Footer type template
 *
 * @package wpstream-theme
 */

// Define the class definitions based on footer types.
$footer_classes_sidebars = array(
	'4-equal-columns'               => array(
		'footer-1'      => 'col-12 col-md-6 col-lg-3',
		'show-footer-1' => 'yes',
		'footer-2'      => 'col-12 col-md-6 col-lg-3',
		'show-footer-2' => 'yes',
		'footer-3'      => 'col-12 col-md-6 col-lg-3',
		'show-footer-3' => 'yes',
		'footer-4'      => 'col-12 col-md-6 col-lg-3',
		'show-footer-4' => 'yes',
	),
	'3-equal-columns'               => array(
		'footer-1'      => 'col-12 col-md-6 col-lg-4',
		'show-footer-1' => 'yes',
		'footer-2'      => 'col-12 col-md-6 col-lg-4',
		'show-footer-2' => 'yes',
		'footer-3'      => 'col-12 col-md-6 col-lg-4',
		'show-footer-3' => 'yes',
	),
	'2-equal-columns'               => array(
		'footer-1'      => 'col-12 col-md-6 col-lg-6',
		'show-footer-1' => 'yes',
		'footer-2'      => 'col-12 col-md-6 col-lg-6',
		'show-footer-2' => 'yes',
	),
	'100%_width-column'             => array(
		'footer-1'      => 'col-12 col-md-6 col-lg-12',
		'show-footer-1' => 'yes',
	),
	'3-columns-1/2-1/4-1/4'         => array(
		'footer-1'      => 'col-12 col-md-6 col-lg-6',
		'show-footer-1' => 'yes',
		'footer-2'      => 'col-12 col-md-6 col-lg-3',
		'show-footer-2' => 'yes',
		'footer-3'      => 'col-12 col-md-6 col-lg-3',
		'show-footer-3' => 'yes',
	),
	'3-columns-1/4-1/2-1/4'         => array(
		'footer-1'      => 'col-12 col-md-6 col-lg-3',
		'show-footer-1' => 'yes',
		'footer-2'      => 'col-12 col-md-6 col-lg-6',
		'show-footer-2' => 'yes',
		'footer-3'      => 'col-12 col-md-6 col-lg-3',
		'show-footer-3' => 'yes',
	),
	'3-columns-1/4-1/4-1/2'         => array(
		'footer-1'      => 'col-12 col-md-6 col-lg-3',
		'show-footer-1' => 'yes',
		'footer-2'      => 'col-12 col-md-6 col-lg-3',
		'show-footer-2' => 'yes',
		'footer-3'      => 'col-12 col-md-6 col-lg-6',
		'show-footer-3' => 'yes',
	),
	'5-columns-1/2-1/2-1/2-1/2-1/4' => array(
		'footer-1'      => 'col-12 col-md-6 col-lg-2',
		'show-footer-1' => 'yes',
		'footer-2'      => 'col-12 col-md-6 col-lg-2',
		'show-footer-2' => 'yes',
		'footer-3'      => 'col-12 col-md-6 col-lg-2',
		'show-footer-3' => 'yes',
		'footer-4'      => 'col-12 col-md-6 col-lg-2',
		'show-footer-4' => 'yes',
		'footer-5'      => 'col-12 col-md-6 col-lg-4',
		'show-footer-5' => 'yes',
	),
	'2-columns-2/3-1/3'             => array(
		'footer-1'      => 'col-12 col-md-6 col-lg-8',
		'show-footer-1' => 'yes',
		'footer-2'      => 'col-12 col-md-6 col-lg-4',
		'show-footer-2' => 'yes',
	),
	'2-columns-1/3-2/3'             => array(
		'footer-1'      => 'col-12 col-md-6 col-lg-4',
		'show-footer-1' => 'yes',
		'footer-2'      => 'col-12 col-md-6 col-lg-8',
		'show-footer-2' => 'yes',
	),
);

$wpstream_theme_footer_type = get_theme_mod( 'wpstream_theme_footer_type', '4-equal-columns' );

// Determine the number of columns based on the selected type.
// phpcs:ignore
$num_columns = 4; // Default for '4-equal-columns'.

if ( '3-equal-columns' === $wpstream_theme_footer_type || '3-columns-1/2-1/4-1/4' === $wpstream_theme_footer_type || '3-columns-1/4-1/2-1/4' === $wpstream_theme_footer_type || '3-columns-1/4-1/4-1/2' === $wpstream_theme_footer_type ) {
	$num_columns = 3;
}

if ( '2-equal-columns' === $wpstream_theme_footer_type || '2-columns-2/3-1/3' === $wpstream_theme_footer_type || '2-columns-1/3-2/3' === $wpstream_theme_footer_type ) {
	$num_columns = 2;
}

if ( '100%_width-column' === $wpstream_theme_footer_type ) {
	$num_columns = 1;
}

if ( '5-columns-1/2-1/2-1/2-1/2-1/4' === $wpstream_theme_footer_type ) {
	$num_columns = 5;
}

$footer_widget_areas = array( 'footer-1', 'footer-2', 'footer-3', 'footer-4' );

if ( isset( $footer_classes_sidebars[ $wpstream_theme_footer_type ] ) ) {
	$footer_classes = $footer_classes_sidebars[ $wpstream_theme_footer_type ];
}

if ( isset( $footer_classes ) ) {
	for ( $i = 1; $i <= $num_columns; $i++ ) {
		$widget_area = 'footer-' . $i;

		// Check if 'show-footer-' . $i exists and is set to 'yes'.
		$show_column = isset( $footer_classes[ 'show-footer-' . $i ] ) && 'yes' === $footer_classes[ 'show-footer-' . $i ];

		if ( $show_column ) {
			$current_class = $footer_classes[ $widget_area ] ?? '';

			echo '<div class="' . esc_attr( $current_class ) . '">';

			if ( is_active_sidebar( $widget_area ) ) {
				dynamic_sidebar( $widget_area );
			}

			echo '</div>';
		}
	}
}