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

class AccountBalanceManager : MonoBehaviour
{
    public const string PLAYER_ACCOUNT_VALUE = "Player.AccountBalance";

    public static AccountBalanceManager Instance;

    public int Gold;

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

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

    public void SetAccountBalanceValue(int gold)
    {
        Gold = gold;

        UpdatePanelValue();
    }

    public void IncreaseAccountBalanceValue(int gold)
    {
        Gold += gold;

        UpdatePanelValue();
    }

    public void DecreaseAccountBalanceValue(int gold)
    {
        Gold -= gold;

        UpdatePanelValue();
    }

    public void SaveValue()
    {
        PlayerPrefs.SetInt(PLAYER_ACCOUNT_VALUE, Gold);
    }

    public void LoadValue()
    {
        if(PlayerPrefs.HasKey(PLAYER_ACCOUNT_VALUE))
        {
            Gold = PlayerPrefs.GetInt(PLAYER_ACCOUNT_VALUE);
        }
        else
        {
            Gold = 0;
        }
    }

    /// <summary>
    /// Function to update value displayed on Inventory Panel - only if is opened
    /// </summary>
    private void UpdatePanelValue()
    {
        if (InventoryUIManager.Instance.GetPanelStatus())
            InventoryUIManager.Instance.DynamicPanel.GetComponent<PanelCashController>().RefreshPanel(Gold);
    }
}