using System;
using Patterns_And_Principles.Patterns.The_Observer_Pattern.WithEvent.WithEvent.Script._02_RealWorldUsage.Subject;
using TMPro;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;

namespace Patterns_And_Principles.Patterns.The_Observer_Pattern.WithEvent.WithEvent.Script._02_RealWorldUsage.Observer
{
    public class UIManager : MonoBehaviour
    {
        [SerializeField]
        private CoinManager coinManager;

        
        [SerializeField]
        private Button coinChangedBt;
        [SerializeField]
        private TextMeshProUGUI coinText;

        private void OnEnable()
        {
            CoinManager.CoinChanged += OnCoinChanged;
        }

        private void OnDisable()
        {
            CoinManager.CoinChanged -= OnCoinChanged;
        }

        private void Awake()
        {
            coinChangedBt.onClick.AddListener(OnCoinChangedButton);
            coinText.text = SaveManager.Instance.Coin.ToString();
        }

        private void OnCoinChangedButton()
        {
            coinManager.AddCoin(25);
        }
        private void OnCoinChanged(int amount)
        {
            int coin = SaveManager.Instance.Coin;
            coinText.text = coin.ToString();
        }
    }
}