<?php
/**
 * Enqueue child styles.
 */
function child_enqueue_styles() {
	wp_enqueue_style( 'child-theme', get_stylesheet_directory_uri() . '/style.css', array(), 107 );
	wp_enqueue_script( 'bw-fade-in', get_stylesheet_directory_uri() . '/assets/js/fade-in.js', array(), 106 );

	//if ( is_user_logged_in() && $_GET['selfbook'] === 'true' ) {
	//	wp_enqueue_style( 'bw-encore', get_stylesheet_directory_uri() . '/assets/css/encore.css', array(), 105 );
	//	wp_enqueue_script( 'bw-encore', get_stylesheet_directory_uri() . '/assets/js/encore.js', array(), 105 );
	//}

	//wp_enqueue_script('bw-promocode', get_stylesheet_directory_uri() . '/assets/js/promocode.js', array(), 105, true);
    // Check if user is logged in
    //$is_user_logged_in = is_user_logged_in() ? 'yes' : 'no';

    // Localize script with user login status
	//wp_localize_script('bw-promocode', 'userStatus', array(
    //	'isLoggedIn' => $is_user_logged_in,
	//));
}
add_action( 'wp_enqueue_scripts', 'child_enqueue_styles' );

function bw_block_editor_assets() {
	wp_enqueue_style( 'bw_block_editor_style', get_stylesheet_directory_uri() . '/assets/css/block-editor.css', array(), 100 );
}
add_action( 'enqueue_block_editor_assets', 'bw_block_editor_assets' );

require_once 'acf-blocks/index.php';
require_once 'includes/extend_theme_customizer.php';
require_once 'includes/filters.php';
require_once 'includes/header_search_bar.php';

add_action( 'wp_enqueue_scripts', 'child_enqueue_styles' );

// Sidebars
add_action( 'widgets_init', 'bw_sidebar' );
function bw_sidebar() {
	$bw_sidebars = array(
		array(
			'name'          => 'The Resort',
			'id'            => 'the-resort-widget-area',
			'description'   => 'Widgets shown on resort pages',
		),
		array(
			'name'          => 'St. Martin',
			'id'            => 'stmartin-widget-area',
			'description'   => 'Widgets shown on St. Martin pages',
		),
	);

	$defaults = array(
		'name'          => 'BW Sidebar',
		'id'            => 'bw-sidebar',
		'description'   => 'Sidebar is shown on the left hand of this web',
		'class'         => '',
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget'  => '</li>',
		'before_title'  => '<h2 class="widgettitle">',
		'after_title'   => '</h2>' 
	);

	foreach( $bw_sidebars as $sidebar ) {
		$args = wp_parse_args( $sidebar, $defaults );
		register_sidebar( $args );
	}
}

function custom_check_availability_button_shortcode($atts) {
    // Check if the user is logged in
    if (is_user_logged_in()) {
        // Link for logged-in users
        $button_link = "/?selfbook=true&amp;hotelId=13689&promo=Encore";
    } else {
        // Default link for logged-out users
        $button_link = "/?selfbook=true&amp;hotelId=13689";
    }

    // Default text as English
    $button_text = "CHECK AVAILABILITY";

    // If WPML is active and the current language is French, adjust the text
    if (defined('ICL_LANGUAGE_CODE') && ICL_LANGUAGE_CODE == 'fr') {
        $button_text = "VÉRIFIER LA DISPONIBILITÉ";
    }

    // Kadence button structure with the link and dynamic text
    $button_html = '<div class="site-header-item site-header-focus-item" data-section="kadence_customizer_header_button2">';
    $button_html .= '<div class="header-button2-wrap"><div class="header-button-inner-wrap">';
    $button_html .= '<a href="' . esc_url($button_link) . '" target="_self" class="button header-button2 button-size-medium button-style-filled">' . esc_html($button_text) . '</a>';
    $button_html .= '</div></div></div>';

    return $button_html;
}
//add_shortcode('check_booking_button', 'custom_check_availability_button_shortcode');


