<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
/**
 * Dynamic render for brentwood/video-hero.
 *
 * The media area is one of two modes (mediaMode):
 *   - "video"    — a full-bleed autoplay video (or its poster as a static image).
 *   - "carousel" — a crossfading slideshow of images with dot navigation, like
 *     the live brentwood.ca image hero. view.js (brentwood-video-hero-view)
 *     drives autoplay + dots; with one slide or JS off it just shows slide 1.
 *
 * The editable white text card is InnerBlocks ($content) in both modes.
 *
 * @var array  $attributes
 * @var string $content  Rendered inner blocks (heading + paragraphs).
 */
$poster = $attributes['poster'] ?? '';
$video  = $attributes['video'] ?? '';
$mode   = ( 'carousel' === ( $attributes['mediaMode'] ?? 'video' ) ) ? 'carousel' : 'video';
// Width: 'full' (default, full-bleed 100vw) or 'theme' (constrained to the
// theme content width).
$width  = ( 'theme' === ( $attributes['width'] ?? 'full' ) ) ? ' bw-hero--theme' : '';

// Card options mirror brentwood/hero-card: content alignment + a closing dash.
$align_ok      = array( 'left', 'center', 'right' );
$content_align = in_array( $attributes['contentAlign'] ?? 'left', $align_ok, true ) ? $attributes['contentAlign'] : 'left';
$dash_align    = in_array( $attributes['dashAlign'] ?? 'left', $align_ok, true ) ? $attributes['dashAlign'] : 'left';
$show_dash     = ! isset( $attributes['showDash'] ) || ! empty( $attributes['showDash'] );

$dash_html = $show_dash
    ? '<img class="bw-hero__dash bw-hero__dash--' . esc_attr( $dash_align ) . '" src="'
        . esc_url( get_stylesheet_directory_uri() . '/assets/dash.svg' ) . '" alt="" aria-hidden="true">'
    : '';

// The white text card (InnerBlocks $content + optional dash), shared by both modes.
$card_html = '<div class="bw-hero__card" style="text-align:' . esc_attr( $content_align ) . '">' . $content . $dash_html . '</div>';

$slides = array();
if ( 'carousel' === $mode && is_array( $attributes['slides'] ?? null ) ) {
    foreach ( $attributes['slides'] as $s ) {
        $url = esc_url_raw( $s['url'] ?? '' );
        if ( '' === $url ) { continue; }
        $slides[] = array( 'url' => $url, 'alt' => sanitize_text_field( $s['alt'] ?? '' ) );
    }
}

if ( 'carousel' === $mode && ! empty( $slides ) ) {
    // Interval in seconds (0 = no autoplay); clamp to a sane range.
    $interval = max( 0, min( 30, (int) ( $attributes['interval'] ?? 5 ) ) );

    $slides_html = '';
    foreach ( $slides as $i => $s ) {
        $slides_html .= sprintf(
            '<div class="bw-hero__slide%s" role="img" aria-label="%s" style="background-image:url(\'%s\')"></div>',
            0 === $i ? ' is-active' : '',
            esc_attr( $s['alt'] ),
            esc_url( $s['url'] )
        );
    }

    $dots_html = '';
    if ( count( $slides ) > 1 ) {
        $dots_html = '<div class="bw-hero__dots" role="tablist" aria-label="' . esc_attr__( 'Hero slides', 'kadence-child' ) . '">';
        foreach ( $slides as $i => $s ) {
            $dots_html .= sprintf(
                '<button type="button" class="bw-hero__dot%s" role="tab" aria-selected="%s" aria-label="%s" data-i="%d"></button>',
                0 === $i ? ' is-active' : '',
                0 === $i ? 'true' : 'false',
                esc_attr( sprintf( __( 'Go to slide %d', 'kadence-child' ), $i + 1 ) ),
                $i
            );
        }
        $dots_html .= '</div>';
    }

    // Only the carousel needs the view script; load it on demand.
    wp_enqueue_script( 'brentwood-video-hero-view' );

    $media = '<div class="bw-hero__carousel" data-interval="' . ( $interval * 1000 ) . '">'
        . '<div class="bw-hero__slides">' . $slides_html . '</div>'
        . $dots_html
        . '</div>';

    echo '<div class="bw-hero bw-hero--carousel' . esc_attr( $width ) . '"><div class="bw-hero__media">' . $media . '</div>'
        . $card_html . '</div>';
    return;
}

if ( $video ) {
    $ext  = strtolower( pathinfo( wp_parse_url( $video, PHP_URL_PATH ) ?? '', PATHINFO_EXTENSION ) );
    $type = in_array( $ext, array( 'webm', 'ogv', 'mov' ), true )
        ? array( 'webm' => 'video/webm', 'ogv' => 'video/ogg', 'mov' => 'video/quicktime' )[ $ext ]
        : 'video/mp4';
    $media = sprintf(
        '<video class="bw-hero__video" autoplay muted loop playsinline preload="metadata"%s><source src="%s" type="%s"></video>',
        $poster ? ' poster="' . esc_url( $poster ) . '"' : '',
        esc_url( $video ),
        esc_attr( $type )
    );
} elseif ( $poster ) {
    $media = sprintf( '<div class="bw-hero__video" style="background-image:url(\'%s\')"></div>', esc_url( $poster ) );
} else {
    $media = '<div class="bw-hero__video"></div>';
}

echo '<div class="bw-hero' . esc_attr( $width ) . '"><div class="bw-hero__media">' . $media . '</div>'
    . $card_html . '</div>';
