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 EMDHardwareClientSimulation.Core;

namespace EMDHardwareClientSimulation
{
    public partial class frmEMDHWClientSimulation : Form
    {
        public frmEMDHWClientSimulation()
        {
            MainController.getInstance();

            InitializeComponent();

            MainController.getInstance().Ui = this;
        }

        private void btnSwitch_Click(object sender, EventArgs e)
        {
            MainController.getInstance().toggleSwitch(112);

            // Send toggleValue command
            if (lblSwitch.Text.Equals("Switch Off"))
            {
                btnSwitch.Text = "Off";
                lblSwitch.BackColor = Color.Green;
                lblSwitch.Text = "Switch On";
            }
            else
            {
                btnSwitch.Text = "On";
                lblSwitch.BackColor = Color.Red;
                lblSwitch.Text = "Switch Off";
            }
        }

        private void btnButton_MouseDown(object sender, MouseEventArgs e)
        {
            MainController.getInstance().buttonPressed(110);

            // Send buttonPressed command
            lblButton.BackColor = Color.Green;
            lblButton.Text = "Button Pressed";
        }

        private void btnButton_MouseUp(object sender, MouseEventArgs e)
        {
            MainController.getInstance().buttonReleased(110);

            // Send buttonReleased command
            lblButton.BackColor = Color.Red;
            lblButton.Text = "Button Released";
        }

        private void btnButton2_MouseDown(object sender, MouseEventArgs e)
        {
            MainController.getInstance().buttonPressed(111);

            // Send buttonPressed command
            lblButton2.BackColor = Color.Green;
            lblButton2.Text = "Button Pressed";
        }

        private void btnButton2_MouseUp(object sender, MouseEventArgs e)
        {
            MainController.getInstance().buttonReleased(111);

            // Send buttonReleased command
            lblButton2.BackColor = Color.Red;
            lblButton2.Text = "Button Released";
        }

        private void frmEMDHWClientSimulation_FormClosing(object sender, FormClosingEventArgs e)
        {
            MainController.getInstance().Exit();
        }

        private delegate void setDimmerTextDelegate(string labelText);
        private delegate void setPowerMeterTextDelegate(string labelText);
        private delegate void setLampDelegate(bool lampOn);
        private delegate void setDimmer2TextDelegate(string labelText);
        private delegate void setPowerMeter2TextDelegate(string labelText);
        private delegate void setLamp2Delegate(bool lampOn);

        public void setDimmerText(string labelText)
        {
            if (this.lblDimmer.InvokeRequired)
            {
                this.lblDimmer.Invoke(new setDimmerTextDelegate(setDimmerText), new object[] { labelText });
            }
            else
            {
                this.lblDimmer.Text = labelText;
            }
        }

        public void setPowerMeterText(string labelText)
        {
            if (this.lblPowerMeter.InvokeRequired)
            {
                this.lblPowerMeter.Invoke(new setPowerMeterTextDelegate(setPowerMeterText), new object[] { labelText });
            }
            else
            {
                this.lblPowerMeter.Text = labelText;
            }
        }

        public void setLamp(bool lampOn)
        {
            if (this.picLamp.InvokeRequired)
            {
                this.picLamp.Invoke(new setLampDelegate(setLamp), new object[] { lampOn });
            }
            else
            {
                if (lampOn)
                {
                    this.picLamp.Image = Properties.Resources.lamp_on;
                }
                else
                {
                    this.picLamp.Image= Properties.Resources.lamp_off;
                }                
            }
        }

        public void setDimmer2Text(string labelText)
        {
            if (this.lblDimmer2.InvokeRequired)
            {
                this.lblDimmer2.Invoke(new setDimmer2TextDelegate(setDimmer2Text), new object[] { labelText });
            }
            else
            {
                this.lblDimmer2.Text = labelText;
            }
        }

        public void setPowerMeter2Text(string labelText)
        {
            if (this.lblPowerMeter2.InvokeRequired)
            {
                this.lblPowerMeter2.Invoke(new setPowerMeter2TextDelegate(setPowerMeter2Text), new object[] { labelText });
            }
            else
            {
                this.lblPowerMeter2.Text = labelText;
            }
        }

        public void setLamp2(bool lampOn)
        {
            if (this.picLamp.InvokeRequired)
            {
                this.picLamp.Invoke(new setLamp2Delegate(setLamp2), new object[] { lampOn });
            }
            else
            {
                if (lampOn)
                {
                    this.picLamp2.Image = Properties.Resources.lamp_on;
                }
                else
                {
                    this.picLamp2.Image = Properties.Resources.lamp_off;
                }
            }
        }
    }
}
