<?php
/**
 * Generic section template for the single-page layout.
 *
 * Expects a `tents_singlepage_section` variable set via set_query_var().
 *
 * @var array $tents_singlepage_section
 */

if ( ! isset( $tents_singlepage_section ) || ! is_array( $tents_singlepage_section ) ) {
	return;
}

$section_id = isset( $tents_singlepage_section['id'] ) ? $tents_singlepage_section['id'] : '';
$title      = isset( $tents_singlepage_section['title'] ) ? $tents_singlepage_section['title'] : '';
$content    = isset( $tents_singlepage_section['content'] ) ? $tents_singlepage_section['content'] : '';
$page_id    = isset( $tents_singlepage_section['page_id'] ) ? (int) $tents_singlepage_section['page_id'] : 0;

// Background image: Customizer setting per sezione (se esiste), altrimenti immagine in evidenza della pagina.
$slug         = isset( $tents_singlepage_section['slug'] ) ? $tents_singlepage_section['slug'] : '';
$bg_setting   = 'section_' . sanitize_title( $slug ) . '_bg_image';
$bg_color_setting = 'section_' . sanitize_title( $slug ) . '_bg_color';
$text_color_setting = 'section_' . sanitize_title( $slug ) . '_text_color';
$overlay_color_setting = 'section_' . sanitize_title( $slug ) . '_overlay_color';
$overlay_opacity_setting = 'section_' . sanitize_title( $slug ) . '_overlay_opacity';
$banner_text_setting = 'section_' . sanitize_title( $slug ) . '_banner_text';
$hide_title_setting  = 'section_' . sanitize_title( $slug ) . '_hide_title';
$bg_image_value = get_theme_mod( $bg_setting );
$bg_color     = get_theme_mod( $bg_color_setting );
$text_color   = get_theme_mod( $text_color_setting );
$overlay_color = get_theme_mod( $overlay_color_setting, '#000000' );
$overlay_opacity = get_theme_mod( $overlay_opacity_setting, '0.4' );
$banner_text     = sanitize_text_field( get_theme_mod( $banner_text_setting, '' ) );
$hide_title      = (bool) get_theme_mod( $hide_title_setting, 0 );
$bg_image_url = '';

if ( $bg_image_value ) {
	// Customizer può salvare un URL oppure un ID attachment.
	if ( is_numeric( $bg_image_value ) ) {
		$bg_image_url = wp_get_attachment_image_url( (int) $bg_image_value, 'full' );
	} else {
		$bg_image_url = (string) $bg_image_value;
	}
} elseif ( $page_id && has_post_thumbnail( $page_id ) ) {
	$bg_image_url = get_the_post_thumbnail_url( $page_id, 'full' );
}

if ( $bg_image_url && function_exists( 'tents_singlepage_normalize_local_asset_url' ) ) {
	$bg_image_url = tents_singlepage_normalize_local_asset_url( $bg_image_url );
}

$has_bg = ! empty( $bg_image_url );
$has_color = ! empty( $bg_color ) && ! $has_bg;
$has_text_color = ! empty( $text_color );
$has_banner = '' !== $banner_text;
$has_content = '' !== trim( wp_strip_all_tags( $content ) );
$show_title  = $title && ! $hide_title;
$stack_buttons = function_exists( 'tents_singlepage_get_section_stack_buttons' )
	? tents_singlepage_get_section_stack_buttons( $slug )
	: array();
$has_stack_buttons = ! empty( $stack_buttons );

// Overlay: compute rgba color so opacity=0 is truly transparent.
$overlay_opacity_float = floatval( $overlay_opacity );
if ( $overlay_opacity_float < 0 ) {
	$overlay_opacity_float = 0;
}
if ( $overlay_opacity_float > 1 ) {
	$overlay_opacity_float = 1;
}

$overlay_rgb = array( 0, 0, 0 );
if ( is_string( $overlay_color ) && preg_match( '/^#([0-9a-fA-F]{6})$/', $overlay_color, $m ) ) {
	$hex = $m[1];
	$overlay_rgb = array(
		hexdec( substr( $hex, 0, 2 ) ),
		hexdec( substr( $hex, 2, 2 ) ),
		hexdec( substr( $hex, 4, 2 ) ),
	);
}
$overlay_rgba = sprintf( 'rgba(%d,%d,%d,%.3f)', $overlay_rgb[0], $overlay_rgb[1], $overlay_rgb[2], $overlay_opacity_float );

$style_parts = array();
if ( $has_color ) {
	$style_parts[] = 'background-color: ' . $bg_color;
}
if ( $has_text_color ) {
	$style_parts[] = 'color: ' . $text_color;
}
$section_style_attr = ! empty( $style_parts ) ? ' style="' . esc_attr( implode( '; ', $style_parts ) ) . ';"' : '';
?>

<section id="<?php echo esc_attr( $section_id ); ?>" class="singlepage-section<?php echo $has_bg ? ' singlepage-section--has-bg' : ''; ?><?php echo $has_color ? ' singlepage-section--has-color' : ''; ?><?php echo $has_text_color ? ' singlepage-section--has-text-color' : ''; ?><?php echo $has_banner ? ' singlepage-section--has-banner' : ''; ?><?php echo ! $has_content ? ' singlepage-section--no-content' : ''; ?><?php echo $has_stack_buttons ? ' singlepage-section--has-stack-buttons' : ''; ?>"<?php echo $section_style_attr; ?>>
	<?php if ( $has_bg ) : ?>
		<div class="singlepage-section__bg" style="background-image: url('<?php echo esc_url( $bg_image_url ); ?>');"></div>
		<?php if ( $overlay_opacity_float > 0 ) : ?>
			<div class="singlepage-section__overlay" style="background-color: <?php echo esc_attr( $overlay_rgba ); ?>;"></div>
		<?php endif; ?>
	<?php endif; ?>

	<?php if ( $has_stack_buttons ) : ?>
		<div class="singlepage-section__stack-buttons">
			<?php foreach ( $stack_buttons as $stack_button ) : ?>
				<a
					class="singlepage-section__stack-button"
					href="<?php echo esc_url( $stack_button['url'] ); ?>"
					style="<?php echo esc_attr( sprintf( '--section-button-bg:%1$s;--section-button-text:%2$s;', $stack_button['bg_color'], $stack_button['text_color'] ) ); ?>"
				>
					<?php echo esc_html( $stack_button['label'] ); ?>
				</a>
			<?php endforeach; ?>
		</div>
	<?php endif; ?>

	<div class="singlepage-section__inner">
		<div class="singlepage-section__content">
			<?php if ( $show_title ) : ?>
				<h2 class="singlepage-section__title"<?php echo $has_text_color ? ' style="color: ' . esc_attr( $text_color ) . ';"' : ''; ?>>
					<?php echo esc_html( $title ); ?>
				</h2>
			<?php endif; ?>

			<?php if ( $content ) : ?>
				<div class="singlepage-section__body">
					<?php echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
				</div>
			<?php endif; ?>
		</div>
	</div>

	<?php if ( $has_banner ) : ?>
		<div class="singlepage-section__banner">
			<p class="singlepage-section__banner-text"><?php echo esc_html( $banner_text ); ?></p>
		</div>
	<?php endif; ?>
</section>

