using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WebServer;
using System.Diagnostics;

namespace WiPoMServer
{
    public partial class Form1 : Form
    {
        private Server server;
        private IWiPoMInterface controller;
        Button[] buttons;
        float actualPowerValue = 0;
        private bool lightEnabled = false;
        private bool pressedButton = false;

        public Form1()
        {
            InitializeComponent();

            buttons = new Button[4];
            buttons[0] = this.button1;
            buttons[1] = this.button2;
            buttons[2] = this.button3;

            controller = new WiPoMInterface();

            server = new Server(80, controller);

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

            Form1.SetDoubleBuffered(this.panel1); 
        }

        public static void SetDoubleBuffered(System.Windows.Forms.Control c)
        {
            //Taxes: Remote Desktop Connection and painting
            //http://blogs.msdn.com/oldnewthing/archive/2006/01/03/508694.aspx
            if (System.Windows.Forms.SystemInformation.TerminalServerSession)
                return;

            System.Reflection.PropertyInfo aProp =
                  typeof(System.Windows.Forms.Control).GetProperty(
                        "DoubleBuffered",
                        System.Reflection.BindingFlags.NonPublic |
                        System.Reflection.BindingFlags.Instance);

            aProp.SetValue(c, true, null);
        }


        private delegate void PowerValueChangedDelegate(object sender, ValueChangedEventArgs e);

        void controller_PowerValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (this.textBox1.InvokeRequired)
            {
                this.Invoke(new PowerValueChangedDelegate(controller_PowerValueChanged), new object[] { sender, e });
            }
            else
            {
                this.actualPowerValue = e.ChangedValueNumber / 60.0f * this.panel1.Height;
                this.textBox1.Text = e.ChangedValueNumber  + " Watt"; 
                // (int)(this.actualPowerValue / 255.0f * 60.0f) + " Watt";
                this.panel1.Invalidate();
            }
        }

        void controller_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            Debug.WriteLine(e.ChangeVarName + "\t" + e.Enabled);
            Color color = e.Enabled? Color.White: Color.Gray;
            switch (e.ChangeVarName)
            {
                case "Up":
                    this.buttons[0].BackColor = color;
                    break;
                case "Down":
                    this.buttons[1].BackColor = color;
                    break;
                case "button":
                    if (e.Enabled)
                        this.buttons[2].BackgroundImage = Properties.Resources.Lamplight1_64x64;
                    else
                        this.buttons[2].BackgroundImage = Properties.Resources.Lamplight2_64x64;
                    this.lightEnabled = e.Enabled;
                    this.Invalidate();
                    break;
                default:
                    break;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.server.Stop();
        }

        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {

            ((WiPoMInterface)controller).PowerValue = e.NewValue;
        }


        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            //Rectangle rect = new Rectangle( this.panel1.Left, this.panel1.Top, this.panel1.Width, 256 -(int) this.actualPowerValue);
            Rectangle rect = new Rectangle(0, 0, this.panel1.Width, 256 - (int) this.actualPowerValue);

            e.Graphics.FillRectangle(new SolidBrush(Color.Gray),rect);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.lightEnabled = !this.lightEnabled;

            this.controller.setValue("button",this.lightEnabled);
        }

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {

        }

        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.pressedButton = true;
            }
        }

    }
}
