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

public class SkillsPanelController : MonoBehaviour
{
    [Header("Current Value")]
    public int FreePoints = 0;

    [Header("Skills Section")]
    [SerializeField] private GameObject FreePointsLabel;

    [SerializeField] private GameObject HealthPointsLabel;
    [SerializeField] private GameObject StrenghtPointsLabel;
    [SerializeField] private GameObject DefensePointsLabel;
    [SerializeField] private GameObject InteligencePointsLabel;


    public void Start()
    {
        // Get current remembered value
        var skillsPointManager = SkillsPointsManger.Instance;

        if (skillsPointManager == null)
            throw new NullReferenceException("AccountBalanceManager not found!!!");

        // Fetch data from global scene manager responsible for player skill spoints
        RefreshPanelView(
            skillsPointManager.FreePoints,
            skillsPointManager.StrenghtPoints,
            skillsPointManager.DefensePoints,
            skillsPointManager.HealthPoints,
            skillsPointManager.InteligencePoints
        );
    }

    public void RefreshPanelView(
        int freePoints,
        int strenghtPoints,
        int defensePoints,
        int healthPoints,
        int inteligencePoints
    ) {

        FreePointsLabel.transform.Find("Layer").transform.Find("Value").GetComponent<TextMeshProUGUI>().text = $"{freePoints}";

        FreePoints = freePoints;

        BildSkillLabelContent(HealthPointsLabel, healthPoints);
        BildSkillLabelContent(StrenghtPointsLabel, strenghtPoints);
        BildSkillLabelContent(DefensePointsLabel, defensePoints);
        BildSkillLabelContent(InteligencePointsLabel, inteligencePoints);
    }

    public void BildSkillLabelContent(GameObject skillPanelLabel, int value)
    {
        skillPanelLabel.transform.Find("Layer").transform.Find("Value").GetComponent<TextMeshProUGUI>().text = $"{value}";

        if (FreePoints > 0)
            skillPanelLabel.transform.Find("Button").gameObject.active = true;
        else
            skillPanelLabel.transform.Find("Button").gameObject.active = false;
    }

    #region spend skills points actions
    public void IncreaseHealth()
    {
        FreePoints -= 1;
        PlayerPrefs.SetInt(SkillsPointsManger.PLAYER_SKILS_FREE_POINTS, FreePoints);

        GameObject.FindGameObjectWithTag("Player").GetComponent<Player>().AddHealthPoint();
    }

    public void IncreaseStrenght()
    {
        FreePoints -= 1;
        PlayerPrefs.SetInt(SkillsPointsManger.PLAYER_SKILS_FREE_POINTS, FreePoints);

        GameObject.FindGameObjectWithTag("Player").GetComponent<Player>().AddStrengthPoint();
    }

    public void IncreaseDefence()
    {
        FreePoints -= 1;
        PlayerPrefs.SetInt(SkillsPointsManger.PLAYER_SKILS_FREE_POINTS, FreePoints);

        GameObject.FindGameObjectWithTag("Player").GetComponent<Player>().AddDefensePoint();
    }

    public void IncreaseInteligence()
    {
        FreePoints -= 1;
        PlayerPrefs.SetInt(SkillsPointsManger.PLAYER_SKILS_FREE_POINTS, FreePoints);

        GameObject.FindGameObjectWithTag("Player").GetComponent<Player>().AddIntelligencePoint();
    }
    #endregion
}