/**
 * AniWorld - NFO Config Module
 *
 * Handles NFO metadata configuration settings.
 *
 * Dependencies: constants.js, api-client.js, ui-utils.js
 */

var AniWorld = window.AniWorld || {};

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

    const API = AniWorld.Constants.API;

    /**
     * Load NFO configuration from server
     */
    async function load() {
        try {
            const response = await AniWorld.ApiClient.request(API.CONFIG);
            
            if (!response) {
                throw new Error('Failed to load configuration');
            }
            
            const config = await response.json();
            
            if (config && config.nfo) {
                const nfo = config.nfo;
                
                // TMDB API Key
                const tmdbKey = document.getElementById('tmdb-api-key');
                if (tmdbKey && nfo.tmdb_api_key) {
                    tmdbKey.value = nfo.tmdb_api_key;
                }
                
                // Auto-create NFO
                const autoCreate = document.getElementById('nfo-auto-create');
                if (autoCreate) {
                    autoCreate.checked = nfo.auto_create || false;
                }
                
                // Update on scan
                const updateOnScan = document.getElementById('nfo-update-on-scan');
                if (updateOnScan) {
                    updateOnScan.checked = nfo.update_on_scan || false;
                }
                
                // Download options
                const downloadPoster = document.getElementById('nfo-download-poster');
                if (downloadPoster) {
                    downloadPoster.checked = nfo.download_poster !== false;
                }
                
                const downloadLogo = document.getElementById('nfo-download-logo');
                if (downloadLogo) {
                    downloadLogo.checked = nfo.download_logo !== false;
                }
                
                const downloadFanart = document.getElementById('nfo-download-fanart');
                if (downloadFanart) {
                    downloadFanart.checked = nfo.download_fanart !== false;
                }
                
                // Image size
                const imageSize = document.getElementById('nfo-image-size');
                if (imageSize && nfo.image_size) {
                    imageSize.value = nfo.image_size;
                }
            }
        } catch (error) {
            console.error('Error loading NFO config:', error);
            AniWorld.UI.showToast('Failed to load NFO configuration', 'error');
        }
    }

    /**
     * Save NFO configuration
     */
    async function save() {
        try {
            AniWorld.UI.showLoading('Saving NFO configuration...');
            
            // Get form values
            const tmdbKey = document.getElementById('tmdb-api-key');
            const autoCreate = document.getElementById('nfo-auto-create');
            const updateOnScan = document.getElementById('nfo-update-on-scan');
            const downloadPoster = document.getElementById('nfo-download-poster');
            const downloadLogo = document.getElementById('nfo-download-logo');
            const downloadFanart = document.getElementById('nfo-download-fanart');
            const imageSize = document.getElementById('nfo-image-size');
            
            // Validate TMDB API key is provided if auto-create is enabled
            if (autoCreate && autoCreate.checked && (!tmdbKey || !tmdbKey.value.trim())) {
                AniWorld.UI.hideLoading();
                AniWorld.UI.showToast('TMDB API key is required when auto-create is enabled', 'error');
                return;
            }
            
            // Get current config
            const configResponse = await AniWorld.ApiClient.get(AniWorld.Constants.API.CONFIG);
            if (!configResponse) {
                throw new Error('Failed to load current configuration');
            }
            const config = await configResponse.json();
            
            // Update NFO settings
            config.nfo = {
                tmdb_api_key: tmdbKey ? tmdbKey.value.trim() || null : null,
                auto_create: autoCreate ? autoCreate.checked : false,
                update_on_scan: updateOnScan ? updateOnScan.checked : false,
                download_poster: downloadPoster ? downloadPoster.checked : true,
                download_logo: downloadLogo ? downloadLogo.checked : true,
                download_fanart: downloadFanart ? downloadFanart.checked : true,
                image_size: imageSize ? imageSize.value : 'original'
            };
            
            // Save configuration
            const response = await AniWorld.ApiClient.put(AniWorld.Constants.API.CONFIG, config);
            
            if (response) {
                AniWorld.UI.showToast('NFO configuration saved successfully', 'success');
            } else {
                throw new Error('Failed to save configuration');
            }
        } catch (error) {
            console.error('Error saving NFO config:', error);
            AniWorld.UI.showToast('Failed to save NFO configuration: ' + error.message, 'error');
        } finally {
            AniWorld.UI.hideLoading();
        }
    }

    /**
     * Test TMDB API connection
     */
    async function testTMDBConnection() {
        try {
            const tmdbKey = document.getElementById('tmdb-api-key');
            
            if (!tmdbKey || !tmdbKey.value.trim()) {
                AniWorld.UI.showToast('Please enter a TMDB API key first', 'warning');
                return;
            }
            
            AniWorld.UI.showLoading('Testing TMDB connection...');
            
            const response = await AniWorld.ApiClient.request(
                '/api/config/tmdb/validate',
                {
                    method: 'POST',
                    body: JSON.stringify({ api_key: tmdbKey.value.trim() })
                }
            );
            
            if (!response) {
                throw new Error('No response from server');
            }
            
            const data = await response.json();
            
            if (data && data.valid) {
                AniWorld.UI.showToast('TMDB API key is valid!', 'success');
            } else {
                const message = data && data.message ? data.message : 'Invalid API key';
                AniWorld.UI.showToast('TMDB validation failed: ' + message, 'error');
            }
        } catch (error) {
            console.error('Error testing TMDB connection:', error);
            AniWorld.UI.showToast('Failed to test TMDB connection: ' + error.message, 'error');
        } finally {
            AniWorld.UI.hideLoading();
        }
    }

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