<?php
/**
 * Theme Customizer settings and custom header style output.
 *
 * @package nxt-event-planner
 */

function nxt_event_planner_sanitize_social_url( $url ) {
    $url = trim( (string) $url );
    if ( '' === $url ) {
        return '';
    }

    if ( '#' === $url ) {
        return '#';
    }

    return esc_url_raw( $url, array( 'http', 'https' ) );
}

function nxt_event_planner_customize_register( $wp_customize ) {
    $wp_customize->add_section(
        'nxt_event_planner_header_topbar',
        array(
            'title'       => __( 'Header Top Bar', 'nxt-event-planner' ),
            'description' => __( 'Control address and social links shown in the header top bar.', 'nxt-event-planner' ),
            'priority'    => 35,
        )
    );

    $wp_customize->add_setting(
        'nxt_event_planner_topbar_address',
        array(
            'default'           => '',
            'sanitize_callback' => 'sanitize_text_field',
        )
    );

    $wp_customize->add_control(
        'nxt_event_planner_topbar_address',
        array(
            'label'   => __( 'Address', 'nxt-event-planner' ),
            'section' => 'nxt_event_planner_header_topbar',
            'type'    => 'text',
        )
    );

    $wp_customize->add_setting(
        'nxt_event_planner_topbar_instagram',
        array(
            'default'           => '#',
            'sanitize_callback' => 'nxt_event_planner_sanitize_social_url',
        )
    );

    $wp_customize->add_control(
        'nxt_event_planner_topbar_instagram',
        array(
            'label'   => __( 'Instagram URL', 'nxt-event-planner' ),
            'section' => 'nxt_event_planner_header_topbar',
            'type'    => 'url',
        )
    );

    $wp_customize->add_setting(
        'nxt_event_planner_topbar_facebook',
        array(
            'default'           => '#',
            'sanitize_callback' => 'nxt_event_planner_sanitize_social_url',
        )
    );

    $wp_customize->add_control(
        'nxt_event_planner_topbar_facebook',
        array(
            'label'   => __( 'Facebook URL', 'nxt-event-planner' ),
            'section' => 'nxt_event_planner_header_topbar',
            'type'    => 'url',
        )
    );

    $wp_customize->add_setting(
        'nxt_event_planner_topbar_youtube',
        array(
            'default'           => '#',
            'sanitize_callback' => 'nxt_event_planner_sanitize_social_url',
        )
    );

    $wp_customize->add_control(
        'nxt_event_planner_topbar_youtube',
        array(
            'label'   => __( 'YouTube URL', 'nxt-event-planner' ),
            'section' => 'nxt_event_planner_header_topbar',
            'type'    => 'url',
        )
    );

    /* --- Header Colors ------------------------------------------------ */
    $wp_customize->add_section(
        'nxt_event_planner_header_colors',
        array(
            'title'       => __( 'Header Colors', 'nxt-event-planner' ),
            'description' => __( 'Recolor the header to match your imported template. Leave blank to keep the default dark style.', 'nxt-event-planner' ),
            'priority'    => 36,
        )
    );

    $nxt_event_planner_header_color_settings = array(
        'nxt_event_planner_header_bg'          => __( 'Header Background', 'nxt-event-planner' ),
        'nxt_event_planner_header_text'        => __( 'Header Text & Icons', 'nxt-event-planner' ),
        'nxt_event_planner_header_accent'      => __( 'Accent / Hover Color', 'nxt-event-planner' ),
        'nxt_event_planner_header_dropdown_bg' => __( 'Dropdown / Mobile Menu Background', 'nxt-event-planner' ),
    );

    foreach ( $nxt_event_planner_header_color_settings as $nxt_event_planner_setting_id => $nxt_event_planner_setting_label ) {
        $wp_customize->add_setting(
            $nxt_event_planner_setting_id,
            array(
                'default'           => '',
                'sanitize_callback' => 'sanitize_hex_color',
            )
        );

        $wp_customize->add_control(
            new WP_Customize_Color_Control(
                $wp_customize,
                $nxt_event_planner_setting_id,
                array(
                    'label'   => $nxt_event_planner_setting_label,
                    'section' => 'nxt_event_planner_header_colors',
                )
            )
        );
    }

    /* --- Site Loader -------------------------------------------------- */
    $wp_customize->add_section(
        'nxt_event_planner_site_loader',
        array(
            'title'       => __( 'Site Loader', 'nxt-event-planner' ),
            'description' => __( 'Preloader shown while the page loads.', 'nxt-event-planner' ),
            'priority'    => 37,
        )
    );

    $wp_customize->add_setting(
        'nxt_event_planner_loader_enable',
        array(
            'default'           => true,
            'sanitize_callback' => 'nxt_event_planner_sanitize_checkbox',
        )
    );

    $wp_customize->add_control(
        'nxt_event_planner_loader_enable',
        array(
            'label'   => __( 'Enable site loader', 'nxt-event-planner' ),
            'section' => 'nxt_event_planner_site_loader',
            'type'    => 'checkbox',
        )
    );

    $wp_customize->add_setting(
        'nxt_event_planner_loader_bg_color',
        array(
            'default'           => '#ffffff',
            'sanitize_callback' => 'sanitize_hex_color',
        )
    );

    $wp_customize->add_control(
        new WP_Customize_Color_Control(
            $wp_customize,
            'nxt_event_planner_loader_bg_color',
            array(
                'label'   => __( 'Loader Background Color', 'nxt-event-planner' ),
                'section' => 'nxt_event_planner_site_loader',
            )
        )
    );

    $wp_customize->add_setting(
        'nxt_event_planner_loader_image',
        array(
            'default'           => get_template_directory_uri() . '/assets/img/loader-logo.webp',
            'sanitize_callback' => 'esc_url_raw',
        )
    );

    $wp_customize->add_control(
        new WP_Customize_Image_Control(
            $wp_customize,
            'nxt_event_planner_loader_image',
            array(
                'label'   => __( 'Loader Image', 'nxt-event-planner' ),
                'section' => 'nxt_event_planner_site_loader',
            )
        )
    );
}
add_action( 'customize_register', 'nxt_event_planner_customize_register' );

