using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic; // uvesti

public class MesssagingManager : MonoBehaviour {

    //postavljamo singlton instancu
    public static MesssagingManager Instance { get; private set; }
    //postavljamo listu delegata pretplacenih
    private List<Action> subscribers = new List<Action>();

   
    void Awake()
    {
        Debug.Log("Pokrenut menadzer poruka");
        //Proveramo da li postoji vec instanca koja je razlicita od trenutne
        if (Instance != null && Instance != this)
        {
            //unistava instancu
            Destroy(gameObject);
        }
        //Cuvamo trenutnu instancu
        Instance = this;

        //Osiguravamo da trenutna instanca ne bude unistena prilikom promene scena
        DontDestroyOnLoad(gameObject);
    }
    // dodaje prosledjeni delegat menadžeru
    public void Subscribe(Action subscriber)
    {
        Debug.Log("Subscriber registered");
        subscribers.Add(subscriber);
    }

  
  // skida prosledjenog pretplatnika
    public void UnSubscribe(Action subscriber)
    {
       
        subscribers.Remove(subscriber);
    }
    // Skida sve pretplatnike
    public void ClearAllSubscribers()
    {
        subscribers.Clear();
    }
    // obavesteva pretplatnike da se nešto desilo
    public void Broadcast()
    {
        Debug.Log("Broj pretplatnika je = " + subscribers.Count);
        foreach (var subscriber in subscribers)
        {
            subscriber();
        }
    }
   
	

}
