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 AudioManager : MonoBehaviour
    {
        [SerializeField]
        private AudioSource _audioSource;

        [SerializeField]
        private AudioClip coinSfx;
        
        private void OnEnable()
        {
            CoinManager.CoinChanged += OnCoinChanged;
        }

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

        private void PlaySfx(AudioClip sfx)
        {
            _audioSource.PlayOneShot(sfx);
        }

        private void OnCoinChanged(int amount)
        {
            PlaySfx(coinSfx);
        }
    }
}