module observer.delivery.weatherdata;

import observer.delivery.subject;
import observer.delivery.observer;
import std.algorithm : remove, countUntil;

class WeatherData : Subject
{
private:
    Observer[] observers;
    float temperature, humidity, pressure;
public:
    override void registerObserver(Observer o)
    {
        observers ~= o;
    }
    
    override void removeObserver(Observer o)
    {
        // Вызовет ошибку в случае отсутствия элемента в массиве после его поиска
        // observers = observers.remove(observers.countUntil(o));
        observers = remove!(current => current == o)(observers);
    }

    override void notifyObservers()
    {
        foreach (Observer o; observers)
        {
            o.update(temperature, humidity, pressure);
        }
    }

    void measurementsChanged()
    {
        notifyObservers();
    }

    void setMeasurements(float temperature, float humidity, float pressure)
    {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }

    @property float getTemperature()
    {
        return temperature;
    }

    @property float getHumidity()
    {
        return humidity;
    }

    @property float getPressure()
    {
        return pressure;
    }
}
