/**
 * AniWorld - Theme Module
 *
 * Dark/light mode management and persistence.
 *
 * Dependencies: constants.js
 */

var AniWorld = window.AniWorld || {};

AniWorld.Theme = (function() {
    'use strict';

    const STORAGE = AniWorld.Constants.STORAGE_KEYS;
    const DEFAULTS = AniWorld.Constants.DEFAULTS;

    /**
     * Initialize theme from saved preference
     */
    function init() {
        const savedTheme = localStorage.getItem(STORAGE.THEME) || DEFAULTS.THEME;
        setTheme(savedTheme);
    }

    /**
     * Set the application theme
     * @param {string} theme - 'light' or 'dark'
     */
    function setTheme(theme) {
        document.documentElement.setAttribute('data-theme', theme);
        localStorage.setItem(STORAGE.THEME, theme);

        // Update theme toggle icon if it exists
        const themeIcon = document.querySelector('#theme-toggle i');
        if (themeIcon) {
            themeIcon.className = theme === 'light' ? 'fas fa-moon' : 'fas fa-sun';
        }
    }

    /**
     * Toggle between light and dark themes
     */
    function toggle() {
        const currentTheme = document.documentElement.getAttribute('data-theme') || DEFAULTS.THEME;
        const newTheme = currentTheme === 'light' ? 'dark' : 'light';
        setTheme(newTheme);
    }

    /**
     * Get the current theme
     * @returns {string} 'light' or 'dark'
     */
    function getCurrentTheme() {
        return document.documentElement.getAttribute('data-theme') || DEFAULTS.THEME;
    }

    /**
     * Check if dark mode is active
     * @returns {boolean}
     */
    function isDarkMode() {
        return getCurrentTheme() === 'dark';
    }

    // Public API
    return {
        init: init,
        setTheme: setTheme,
        toggle: toggle,
        getCurrentTheme: getCurrentTheme,
        isDarkMode: isDarkMode
    };
})();