// Addition to WPML
function custom_special_offers_button_shortcode($atts) {
    // Default to English
    $button_link = "/specials/";
    $button_text = "SPECIAL OFFERS";

    // Check for WPML's defined language code
    if (defined('ICL_LANGUAGE_CODE')) {
        if (ICL_LANGUAGE_CODE == 'fr') {
            $button_link = "/fr/specials/";
            $button_text = "OFFRES SPÉCIALES";
        }
        // Extend this for other languages if necessary
    }

    // Kadence button structure with dynamic link and text
    $button_html = '<div class="site-header-item site-header-focus-item" data-section="kadence_customizer_header_button">';
    $button_html .= '<div class="header-button-wrap"><div class="header-button-inner-wrap">';
    $button_html .= '<a href="' . $button_link . '" target="_self" class="button header-button button-size-medium button-style-filled special-offer">' . $button_text . '</a>';
    $button_html .= '</div></div></div>';

    return $button_html;
}
//add_shortcode('special_offers_button', 'custom_special_offers_button_shortcode');

// Scroll Down Indicator
function bw_scroll_down_indicator() {
    global $post;

    // Check if the custom class exists in the page content
    if ( $post && strpos($post->post_content, 'bw-scroll-indicator-row') !== false ) {
        ?>
        <style>
            .bw-scroll-indicator {
                position: fixed;
                bottom: 5vh;
                left: 50%;
                transform: translateX(-50%);
                animation: bwPulse 1.5s infinite;
                opacity: 0.8;
                z-index: 9999;
                display: block; /* Initially visible */
            }

            @keyframes bwPulse {
                0% { transform: translateX(-50%) translateY(0); }
                50% { transform: translateX(-50%) translateY(-10px); }
                100% { transform: translateX(-50%) translateY(0); }
            }
        </style>
        
        <script>
            document.addEventListener('scroll', function() {
                const indicator = document.querySelector('.bw-scroll-indicator');
                const scrollPosition = window.scrollY;
                const viewportHeight = window.innerHeight;

                if (scrollPosition > viewportHeight) {
                    indicator.style.display = 'none'; // Hide indicator after 100vh
                } else {
                    indicator.style.display = 'block'; // Show indicator within first 100vh
                }
            });
        </script>

        <div class="bw-scroll-indicator">
            <a href="#welcome">
                <svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" width="40" height="40" x="0" y="0" viewBox="0 0 128 128" style="enable-background:new 0 0 512 512" xml:space="preserve" class="">
                    <g>
                        <path d="M64 88a3.988 3.988 0 0 1-2.828-1.172l-40-40c-1.563-1.563-1.563-4.094 0-5.656s4.094-1.563 5.656 0L64 78.344l37.172-37.172c1.563-1.563 4.094-1.563 5.656 0s1.563 4.094 0 5.656l-40 40A3.988 3.988 0 0 1 64 88z" fill="#ffffff" opacity="1" data-original="#000000" class=""></path>
                    </g>
                </svg>
            </a>
        </div>
        <?php
    }
}
add_action( 'wp_footer', 'bw_scroll_down_indicator' );

// Shortcode for top bar content
function bw_top_bar_content_shortcode() {
    return get_field('top_bar_content', 'option');
}
add_shortcode('bw_top_bar_content', 'bw_top_bar_content_shortcode');

// New combined shortcode for top bar button
function bw_top_bar_button_shortcode() {
    $button_text = get_field('top_bar_button_text', 'option');
    $button_link = get_field('top_bar_button_link', 'option');

    if (!$button_text || !$button_link) {
        return ''; // Return empty string if text or link is not set
    }

    // Construct the button HTML
    return '<a href="' . esc_url($button_link) . '" class="bw-top-bar-button">' . esc_html($button_text) . '</a>';
}
add_shortcode('bw_top_bar_button', 'bw_top_bar_button_shortcode');

// YITH Wishlisth plugin 
add_filter( 'yith_wcwl_wishlist_table_columns', 'customize_yith_wcwl_table_columns' );

function customize_yith_wcwl_table_columns( $columns ) {
    unset( $columns['price'] ); // Unregister unit price
    unset( $columns['stock_status'] ); // Unregister stock status
    return $columns;
}

// Add Promo Cookie
 function set_promo_code_cookie() {
    if (is_single() || is_page()) {
        global $post;
		echo "<script>console.log('Current Post ID: " . $post->ID . "');</script>";
        $promo_code = get_post_meta($post->ID, 'promo_code', true);
		echo "<script>console.log('Promo Code from Cookie: " .$promo_code . "');</script>";
        if (!empty($promo_code)) {
            echo "<script>document.cookie = 'promo_code=" . $promo_code . "; path=/; max-age=" . (86400 * 30) . "';</script>";
            echo "<script>console.log('Set Cookie: " . $promo_code . "');</script>";
        }
    }
}
add_action('template_redirect', 'set_promo_code_cookie');


?>