using System;
using System.Diagnostics.Eventing.Reader;
using System.Net;
using System.Windows.Forms;
using Ebel.Hardware.Communications;

namespace HwComWrapperNETGui.UserControls
{
    public partial class MsgCat01 : UserControl
    {
        #region Constructors and Destructors

        public MsgCat01()
        {
            this.InitializeComponent();

            this.comboBoxSwSleep.DataSource = Enum.GetValues(typeof(ConfigStateRequested));
            this.comboBoxSwSleep.SelectedIndex = 0;
            
            this.button0C.Click += (sender, args) => this.HandleButtonClick(this.ButtonGetNicConfigClicked);
            this.button0D.Click += this.OnButtonSetNicConfigClicked;
            this.button0E.Click += (sender, args) => this.HandleButtonClick(this.ButtonGetDeviceNameClicked);
            this.button0F.Click += this.OnButtonSetDeviceNameClicked;
            this.button10.Click += (sender, args) => this.HandleButtonClick(this.ButtonGetWakeupSleepConfigClicked);
            this.button11.Click += this.OnButtonSetWakupSleepConfigClicked;
            this.button12.Click += (sender, args) => this.HandleButtonClick(this.ButtonGetTerminationConfigClicked);
            this.button13.Click += this.OnButtonSetTerminationConfigClicked;
            this.button14.Click += (sender, args) => this.HandleButtonClick(this.ButtonGetSwSleepConfigClicked);
            this.button15.Click += this.OnButtonSetSwSleepConfigClicked;
            this.btnGetWifiConfigReq.Click += (sender, args) => this.HandleButtonClick(this.ButtonGetWifiConfigClicked);
            this.btnSetWifiConfigReq.Click += this.OnButtonSetWifiConfigClicked;
            this.btnSetWifiInfoReq.Click += (sender, args) => this.HandleButtonClick(this.ButtonGetWifiInfoClicked);
        }

        #endregion

        #region Public Events

        /// <summary>
        ///     Occurs when the COM_GetDeviceNameReq button was clicked. 
        /// </summary>
        public event EventHandler ButtonGetDeviceNameClicked;
        
        /// <summary>
        ///     Occurs when the COM_GetNicConfigReq button was clicked. 
        /// </summary>
        public event EventHandler ButtonGetNicConfigClicked;

        /// <summary>
        ///     Occurs when the COM_GetSwSleepConfigReq button was clicked.
        /// </summary>
        public event EventHandler ButtonGetSwSleepConfigClicked;

        /// <summary>
        ///     Occurs when the COM_GetTerminationConfigReq button was clicked. 
        /// </summary>
        public event EventHandler ButtonGetTerminationConfigClicked;
        
        /// <summary>
        ///     Occurs when the COM_GetWakeupSleepConfigReq button was clicked. 
        /// </summary>
        public event EventHandler ButtonGetWakeupSleepConfigClicked;
        
        /// <summary>
        ///     Occurs when the COM_SetDeviceNameReq button was clicked.
        /// </summary>
        public event EventHandler<DeviceNameEventArgs> ButtonSetDeviceNameClicked;
        
        /// <summary>
        ///     Occurs when the COM_SetNicConfigReq button was clicked.
        /// </summary>
        public event EventHandler<NICConfigEventArgs> ButtonSetNicConfigClicked;

        /// <summary>
        ///     Occurs when the COM_SetSwSleepConfigReq button was clicked.
        /// </summary>
        public event EventHandler<SoftwareSleepConfigEventArgs> ButtonSetSwSleepConfigClicked;

        /// <summary>
        ///     Occurs when the COM_SetTerminationConfigReq button was clicked.
        /// </summary>
        public event EventHandler<TerminationConfigEventArgs> ButtonSetTerminationConfigClicked;

        /// <summary>
        ///     Occurs when the COM_SetWakeupSleepConfigReq button was clicked.
        /// </summary>
        public event EventHandler<WakeupSleepConfigEventArgs> ButtonSetWakeupSleepConfigClicked;

        /// <summary>
        ///     Occurs when the COM_GetWifiConfigReq button was clicked. 
        /// </summary>
        public event EventHandler ButtonGetWifiConfigClicked;