function nxt_event_planner_sanitize_checkbox( $checked ) {
    return ( isset( $checked ) && true === (bool) $checked );
}

// Output the site loader markup at the top of <body>.
function nxt_event_planner_render_site_loader() {
    if ( ! get_theme_mod( 'nxt_event_planner_loader_enable', true ) ) {
        return;
    }

    $nxt_event_planner_loader_bg    = get_theme_mod( 'nxt_event_planner_loader_bg_color', '#ffffff' );
    $nxt_event_planner_loader_image = get_theme_mod( 'nxt_event_planner_loader_image', get_template_directory_uri() . '/assets/img/loader-logo.webp' );
    ?>
    <div id="nxt-site-loader" class="nxt-site-loader" style="background-color: <?php echo esc_attr( $nxt_event_planner_loader_bg ); ?>;">
        <div class="nxt-site-loader__inner">
            <?php if ( $nxt_event_planner_loader_image ) : ?>
                <img class="nxt-site-loader__image" src="<?php echo esc_url( $nxt_event_planner_loader_image ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>">
            <?php endif; ?>
            <span class="nxt-site-loader__spinner" aria-hidden="true"></span>
        </div>
    </div>
    <?php
}
add_action( 'wp_body_open', 'nxt_event_planner_render_site_loader' );

// Final, direct output of header styles.
function nxt_event_planner_custom_header_style() {
    $nxt_event_planner_css = '';

    // Hide header text if disabled. Text color is handled by the --hdr-text
    // token (see "Header Colors" section) so the Customizer color picker wins.
    if ( ! display_header_text() ) {
        $nxt_event_planner_css .= '.site-title, .site-description { position: absolute; clip: rect(1px, 1px, 1px, 1px); }';
    }

    // Header color tokens (override CSS variable defaults).
    $nxt_event_planner_header_vars = array(
        '--hdr-bg'          => get_theme_mod( 'nxt_event_planner_header_bg' ),
        '--hdr-text'        => get_theme_mod( 'nxt_event_planner_header_text' ),
        '--hdr-accent'      => get_theme_mod( 'nxt_event_planner_header_accent' ),
        '--hdr-submenu-bg'  => get_theme_mod( 'nxt_event_planner_header_dropdown_bg' ),
    );

    $nxt_event_planner_var_css = '';
    foreach ( $nxt_event_planner_header_vars as $nxt_event_planner_var_name => $nxt_event_planner_var_value ) {
        if ( $nxt_event_planner_var_value ) {
            $nxt_event_planner_var_css .= $nxt_event_planner_var_name . ': ' . esc_attr( $nxt_event_planner_var_value ) . ';';
        }
    }
    if ( '' !== $nxt_event_planner_var_css ) {
        $nxt_event_planner_css .= '.site-header { ' . $nxt_event_planner_var_css . ' }';
    }

    // Header background image (takes precedence over background color var).
    $nxt_event_planner_header_image = get_header_image();
    if ( $nxt_event_planner_header_image ) {
        $nxt_event_planner_css .= '.site-header { background-image: url(' . esc_url( $nxt_event_planner_header_image ) . '); background-size: cover; background-position: center center; }';
    }

    if ( ! empty( $nxt_event_planner_css ) ) {
        echo '<style type="text/css">' . $nxt_event_planner_css . '</style>';
    }
}
add_action( 'wp_head', 'nxt_event_planner_custom_header_style', 99 );
