using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WiPoMServer;
using WebServer;
using System.Diagnostics;
using System.ComponentModel;

namespace Wireless_Power_Meter_GUI
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window, INotifyPropertyChanged
    {
        private Server server;
        private IWiPoMInterface controller;

        private string lampBitmap;
        public string LampBitmap
        {
            get
            {
                return this.lampBitmap;
            }

            set
            {
                this.lampBitmap = value;
                this.OnNotify("LampBitmap");
            }
        }

        private bool lightEnabled;

        public Window1()
        {
            InitializeComponent();

            this.LampBitmap = Properties.Settings.Default.lampOff;

            this.Lamp.DataContext = this;

            controller = new WiPoMInterface();

            server = new Server(80, controller);

            controller.ValueChanged += new EventHandler<ValueChangedEventArgs>(controller_ValueChanged);
            controller.PowerValueChanged += new EventHandler<ValueChangedEventArgs>(controller_PowerValueChanged);

        }

        void controller_PowerValueChanged(object sender, ValueChangedEventArgs e)
        {
            Dispatcher.BeginInvoke(new ChangePowerValueDelegate(this.ChangePowerValue), new object[] { e });
        }
        private delegate void ChangePowerValueDelegate(ValueChangedEventArgs e);

        public void ChangePowerValue(ValueChangedEventArgs e)
        {
            this.powerLabel.Content = e.ChangedValueNumber + " Watt";
            this.BarHeight.Height = (this.FullBar.Height -2)  - ( e.ChangedValueNumber / 60.0f * (this.FullBar.Height-2) );
        }

        void controller_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            Dispatcher.BeginInvoke(new ChangeButtonStatesDelegate(this.ChangeButtonStates), new object[] { e });
        }

        private delegate void ChangeButtonStatesDelegate(ValueChangedEventArgs e);

        public void ChangeButtonStates(ValueChangedEventArgs e)
        {
            Debug.WriteLine(e.ChangeVarName + "\t" + e.Enabled);
            Color color = e.Enabled ? Colors.White : Colors.Black;
            switch (e.ChangeVarName)
            {
                case "Up":
                    this.Up.Background = new SolidColorBrush(color);
                    break;
                case "Down":
                    this.Down.Background = new SolidColorBrush(color);
                    break;
                case "button":
                    if (e.Enabled)
                        this.LampBitmap = Properties.Settings.Default.lampOn;
                    else
                        this.LampBitmap = Properties.Settings.Default.lampOff;
                    this.lightEnabled = e.Enabled;
                    break;
                default:
                    break;
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.server.Stop();
        }

        public void OnNotify(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
