using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tutorial_5
{
    public partial class WeatherData : Form, ISubject
    {
        List<IWeatherObserver> observers;
        public void DeregisterObserver(IWeatherObserver observer)
        {
            if (observers.Contains(observer)) observers.Remove(observer);
            else Console.Error.WriteLine("{0} does not exist in the observers list.", observer.ToString());
        }

        /// <summary>
        /// 'Push' method
        /// </summary>
        public void NotifyObservers()
        {
            double t = GetTemperature();
            double h = GetHumidity();
            double p = GetPressure();

            foreach (var observer in observers)
            {
                observer.Update(t, h, p);
            }
        }

        public void RegisterObserver(IWeatherObserver observer)
        {
            if (observers.Contains(observer)) Console.Error.WriteLine("{0} already exists in the observers list.", observer.ToString());
            else observers.Add(observer);
        }

        public double GetTemperature()
        {
            return TemperatureSlider.Value;
        }

        public double GetHumidity()
        {
            return HumiditySlider.Value;
        }

        public double GetPressure()
        {
            return PressureSlider.Value;
        }

        void MeasurementChanged()
        {
            NotifyObservers();
        }

        public WeatherData()
        {
            InitializeComponent();
            observers = new List<IWeatherObserver>();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

        /// <summary>
        /// Analagous to the Update() method in Excercise 1
        /// Pushes data to observers via Subject.NotifyObservers
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SliderChanged(object sender, EventArgs e)
        {
            NotifyObservers();
            TemperatureLabel.Text = TemperatureSlider.Value.ToString() + " ℃";
            HumidityLabel.Text = HumiditySlider.Value.ToString() + " %";
            PressureLabel.Text = PressureSlider.Value.ToString() + " kPa";
        }

        private void Form1_Load_1(object sender, EventArgs e)
        {
            CurrentConditionsDisplay ccd = new CurrentConditionsDisplay(this);
            ccd.Show();
        }
    }
}
