using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LevelSystem : MonoBehaviour
{
    private int currentLevel;
    private int currentXP;

    // The level data in a 2D array
    private int[,] levelData = new int[,] {
        { 100, 0 }, { 2650, 2550 }, { 3900, 1250 }, { 4750, 850 }, { 5600, 850 },
        { 6350, 750 }, { 7100, 750 }, { 7850, 750 }, { 8150, 300 }, { 8600, 450 },
        { 9050, 450 }, { 9500, 450 }, { 9950, 450 }, { 10400, 450 }, { 10850, 450 },
        { 11300, 450 }, { 11750, 450 }, { 12200, 450 }, { 12350, 150 }, { 12500, 150 },
        { 12650, 150 }, { 12800, 150 }, { 12950, 150 }, { 13100, 150 }, { 13250, 150 },
        { 13400, 150 }, { 13550, 150 }, { 13700, 150 }, { 13850, 150 }, { 14000, 150 },
        { 14150, 150 }, { 14300, 150 }, { 14450, 150 }, { 14600, 150 }, { 14750, 150 },
        { 14900, 150 }, { 15050, 150 }, { 15200, 150 }, { 15350, 150 }, { 15500, 150 },
        { 15650, 150 }, { 15800, 150 }, { 15950, 150 }, { 16100, 150 }, { 16250, 150 },
        { 16400, 150 }, { 16550, 150 }, { 16700, 150 }, { 16850, 150 }, { 17000, 150 },
        { 17150, 150 }, { 17300, 150 }, { 17450, 150 }, { 17600, 150 }, { 17750, 150 },
        { 17900, 150 }, { 18000, 150 }, { 18000, 0 }
    };

    void Start()
    {
        LoadProgress();
    }


    public void AddXP(int xp)
    {
        // Add the given XP to the current XP
        currentXP += xp;

        // Check if the current XP is enough to level up
        while (currentLevel < levelData.GetLength(0) && currentXP >= levelData[currentLevel, 0])
        {
            currentXP -= levelData[currentLevel, 0];
            currentLevel++;
        }

        // Save the progress
        SaveProgress();
    }


    public int GetCurrentLevel()
    {
        return currentLevel;
    }

    public int GetCurrentXP()
    {
        return currentXP;
    }

    public int GetXPToNextLevel()
    {
        if (currentLevel >= levelData.GetLength(0))
        {
            // If the player has reached the maximum level, return 0 XP to next level
            return 0;
        }
        else
        {
            // Otherwise, return the XP needed to reach the next level
            return levelData[currentLevel, 0] - currentXP;
        }
    }

    public int GetTotalXPNeededForLevel(int level)
    {
        if (level < 1 || level >= levelData.GetLength(0))
        {
            // If the input level is invalid, return -1
            return -1;
        }
        else
        {
            // Otherwise, return the total XP needed for the input level
            int totalXP = 0;
            for (int i = 1; i <= level; i++)
            {
                totalXP += levelData[i, 0];
            }
            return totalXP;
        }
    }

    public void SaveProgress()
    {
        SaveManager.Instance.UpdatePlayerStats(currentXP, currentLevel);
    }

    public void LoadProgress()
    {
        GameData data = SaveManager.Instance.LoadGame();
        currentLevel = data.currentLevel;
        currentXP = data.currentXP;
    }

}