using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

class SkillsPointsManger : MonoBehaviour
{
    public const string PLAYER_SKILS_FREE_POINTS        = "Player.Skills.FreePoints";
    public const string PLAYER_SKILS_HEALTH_POINTS      = "Player.Skills.HealthPoints";
    public const string PLAYER_SKILS_STRENGHT_POINTS    = "Player.Skills.StenghtPoints";
    public const string PLAYER_SKILS_DEFENSE_POINTS     = "Player.Skills.DefensePoints";
    public const string PLAYER_SKILS_INTELIGENCE_POINTS = "Player.Skills.InteligencePoints";


    public static SkillsPointsManger Instance;

    [Header("Player Skills Points")]
    public int FreePoints        = 0;

    public int StrenghtPoints    = 0;
    public int DefensePoints     = 0;
    public int HealthPoints      = 0;
    public int InteligencePoints = 0;

    public void Awake()
    {
        if (Instance == null)
        {
            // Load saved gold value
            LoadValue();

            Instance = this;
        }
        else
        {
            Destroy(gameObject);
        }
    }

    /// <summary>
    /// Function to load values after loading scenes but after changing them by player script
    /// Thats because:
    /// - this manager fetch values globally on scene
    /// - Skills Panel gets info from this script
    /// - but.. Plater script modify vales themself and save them in PlayerPrefs instead of this Singleton class - messy...
    /// So we must fetch every one change from player.cs
    /// </summary>
    public void LoadValue()
    {
        if (PlayerPrefs.HasKey(PLAYER_SKILS_FREE_POINTS))
        {
            FreePoints = PlayerPrefs.GetInt(PLAYER_SKILS_FREE_POINTS);
        }


        if (PlayerPrefs.HasKey(PLAYER_SKILS_HEALTH_POINTS))
        {
            HealthPoints = PlayerPrefs.GetInt(PLAYER_SKILS_HEALTH_POINTS);
        }

        if (PlayerPrefs.HasKey(PLAYER_SKILS_STRENGHT_POINTS))
        {
            StrenghtPoints = PlayerPrefs.GetInt(PLAYER_SKILS_STRENGHT_POINTS);
        }

        if (PlayerPrefs.HasKey(PLAYER_SKILS_DEFENSE_POINTS))
        {
            DefensePoints = PlayerPrefs.GetInt(PLAYER_SKILS_DEFENSE_POINTS);
        }

        if (PlayerPrefs.HasKey(PLAYER_SKILS_INTELIGENCE_POINTS))
        {
            InteligencePoints = PlayerPrefs.GetInt(PLAYER_SKILS_INTELIGENCE_POINTS);
        }
    }

    /// <summary>
    /// Points are added after level up! :D
    /// see Player::ManageLevels
    /// </summary>
    public void AddFreePoints(int newPoints)
    {
        FreePoints += newPoints;

        PlayerPrefs.SetInt(PLAYER_SKILS_FREE_POINTS, FreePoints);
    }

    /// <summary>
    /// Function to update value displayed on Inventory Panel - only if is opened
    /// </summary>
    public void UpdatePanelView()
    {
        LoadValue();

        if (SkillsUIManager.Instance.GetPanelStatus())
        {
            //SkillsUIManager
            SkillsUIManager.Instance.DynamicPanel.GetComponent<SkillsPanelController>().RefreshPanelView(
                FreePoints,
                StrenghtPoints,
                DefensePoints,
                HealthPoints,
                InteligencePoints
            );
        }

    }

}