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

public class CharacterLevelling
{

    public static Dictionary<int, MagicID> ms_magicLevels = new Dictionary<int, MagicID>()
    {
        // Milo starts off with Heal1
        { 3, MagicID.Fire1 },
        { 5, MagicID.Frost1 },
        { 7, MagicID.Heal2 },
        { 9, MagicID.Fire2 },
        { 12, MagicID.Frost2 },
        { 15, MagicID.Bolt1 },
        { 16, MagicID.Heal3 },
        { 19, MagicID.Fire3 },
        { 21, MagicID.Frost3 },
        { 22, MagicID.Bolt2 },
        { 25, MagicID.Heal4 },
        { 28, MagicID.Frost4 },
        { 30, MagicID.Bolt3 },
        { 32, MagicID.Fire4 },
        { 34, MagicID.Bolt4 },
    };

    public static void LevelUp( CharacterInfo info, out string levelUpText )
    {
        info.m_level++;

        while (info.m_experience >= CharacterInfo.EXP_FOR_LEVEL_GAIN)
        {
            info.m_experience -= CharacterInfo.EXP_FOR_LEVEL_GAIN;
        }

        MagicID newMagic;

        int level = info.m_level;

        int hpGain = Random.Range(1, 3 + (level / 5));
        int mpGain = Random.Range(1, 3 + (level / 8));
        int atkGain = Random.Range(1, 3 + (level / 5));
        int defGain = Random.Range(1, 3 + (level / 5));

        levelUpText = "";
        levelUpText += string.Format("{0} has levelled up!\n", info.m_name);
        levelUpText += string.Format("+{0}HP +{1}MP\n", hpGain, mpGain);
        levelUpText += string.Format("+{0}Attack +{1}Defense\n", atkGain, defGain);

        info.m_maxHp += hpGain;
        info.m_maxMp += mpGain;
        info.m_attack += atkGain;
        info.m_defense += defGain;

        if ( ms_magicLevels.TryGetValue( level, out newMagic ) )
        {
            CharacterMagic magic = CharacterMagic.GetMagic( newMagic );
            info.m_magic.Add(newMagic);
            levelUpText += string.Format("Learned new magic: {0}", magic.m_name);
        }
    }

}
