/**
 * AniWorld - Advanced Config Module
 *
 * Handles advanced configuration settings like concurrent downloads,
 * timeouts, and debug mode.
 *
 * Dependencies: constants.js, api-client.js, ui-utils.js
 */

var AniWorld = window.AniWorld || {};

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

    const API = AniWorld.Constants.API;

    /**
     * Load advanced configuration
     */
    async function load() {
        try {
            const response = await AniWorld.ApiClient.get(API.CONFIG_SECTION + '/advanced');
            if (!response) return;

            const data = await response.json();

            if (data.success) {
                const config = data.config;
                document.getElementById('max-concurrent-downloads').value = config.max_concurrent_downloads || 3;
                document.getElementById('provider-timeout').value = config.provider_timeout || 30;
                document.getElementById('enable-debug-mode').checked = config.enable_debug_mode === true;
            }
        } catch (error) {
            console.error('Error loading advanced config:', error);
        }
    }

    /**
     * Save advanced configuration
     */
    async function save() {
        try {
            const config = {
                max_concurrent_downloads: parseInt(document.getElementById('max-concurrent-downloads').value),
                provider_timeout: parseInt(document.getElementById('provider-timeout').value),
                enable_debug_mode: document.getElementById('enable-debug-mode').checked
            };

            const response = await AniWorld.ApiClient.request(API.CONFIG_SECTION + '/advanced', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(config)
            });

            if (!response) return;
            const data = await response.json();

            if (data.success) {
                AniWorld.UI.showToast('Advanced configuration saved successfully', 'success');
            } else {
                AniWorld.UI.showToast('Failed to save config: ' + data.error, 'error');
            }
        } catch (error) {
            console.error('Error saving advanced config:', error);
            AniWorld.UI.showToast('Failed to save advanced configuration', 'error');
        }
    }

    // Public API
    return {
        load: load,
        save: save
    };
})();
