<?php
/**
 * RSM Footer front-end template.
 *
 * @package Rsmspark_Content_Widgets
 * @var array<string, mixed> $brand
 * @var string               $brand_logo_url
 * @var array<int, mixed>    $social
 * @var array<int, mixed>    $columns
 * @var string               $copyright
 * @var string               $inline_style
 * @var int                  $column_count
 * @var bool                 $has_brand
 * @var bool                 $has_columns
 * @var bool                 $has_copyright
 * @var string               $region
 * @var string               $footer_class
 * @var string               $placement
 * @var string               $reveal_mode
 * @var bool                 $back_to_top
 * @var bool                 $is_sticky
 */

defined( 'ABSPATH' ) || exit;

$uid           = 'rsmspark-footer-' . wp_unique_id();
$footer_class  = ! empty( $footer_class ) ? (string) $footer_class : 'rsmspark-footer';
$region        = ! empty( $region ) ? (string) $region : 'default';
$placement     = ! empty( $placement ) ? (string) $placement : 'static';
$reveal_mode   = ! empty( $reveal_mode ) ? (string) $reveal_mode : 'always';
$needs_sticky  = ! empty( $is_sticky ) || ! empty( $back_to_top );

if ( empty( $has_brand ) && empty( $has_columns ) && empty( $has_copyright ) ) {
	return;
}
?>
<div
	id="<?php echo esc_attr( $uid ); ?>"
	class="<?php echo esc_attr( $footer_class ); ?>"
	style="<?php echo esc_attr( $inline_style ); ?>"
	data-rsmspark-footer="1"
	data-rsmspark-footer-region="<?php echo esc_attr( $region ); ?>"
	data-rsmspark-footer-placement="<?php echo esc_attr( $placement ); ?>"
	data-rsmspark-footer-reveal="<?php echo esc_attr( $reveal_mode ); ?>"
	<?php echo $needs_sticky ? 'data-rsmspark-footer-sticky="1"' : ''; ?>
>
	<div class="rsmspark-footer__inner">
		<div
			class="rsmspark-footer__grid"
			style="<?php echo esc_attr( '--rf-footer-columns:' . (int) $column_count . ';' ); ?>"
		>
			<?php if ( ! empty( $has_brand ) ) : ?>
				<div class="rsmspark-footer__brand">
					<?php if ( ! empty( $brand['name'] ) || ! empty( $brand_logo_url ) ) : ?>
						<div class="rsmspark-footer__brand-head">
							<?php if ( ! empty( $brand_logo_url ) ) : ?>
								<img
									class="rsmspark-footer__brand-logo"
									src="<?php echo esc_url( $brand_logo_url ); ?>"
									alt=""
									loading="lazy"
									decoding="async"
								/>
							<?php endif; ?>
							<?php if ( ! empty( $brand['name'] ) ) : ?>
								<span class="rsmspark-footer__brand-name"><?php echo esc_html( $brand['name'] ); ?></span>
							<?php endif; ?>
						</div>
					<?php endif; ?>

					<?php if ( ! empty( $brand['description'] ) ) : ?>
						<p class="rsmspark-footer__brand-text"><?php echo esc_html( $brand['description'] ); ?></p>
					<?php endif; ?>

					<?php if ( ! empty( $social ) ) : ?>
						<ul class="rsmspark-footer__social" aria-label="<?php esc_attr_e( 'Social links', 'rsm-content-widgets' ); ?>">
							<?php foreach ( $social as $social_item ) : ?>
								<li>
									<a
										class="rsmspark-footer__social-link"
										href="<?php echo esc_url( $social_item['url'] ); ?>"
										<?php echo ! empty( $social_item['label'] ) ? 'aria-label="' . esc_attr( $social_item['label'] ) . '"' : ''; ?>
										target="_blank"
										rel="noopener noreferrer"
									>
										<?php echo wp_kses_post( Rsmspark_Content_Widgets_Widget_Footer::social_icon_html( $social_item['icon'] ) ); ?>
									</a>
								</li>
							<?php endforeach; ?>
						</ul>
					<?php endif; ?>
				</div>
			<?php endif; ?>

			<?php foreach ( $columns as $column_index => $column ) : ?>
				<?php
				$is_last_column = ( $column_index === count( $columns ) - 1 );
				$show_copyright = $is_last_column && ! empty( $has_copyright );
				?>
				<div class="rsmspark-footer__column rsmspark-footer__column--<?php echo esc_attr( $column['type'] ); ?>">
					<?php if ( ! empty( $column['title'] ) ) : ?>
						<h2 class="rsmspark-footer__column-title"><?php echo esc_html( $column['title'] ); ?></h2>
					<?php endif; ?>

					<?php if ( 'text' === $column['type'] ) : ?>
						<?php if ( ! empty( $column['body'] ) ) : ?>
							<p class="rsmspark-footer__column-text"><?php echo esc_html( $column['body'] ); ?></p>
						<?php endif; ?>
						<?php if ( $show_copyright ) : ?>
							<p class="rsmspark-footer__copyright"><?php echo esc_html( $copyright ); ?></p>
						<?php endif; ?>
					<?php elseif ( ! empty( $column['links'] ) ) : ?>
						<ul class="rsmspark-footer__links">
							<?php foreach ( $column['links'] as $link ) : ?>
								<?php
								$link_href = esc_url( $link['url'] );
								if ( '' === $link_href && ! empty( $link['url'] ) ) {
									$link_href = esc_attr( $link['url'] );
								}
								?>
								<li>
									<a class="rsmspark-footer__link" href="<?php echo esc_attr( $link_href ); ?>">
										<?php echo esc_html( $link['label'] ); ?>
									</a>
								</li>
							<?php endforeach; ?>
						</ul>
					<?php endif; ?>
				</div>
			<?php endforeach; ?>
		</div>

		<?php
		$copyright_in_column = false;
		if ( ! empty( $columns ) && ! empty( $has_copyright ) ) {
			$last                  = $columns[ count( $columns ) - 1 ];
			$copyright_in_column = ( 'text' === ( $last['type'] ?? '' ) );
		}
		?>
		<?php if ( ! empty( $has_copyright ) && ! $copyright_in_column ) : ?>
			<p class="rsmspark-footer__copyright rsmspark-footer__copyright--solo"><?php echo esc_html( $copyright ); ?></p>
		<?php endif; ?>

		<?php if ( ! empty( $back_to_top ) ) : ?>
			<button
				type="button"
				class="rsmspark-footer__back-to-top"
				aria-label="<?php esc_attr_e( 'Back to top', 'rsm-content-widgets' ); ?>"
			>
				<span class="dashicons dashicons-arrow-up-alt2" aria-hidden="true"></span>
			</button>
		<?php endif; ?>
	</div>
</div>