        /// <summary>
        ///     Occurs when the COM_SetWifiConfigReq button was clicked.
        /// </summary>
        public event EventHandler<WifiConfigEventArgs> ButtonSetWifiConfigClicked;

        /// <summary>
        ///     Occurs when the COM_GetWifiInfoReq button was clicked. 
        /// </summary>
        public event EventHandler ButtonGetWifiInfoClicked;
        #endregion

        #region Methods

        private void HandleButtonClick(EventHandler handler)
        {
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }

        private void OnButtonSetDeviceNameClicked(object sender, EventArgs eventArgs)
        {
            var handler = this.ButtonSetDeviceNameClicked;
            if (handler == null)
            {
                return;
            }

            handler(this, new DeviceNameEventArgs(this.textBoxDeviceName.Text));
        }

        private void OnButtonSetNicConfigClicked(object sender, EventArgs eventArgs)
        {
            var handler = this.ButtonSetNicConfigClicked;
            if (handler == null)
            {
                return;
            }

            IPAddress ip = null;
            IPAddress subnet = null;
            IPAddress gateway = null;

            try
            {
                ip = IPAddress.Parse(this.textBoxIP.Text);
                subnet = IPAddress.Parse(this.textBoxSubnet.Text);
                gateway = IPAddress.Parse(this.textBoxGateway.Text);
            }
            catch (FormatException e)
            {
                var caption = string.Empty;
                if (ip == null)
                {
                    caption = "IP invalid";
                }
                else if (subnet == null)
                {
                    caption = "Subnet invalid";
                }
                else if (gateway == null)
                {
                    caption = "Gateway invalid";
                }

                MessageBox.Show(e.Message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            handler(this, new NICConfigEventArgs(new NICConfig { IP = ip, Subnet = subnet, Gateway = gateway }));
        }
        
        private void OnButtonSetSwSleepConfigClicked(object sender, EventArgs e)
        {
            var handler = this.ButtonSetSwSleepConfigClicked;
            if (handler == null)
            {
                return;
            }

            ConfigState configState;
            switch ((ConfigStateRequested)this.comboBoxSwSleep.SelectedValue)
            {
                case ConfigStateRequested.Disable:
                    configState = ConfigState.Disabled;
                    break;
                case ConfigStateRequested.Enable:
                    configState = ConfigState.Enabled;
                    break;
                default:
                    configState = ConfigState.NotSupported;
                    break;
            }

            handler(this, new SoftwareSleepConfigEventArgs(new SoftwareSleepConfig(configState)));
        }

        private void OnButtonSetTerminationConfigClicked(object sender, EventArgs eventArgs)
        {
            var handler = this.ButtonSetTerminationConfigClicked;
            if (handler == null)
            {
                return;
            }

            var terminationConfig = this.terminationInfoControl1.GetTerminationConfig();
            handler(this, new TerminationConfigEventArgs(terminationConfig));
        }

        private void OnButtonSetWakupSleepConfigClicked(object sender, EventArgs eventArgs)
        {
            var handler = this.ButtonSetWakeupSleepConfigClicked;
            if (handler == null)
            {
                return;
            }

            var wakeupSleepConfig = this.wakeupSleepInfoControl1.GetWakeupSleepConfig();
            handler(this, new WakeupSleepConfigEventArgs(wakeupSleepConfig));
        }

        private void OnButtonSetWifiConfigClicked(object sender, EventArgs eventArgs)
        {
            var handler = this.ButtonSetWifiConfigClicked;
            if (handler == null)
            {
                return;
            }

            WifiConfig wifiConfig = null;
            if (ModifierKeys.HasFlag(Keys.Control))
            {
                WifiDialog dlg = new WifiDialog();
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    wifiConfig = dlg.GetWifiConfig();
                }
                else
                {
                    return;
                }
            }
            else
            {
                byte mode = 0;
                try
                {
                    mode = Convert.ToByte(this.nudWifiMode.Value);
                }
                catch (OverflowException e)
                {
                    MessageBox.Show(e.Message, "Invalid WIFI mode", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                wifiConfig = new WifiConfig();
                wifiConfig.Mode = mode;
            }

            handler(this, new WifiConfigEventArgs(wifiConfig));
        }

        #endregion
    }
}