using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TemperatureManager.MVC
{
    public class SeuilTemperatureModele : IObservable<SeuilTemperature>
    {

        private List<IObserver<SeuilTemperature>> observers;
        private SeuilTemperature seuil;
        public EventHandler<EventArgs> CaptureHandler;

        public SeuilTemperatureModele(SeuilTemperature initialValue)
        {
            seuil = initialValue;
            observers=new List<IObserver<SeuilTemperature>>();
        }

        public void Incremente()
        {
            seuil.ValueTemp++;
            Notify(seuil);
        }

        public void Decremente()
        {
            seuil.ValueTemp--;
            Notify(seuil);
        }

        private void Notify(SeuilTemperature temp)
        {
            foreach (var observer in this.observers)
            {
                observer.OnNext(seuil);       
            }
        }



        public IDisposable Subscribe(IObserver<SeuilTemperature> observer)
        {
            if (!observers.Contains(observer))
                observers.Add(observer);
            return new Unsubscriber(observers, observer);
        }

        private class Unsubscriber : IDisposable
        {
            private List<IObserver<SeuilTemperature>> _observers;
            private IObserver<SeuilTemperature> _observer;

            public Unsubscriber(List<IObserver<SeuilTemperature>> observers, IObserver<SeuilTemperature> observer)
            {
                this._observers = observers;
                this._observer = observer;
            }

            public void Dispose()
            {
                if (_observer != null && _observers.Contains(_observer))
                    _observers.Remove(_observer);
            }
        }




    }
}
      

