using System.Collections.Generic;
using Patterns_And_Principles.Patterns.The_Observer_Pattern.WithoutEvent.Script._01_BasicExample.Interface;
using UnityEngine;

namespace Patterns_And_Principles.Patterns.The_Observer_Pattern.WithoutEvent.Script._01_BasicExample
{
    public class Subject : MonoBehaviour, ISubject
    {
        private readonly List<IObserver> _observers = new List<IObserver>();

        public void Attach(IObserver observer)
        {
            _observers.Add(observer);
        }

        public void Detach(IObserver observer)
        {
            _observers.Remove(observer);
        }

        public void NotifyObservers()
        {
            foreach (var observer in _observers)
            {
                observer.Notify(this);
            }
        }
        private void Update()
        {
            // Press Space to trigger notification
            if (Input.GetKeyDown(KeyCode.Space))
            {
                Debug.Log("Subject triggered!");
                NotifyObservers();
            }
        }
    }
}