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

namespace Patterns_And_Principles.Patterns.The_Observer_Pattern.WithEvent.WithEvent.Script._02_RealWorldUsage.Observer
{
    public class SaveManager : MonoBehaviour
    {
        public static SaveManager Instance;
        public int Coin {
            get
            {
                return PlayerPrefs.GetInt("Coin", 0);
                ;
            }
            set
            {
                if (value < 0)
                {
                    Debug.Log("Coin cannot be negative");
                    return;
                }

                if (_coin == value) return;
                _coin = value;
                // CoinManager'dan daha mantıklı burda cağırmak bazı durumlarda.
                // CoinChanged?.Invoke(_coin);
                PlayerPrefs.SetInt("Coin", _coin);
                Debug.Log($"[SaveManager] Coin updated: {_coin}");
            } }
        private int _coin;
        private void OnEnable()
        {
            CoinManager.CoinChanged += OnCoinChanged;
        }

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

        private void Awake()
        {
            Instance = this;
        }
        
        private void OnCoinChanged(int amount)
        {
            Coin += amount;
        }
    }
}