/**
 * Theme Customizer Live Preview
 *
 * @package ChaletMontagne
 */

(function($) {
    'use strict';

    /**
     * Couleurs
     */

    // Primary Color
    wp.customize('chaletmontagne_primary_color', function(value) {
        value.bind(function(newval) {
            document.documentElement.style.setProperty('--color-primary', newval);
        });
    });

    // Secondary Color
    wp.customize('chaletmontagne_secondary_color', function(value) {
        value.bind(function(newval) {
            document.documentElement.style.setProperty('--color-secondary', newval);
        });
    });

    // Accent Color
    wp.customize('chaletmontagne_accent_color', function(value) {
        value.bind(function(newval) {
            document.documentElement.style.setProperty('--color-accent', newval);
        });
    });

    // Text Color
    wp.customize('chaletmontagne_text_color', function(value) {
        value.bind(function(newval) {
            if (newval) {
                document.documentElement.style.setProperty('--color-text', newval);
                $('body').css('color', newval);
            } else {
                document.documentElement.style.removeProperty('--color-text');
                $('body').css('color', '');
            }
        });
    });

    // Headings Color
    wp.customize('chaletmontagne_headings_color', function(value) {
        value.bind(function(newval) {
            if (newval) {
                $('h1, h2, h3, h4, h5, h6').css('color', newval);
            } else {
                $('h1, h2, h3, h4, h5, h6').css('color', '');
            }
        });
    });

    // Background Color
    wp.customize('chaletmontagne_background_color', function(value) {
        value.bind(function(newval) {
            if (newval) {
                document.documentElement.style.setProperty('--color-bg-luxury', newval);
                $('body').css('background', newval);
            } else {
                document.documentElement.style.removeProperty('--color-bg-luxury');
                $('body').css('background', '');
            }
        });
    });

    // Content Background Color
    wp.customize('chaletmontagne_content_bg_color', function(value) {
        value.bind(function(newval) {
            var color = newval || '#FFFFFF';
            var selectors = '.site-content, .luxury-card, .luxury-form, .luxury-info-item, .entry-content, article, .luxury-card-contact, .site-header';
            $(selectors).css('background', color);
            // All widgets transparent
            $('.widget, .footer-widget').css('background', 'transparent');
        });
    });

    /**
     * Hero Section
     */

    // Hero Overlay Opacity
    wp.customize('chaletmontagne_hero_overlay_opacity', function(value) {
        value.bind(function(newval) {
            var opacity = newval / 100;
            $('.hero-section').css('--hero-overlay-opacity', opacity);
        });
    });

    // Hero Title
    wp.customize('chaletmontagne_hero_title', function(value) {
        value.bind(function(newval) {
            var esc = $('<div>').text(newval).html();
            $('.hero-section .hero-title, .hero-slider .hero-title, .page-hero .hero-title').html(esc.replace(/\r?\n/g, '<br>'));
        });
    });

    // Hero Title Size
    wp.customize('chaletmontagne_hero_title_size', function(value) {
        value.bind(function(newval) {
            var size = parseInt(newval, 10);
            if (size > 0) {
                document.documentElement.style.setProperty('--hero-title-size', 'min(' + size + 'px, 9vw)');
            } else {
                document.documentElement.style.setProperty('--hero-title-size', 'initial'); // 'initial' = valeur invalide garantie : le fallback de var() s'applique, y compris face a la variable emise par le serveur
            }
        });
    });

    // Hero Subtitle
    wp.customize('chaletmontagne_hero_subtitle', function(value) {
        value.bind(function(newval) {
            $('.hero-section .hero-subtitle, .page-hero .hero-subtitle').text(newval);
        });
    });

    // Hero CTA Text
    wp.customize('chaletmontagne_hero_cta_text', function(value) {
        value.bind(function(newval) {
            $('.hero-section .hero-cta, .page-hero .hero-cta').text(newval);
        });
    });

    /**
     * Contact Information
     */

    // Phone
    wp.customize('chaletmontagne_phone', function(value) {
        value.bind(function(newval) {
            $('.contact-phone, .header-phone').text(newval);
            $('.contact-phone a, .header-phone a').attr('href', 'tel:' + newval.replace(/\s/g, ''));
        });
    });

    // Email
    wp.customize('chaletmontagne_email', function(value) {
        value.bind(function(newval) {
            $('.contact-email, .header-email').text(newval);
            $('.contact-email a, .header-email a').attr('href', 'mailto:' + newval);
        });
    });

    // Address
    wp.customize('chaletmontagne_address', function(value) {
        value.bind(function(newval) {
            $('.contact-address, .footer-address').html(newval.replace(/\n/g, '<br>'));
        });
    });

    // Business Hours
    wp.customize('chaletmontagne_hours', function(value) {
        value.bind(function(newval) {
            $('.contact-hours, .footer-hours').text(newval);
        });
    });

    /**
     * Station Information
     */

    // Station Name
    wp.customize('chaletmontagne_station_name', function(value) {
        value.bind(function(newval) {
            $('.station-name').text(newval);
        });
    });

    // Distance to slopes
    wp.customize('chaletmontagne_distance_slopes', function(value) {
        value.bind(function(newval) {
            $('.distance-slopes').text(newval);
        });
    });

    // Distance to center
    wp.customize('chaletmontagne_distance_center', function(value) {
        value.bind(function(newval) {
            $('.distance-center').text(newval);
        });
    });

    // Activities
    wp.customize('chaletmontagne_activities', function(value) {
        value.bind(function(newval) {
            $('.station-activities').html(newval.replace(/\n/g, '<br>'));
        });
    });

    /**
     * Features / Arguments de vente
     */

    wp.customize('chaletmontagne_feature_1', function(value) {
        value.bind(function(newval) {
            $('.feature-1, .features-list li:nth-child(1)').text(newval);
        });
    });

    wp.customize('chaletmontagne_feature_2', function(value) {
        value.bind(function(newval) {
            $('.feature-2, .features-list li:nth-child(2)').text(newval);
        });
    });

    wp.customize('chaletmontagne_feature_3', function(value) {
        value.bind(function(newval) {
            $('.feature-3, .features-list li:nth-child(3)').text(newval);
        });
    });

    wp.customize('chaletmontagne_feature_4', function(value) {
        value.bind(function(newval) {
            $('.feature-4, .features-list li:nth-child(4)').text(newval);
        });
    });

    /**
     * Commercial Options
     */

    // Starting price
    wp.customize('chaletmontagne_starting_price', function(value) {
        value.bind(function(newval) {
            $('.starting-price').text(newval);
        });
    });

    // Direct booking badge
    wp.customize('chaletmontagne_direct_booking_badge', function(value) {
        value.bind(function(newval) {
            $('.direct-booking-badge').text(newval);
        });
    });

    // Cancellation policy
    wp.customize('chaletmontagne_cancellation_policy', function(value) {
        value.bind(function(newval) {
            $('.cancellation-policy').html(newval.replace(/\n/g, '<br>'));
        });
    });

    // Certification
    wp.customize('chaletmontagne_certification', function(value) {
        value.bind(function(newval) {
            $('.certification-badge').text(newval);
        });
    });


    /**
     * Site Identity (WordPress default)
     */

    // Site Title
    wp.customize('blogname', function(value) {
        value.bind(function(newval) {
            $('.site-title a').text(newval);
        });
    });

    // Site Description
    wp.customize('blogdescription', function(value) {
        value.bind(function(newval) {
            $('.site-description').text(newval);
        });
    });

    // Header Text Color
    wp.customize('header_textcolor', function(value) {
        value.bind(function(newval) {
            if ('blank' === newval) {
                $('.site-title, .site-description').css({
                    'clip': 'rect(1px, 1px, 1px, 1px)',
                    'position': 'absolute'
                });
            } else {
                $('.site-title, .site-description').css({
                    'clip': 'auto',
                    'position': 'relative',
                    'color': newval
                });
            }
        });
    });

})(jQuery);
