/**
 * Game Data Manager
 * Central system for loading game mode-specific data
 */

class GameDataManager {
    constructor() {
        this.loadedData = {};
        this.currentMode = 'standard';
        
        // Define which data files map to which game modes
        this.modeDataMapping = {
            'standard': 'mainGameData',
            'timed': 'mainGameData', 
            'scored': 'mainGameData',
            'photography-studio': 'dressUpGameData',
            'training-academy': 'trainingGameData',
            'punishment-gauntlet': 'humiliationGameData',
            'endurance-trials': 'enduranceGameData'
        };
    }

    /**
     * Get data for a specific game mode
     */
    getDataForMode(mode) {
        const dataType = this.modeDataMapping[mode] || 'mainGameData';
        
        // Return the appropriate data based on mode
        switch(dataType) {
            case 'mainGameData':
                return window.mainGameData || this.getFallbackMainData();
            case 'humiliationGameData':
                return window.humiliationGameData || this.getFallbackHumiliationData();
            case 'trainingGameData':
                return window.trainingGameData || this.getFallbackTrainingData();
            case 'enduranceGameData':
                return window.enduranceGameData || this.getFallbackEnduranceData();
            case 'dressUpGameData':
                return window.dressUpGameData || this.getFallbackDressUpData();
            default:
                return this.getFallbackMainData();
        }
    }

    /**
     * Get tasks for a specific mode
     */
    getTasksForMode(mode) {
        const data = this.getDataForMode(mode);
        return data.mainTasks || [];
    }

    /**
     * Get scenarios for a specific mode
     */
    getScenariosForMode(mode) {
        const data = this.getDataForMode(mode);
        return data.scenarios || [];
    }

    /**
     * Get consequence tasks for a specific mode
     */
    getConsequenceTasksForMode(mode) {
        const data = this.getDataForMode(mode);
        return data.consequenceTasks || [];
    }

    /**
     * Get configuration for a specific mode
     */
    getConfigForMode(mode) {
        const data = this.getDataForMode(mode);
        return data.config || {};
    }

    /**
     * Find a specific task by ID across all loaded data
     */
    findTaskById(taskId, mode = null) {
        if (mode) {
            const data = this.getDataForMode(mode);
            const allTasks = [...(data.mainTasks || []), ...(data.scenarios || [])];
            return allTasks.find(task => task.id === taskId);
        }

        // Search across all data types if no mode specified
        const allDataTypes = ['mainGameData', 'humiliationGameData', 'trainingGameData', 'enduranceGameData', 'dressUpGameData'];
        
        for (const dataType of allDataTypes) {
            const data = window[dataType];
            if (data) {
                const allTasks = [...(data.mainTasks || []), ...(data.scenarios || [])];
                const found = allTasks.find(task => task.id === taskId);
                if (found) return found;
            }
        }

        return null;
    }

    /**
     * Get all tasks from all modes (for backwards compatibility)
     */
    getAllTasks() {
        const allTasks = [];
        const allDataTypes = ['mainGameData', 'humiliationGameData', 'trainingGameData', 'enduranceGameData', 'dressUpGameData'];
        
        for (const dataType of allDataTypes) {
            const data = window[dataType];
            if (data) {
                if (data.mainTasks) allTasks.push(...data.mainTasks);
                if (data.scenarios) allTasks.push(...data.scenarios);
            }
        }

        return allTasks;
    }

    // Fallback data methods (simplified versions for testing)
    getFallbackMainData() {
        return {
            mainTasks: [
                { id: 1, text: "Basic task", difficulty: "Easy" }
            ],
            consequenceTasks: [
                { id: 1, text: "Basic consequence", difficulty: "Medium" }
            ]
        };
    }

    getFallbackHumiliationData() {
        return {
            mainTasks: [
                {
                    id: 'mirror-humiliation-task',
                    text: "Webcam Mirror Self-Humiliation",
                    difficulty: "Medium",
                    interactiveType: "mirror-task",
                    story: "Look at yourself through your webcam and repeat degrading phrases.",
                    mirrorInstructions: "Look directly at yourself through the webcam while speaking the degrading phrases",
                    mirrorTaskText: "Repeat each phrase 5 times while maintaining eye contact with yourself: 'I am a pathetic toy', 'I exist for others' pleasure', 'I have no dignity'"
                }
            ],
            scenarios: []
        };
    }

    getFallbackTrainingData() {
        return { mainTasks: [], scenarios: [] };
    }

    getFallbackEnduranceData() {
        return { mainTasks: [], scenarios: [] };
    }

    getFallbackDressUpData() {
        return { mainTasks: [], scenarios: [] };
    }
}

// Create global instance
window.gameDataManager = new GameDataManager();

console.log('🎮 Game Data Manager initialized');