// Custom switcher with error handling
document.addEventListener("DOMContentLoaded", function() {
    try {
        // Safe querySelector helper
        function safeQuerySelector(selector) {
            try {
                return document.querySelector(selector);
            } catch (error) {
                console.log(`Error selecting ${selector}:`, error);
                return null;
            }
        }

        // Safe addEventListener helper
        function safeAddEventListener(element, eventType, handler) {
            if (element) {
                try {
                    element.addEventListener(eventType, handler);
                } catch (error) {
                    console.log(`Error adding event listener to ${element}:`, error);
                }
            }
        }

        // Theme switcher click handlers
        const switchers = {
            // Theme modes
            "#switcher-light-theme": function() {
                document.documentElement.setAttribute('data-theme-mode', 'light');
            },
            "#switcher-dark-theme": function() {
                document.documentElement.setAttribute('data-theme-mode', 'dark');
            },
            // Menu styles
            "#switcher-menu-light": function() {
                document.documentElement.setAttribute('data-menu-styles', 'light');
            },
            "#switcher-menu-dark": function() {
                document.documentElement.setAttribute('data-menu-styles', 'dark');
            },
            "#switcher-menu-primary": function() {
                document.documentElement.setAttribute('data-menu-styles', 'color');
            },
            "#switcher-menu-gradient": function() {
                document.documentElement.setAttribute('data-menu-styles', 'gradient');
            },
            "#switcher-menu-transparent": function() {
                document.documentElement.setAttribute('data-menu-styles', 'transparent');
            },
            // Header styles
            "#switcher-header-light": function() {
                document.documentElement.setAttribute('data-header-styles', 'light');
            },
            "#switcher-header-dark": function() {
                document.documentElement.setAttribute('data-header-styles', 'dark');
            },
            "#switcher-header-primary": function() {
                document.documentElement.setAttribute('data-header-styles', 'color');
            },
            "#switcher-header-gradient": function() {
                document.documentElement.setAttribute('data-header-styles', 'gradient');
            },
            "#switcher-header-transparent": function() {
                document.documentElement.setAttribute('data-header-styles', 'transparent');
            },
            // Layout width
            "#switcher-full-width": function() {
                document.documentElement.setAttribute('data-width', 'fullwidth');
            },
            "#switcher-boxed": function() {
                document.documentElement.setAttribute('data-width', 'boxed');
            },
            // Page styles
            "#switcher-regular": function() {
                document.documentElement.setAttribute('data-page-style', 'regular');
            },
            "#switcher-classic": function() {
                document.documentElement.setAttribute('data-page-style', 'classic');
            },
            "#switcher-modern": function() {
                document.documentElement.setAttribute('data-page-style', 'modern');
            },
            // Menu position
            "#switcher-menu-fixed": function() {
                document.documentElement.setAttribute('data-menu-position', 'fixed');
            },
            "#switcher-menu-scrollable": function() {
                document.documentElement.setAttribute('data-menu-position', 'scrollable');
            },
            // Header position
            "#switcher-header-fixed": function() {
                document.documentElement.setAttribute('data-header-position', 'fixed');
            },
            "#switcher-header-scrollable": function() {
                document.documentElement.setAttribute('data-header-position', 'scrollable');
            }
        };

        // Attach event listeners to all switchers
        for (let selector in switchers) {
            const element = safeQuerySelector(selector);
            safeAddEventListener(element, 'click', switchers[selector]);
        }

        // Handle theme reset
        const resetBtn = safeQuerySelector('#reset-all');
        safeAddEventListener(resetBtn, 'click', function() {
            // Reset all theme settings to default
            const html = document.documentElement;
            html.setAttribute('data-theme-mode', 'light');
            html.setAttribute('data-menu-styles', 'dark');
            html.setAttribute('data-header-styles', 'light');
            html.setAttribute('data-width', 'fullwidth');
            html.setAttribute('data-page-style', 'regular');
            html.setAttribute('data-menu-position', 'fixed');
            html.setAttribute('data-header-position', 'fixed');

            // Clear local storage
            localStorage.clear();

            // Reload page to apply all default settings
            window.location.reload();
        });

        // Initialize theme based on localStorage or defaults
        function initTheme() {
            const html = document.documentElement;

            // Theme mode
            if (localStorage.getItem('ynexdarktheme') === 'true') {
                html.setAttribute('data-theme-mode', 'dark');
                const darkModeSwitch = safeQuerySelector('#switcher-dark-theme');
                if (darkModeSwitch) darkModeSwitch.checked = true;
            } else {
                html.setAttribute('data-theme-mode', 'light');
                const lightModeSwitch = safeQuerySelector('#switcher-light-theme');
                if (lightModeSwitch) lightModeSwitch.checked = true;
            }

            // Menu styles
            if (localStorage.getItem('ynexMenu')) {
                const menuStyle = localStorage.getItem('ynexMenu');
                html.setAttribute('data-menu-styles', menuStyle);
                const menuSwitch = safeQuerySelector(`#switcher-menu-${menuStyle}`);
                if (menuSwitch) menuSwitch.checked = true;
            }

            // Header styles
            if (localStorage.getItem('ynexHeader')) {
                const headerStyle = localStorage.getItem('ynexHeader');
                html.setAttribute('data-header-styles', headerStyle);
                const headerSwitch = safeQuerySelector(`#switcher-header-${headerStyle}`);
                if (headerSwitch) headerSwitch.checked = true;
            }
        }

        // Run theme initialization
        initTheme();

    } catch (error) {
        console.log("Error in theme switcher initialization:", error);
    }
});
