/**
 * Theme Customizer Controls (Panel Side)
 *
 * @package ChaletMontagne
 */

(function($) {
    'use strict';

    /**
     * Luxury Preset Change Detection
     * Automatically resets all custom colors when changing themes
     */
    wp.customize('chaletmontagne_luxury_preset', function(value) {
        var previousPreset = value.get();

        value.bind(function(newPreset) {
            // Don't reset if it's the initial load or same preset
            if (previousPreset === newPreset || !previousPreset) {
                previousPreset = newPreset;
                return;
            }

            // Define color mappings for each preset
            var presetColors = {
                'marron-sobre': {
                    primary: '#7B5B3A',
                    secondary: '#A38B6D',
                    accent: '#9C7A52',
                    text: '#4A3D30',
                    headings: '#3A2C1E',
                    background: '#FAF8F5',
                    content_bg: '#FFFFFF'
                },
                'esprit-bois': {
                    primary: '#B07D3A',
                    secondary: '#D4A95C',
                    accent: '#C4853F',
                    text: '#5A4430',
                    headings: '#4A3520',
                    background: '#F5EDE0',
                    content_bg: '#FBF6EE'
                },
                'noir-prestige': {
                    primary: '#C9A84C',
                    secondary: '#E8D5A0',
                    accent: '#B89530',
                    text: '#E8E0D0',
                    headings: '#E8E0D0',
                    background: '#0A0A0A',
                    content_bg: '#111110'
                },
                'ardoise-mineral': {
                    primary: '#5A7080',
                    secondary: '#8AA0B0',
                    accent: '#8C7B5E',
                    text: '#3A4A55',
                    headings: '#1E2D38',
                    background: '#F5F7F9',
                    content_bg: '#FFFFFF'
                },
                'foret-sauge': {
                    primary: '#5A7A5E',
                    secondary: '#8AA88C',
                    accent: '#A08B60',
                    text: '#354538',
                    headings: '#1E3022',
                    background: '#F6F8F5',
                    content_bg: '#FFFFFF'
                }
            };

            // Get the new preset colors
            var newColors = presetColors[newPreset] || presetColors['marron-sobre'];

            // Send AJAX request to save colors to database
            $.ajax({
                url: chaletMontagneCustomizer.ajaxurl,
                type: 'POST',
                data: {
                    action: 'chaletmontagne_reset_colors',
                    preset: newPreset,
                    nonce: chaletMontagneCustomizer.nonce
                },
                success: function(response) {
                    if (response.success) {
                        // Update all color controls in customizer
                        wp.customize('chaletmontagne_primary_color').set(newColors.primary);
                        wp.customize('chaletmontagne_secondary_color').set(newColors.secondary);
                        wp.customize('chaletmontagne_accent_color').set(newColors.accent);
                        wp.customize('chaletmontagne_text_color').set(newColors.text);
                        wp.customize('chaletmontagne_headings_color').set(newColors.headings);
                        wp.customize('chaletmontagne_background_color').set(newColors.background);
                        wp.customize('chaletmontagne_content_bg_color').set(newColors.content_bg);
                    }
                }
            });

            previousPreset = newPreset;
        });
    });

})(jQuery);
