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

public class Level
{
    public int currentLevel;

    public UnityEvent OnLevelUpEvent = new UnityEvent();
    public UnityEvent OnExperienceChanged = new UnityEvent();

    float currentExperience;
    public float GetCurrentExperience()
    {
        return currentExperience;
    }

    public float baseExperienceThreshold = 500f;
    public float ExperienceThresholdGrowth = 75f;
    public float exponent = 1.5f;
    public float FinalExperienceThreshold;

    public Level()
    {
        currentLevel = 1;
        currentExperience = 0f;
        //baseExperienceThreshold = 500f;
        OnLevelUpEvent.AddListener(UpdateExperienceThreshold);
        UpdateExperienceThreshold();
    }
    public Level(int currentLevel, float currentExperience)
    {
        this.currentLevel = currentLevel;
        this.currentExperience = currentExperience;

        OnLevelUpEvent.AddListener(UpdateExperienceThreshold);
        UpdateExperienceThreshold();
    }

    public void GainExperience(float xpIncome)
    {
        currentExperience += xpIncome;
        if (currentExperience >= FinalExperienceThreshold)
        {
            currentExperience -= FinalExperienceThreshold;
            currentLevel++;
            OnLevelUpEvent.Invoke();
            Debug.Log("LevelUp Event Trigger");
        }
        Debug.Log(xpIncome + " exp received");
        Debug.Log(currentExperience + " / " + FinalExperienceThreshold);
        if (currentExperience >= FinalExperienceThreshold)
        {
            GainExperience(0);
        }
        OnExperienceChanged.Invoke();
    }

    public void UpdateExperienceThreshold()
    {
        FinalExperienceThreshold = Mathf.RoundToInt(baseExperienceThreshold + ExperienceThresholdGrowth * Mathf.Pow((float)currentLevel, exponent));
    }

}
