/**
 * Galaxy Strike Online - XP Progression Configuration
 * Math settings from https://math.korvarix.com/
 * 
 * Decoded configuration parameters:
 * - Current Level: 1
 * - Max Level: 50
 * - Formula parameters with multiple progression curves enabled
 */

const XP_CONFIG = {
    // Basic settings
    currentLevel: 1,
    maxLevel: 50,
    
    // Formula parameters (decoded from the website)
    formula: {
        // Linear progression parameters
        linearBase: 500,        // lb: 500
        linearExponent: 2.2,    // le: 2.2
        
        // Base curve parameters  
        curveBase: 12.5,        // kb: 12.5
        curveExponent: 1.4,     // ke: 1.4
        
        // Multiplier parameters
        multiplier: 0.11667,    // mqm: 0.11667
        squareMultiplier: 0.05, // sqm: 0.05
        divisionMultiplier: 0.125, // dm: 0.125
        rootMultiplier: 0.25,   // rm: 0.25
        constant: 0.5,          // kt: 0.5
        
        // Threshold parameters
        multiplierThreshold: 15, // mqt: 15
        squareThreshold: 10,    // sqt: 10
        divisionThreshold: 45   // dt: 45
    },
    
    // Enabled formula types
    enabled: {
        linear: true,      // l: true
        curve: true,       // k: true  
        multiplier: true,  // mq: true
        square: true,      // sq: true
        division: true,    // d: true
        root: true         // r: true
    },
    
    // Activity XP rewards (based on website estimates)
    activities: {
        combat: {
            tutorial: 10,
            easy: 25,
            normal: 50,
            hard: 100,
            extreme: 200,
            legendary: 500,
            mythic: 1000
        },
        quests: {
            main: 200,
            side: 50,
            daily_easy: 25,
            daily_medium: 75,
            daily_hard: 150,
            daily_extreme: 300,
            weekly_basic: 200,
            weekly_medium: 500,
            weekly_hard: 800,
            weekly_extreme: 1200,
            weekly_legendary: 2000,
            weekly_mythic: 3000
        },
        dungeons: {
            tutorial: 50,
            easy: 100,
            medium: 250,
            hard: 500,
            extreme: 1000,
            legendary: 2000,
            mythic: 5000
        },
        raids: {
            weekly: 1000,
            monthly: 5000
        }
    }
};

/**
 * Calculate XP required for a specific level
 * Uses the complex formula from the website configuration
 * @param {number} level - The level to calculate XP for
 * @returns {number} XP required for this level
 */
function calculateXPForLevel(level) {
    if (level <= 1) return 0;
    if (level > XP_CONFIG.maxLevel) return Infinity;
    
    const f = XP_CONFIG.formula;
    const enabled = XP_CONFIG.enabled;
    let xp = 0;
    
    // Base linear progression: lb * level^le
    if (enabled.linear) {
        xp += f.linearBase * Math.pow(level, f.linearExponent);
    }
    
    // Curve progression: kb * level^ke
    if (enabled.curve) {
        xp += f.curveBase * Math.pow(level, f.curveExponent);
    }
    
    // Multiplier progression (after threshold)
    if (enabled.multiplier && level > f.multiplierThreshold) {
        const multiplierBonus = (level - f.multiplierThreshold) * f.multiplier;
        xp *= (1 + multiplierBonus);
    }
    
    // Square progression (after threshold)
    if (enabled.square && level > f.squareThreshold) {
        const squareBonus = Math.pow(level - f.squareThreshold, 2) * f.squareMultiplier;
        xp += squareBonus;
    }
    
    // Division progression (after threshold)
    if (enabled.division && level > f.divisionThreshold) {
        const divisionBonus = (level - f.divisionThreshold) * f.divisionMultiplier;
        xp *= (1 + divisionBonus);
    }
    
    // Root progression
    if (enabled.root) {
        const rootBonus = Math.sqrt(level) * f.rootMultiplier;
        xp += rootBonus;
    }
    
    // Add constant
    xp += f.constant;
    
    return Math.floor(xp);
}

/**
 * Calculate total XP needed to reach from level 1 to target level
 * @param {number} targetLevel - The target level
 * @returns {number} Total XP needed
 */
function calculateTotalXPToLevel(targetLevel) {
    let totalXP = 0;
    for (let level = 2; level <= targetLevel; level++) {
        totalXP += calculateXPForLevel(level);
    }
    return totalXP;
}

/**
 * Calculate XP remaining until next level up
 * @param {number} currentLevel - Current player level
 * @param {number} currentXP - Current player XP
 * @returns {number} XP needed for next level
 */
function calculateXPToNextLevel(currentLevel, currentXP) {
    if (currentLevel >= XP_CONFIG.maxLevel) return 0;
    
    const xpForNextLevel = calculateXPForLevel(currentLevel + 1);
    const totalXPToCurrentLevel = calculateTotalXPToLevel(currentLevel);
    
    return Math.max(0, xpForNextLevel - currentXP);
}

/**
 * Get level from total XP
 * @param {number} totalXP - Total accumulated XP
 * @returns {object} Level info { level, xpIntoLevel, xpToNext, progress }
 */
function getLevelFromXP(totalXP) {
    let level = 1;
    let cumulativeXP = 0;
    
    // Find current level
    while (level < XP_CONFIG.maxLevel) {
        const xpForNextLevel = calculateXPForLevel(level + 1);
        if (cumulativeXP + xpForNextLevel > totalXP) {
            break;
        }
        cumulativeXP += xpForNextLevel;
        level++;
    }
    
    const xpIntoLevel = totalXP - cumulativeXP;
    const xpToNext = calculateXPToNextLevel(level, totalXP);
    const progress = xpToNext > 0 ? xpIntoLevel / (xpIntoLevel + xpToNext) : 1;
    
    return {
        level,
        xpIntoLevel,
        xpToNext,
        progress: Math.min(1, Math.max(0, progress))
    };
}

/**
 * Generate XP progression table for all levels
 * @returns {Array} Array of level data
 */
function generateProgressionTable() {
    const table = [];
    let cumulativeXP = 0;
    
    for (let level = 1; level <= XP_CONFIG.maxLevel; level++) {
        const xpForThisLevel = level === 1 ? 0 : calculateXPForLevel(level);
        const xpToNext = level < XP_CONFIG.maxLevel ? calculateXPForLevel(level + 1) : 0;
        
        table.push({
            level,
            xpForThisLevel,
            xpToNext,
            cumulativeXP,
            totalXPToLevel: cumulativeXP
        });
        
        cumulativeXP += xpForThisLevel;
    }
    
    return table;
}

// Export for use in both client and server
if (typeof module !== 'undefined' && module.exports) {
    // Node.js (server)
    module.exports = {
        XP_CONFIG,
        calculateXPForLevel,
        calculateTotalXPToLevel,
        calculateXPToNextLevel,
        getLevelFromXP,
        generateProgressionTable
    };
} else {
    // Browser (client)
    window.XPProgression = {
        XP_CONFIG,
        calculateXPForLevel,
        calculateTotalXPToLevel,
        calculateXPToNextLevel,
        getLevelFromXP,
        generateProgressionTable
    };
}
