using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using Ebel.Hardware.Communications;

namespace HwComWrapperNETGui.UserControls
{
    public partial class WifiConfigControl : UserControl
    {
        public WifiConfigControl()
        {
            InitializeComponent();
            
            this.lblDhcpStartAddress.DataBindings.Add("Enabled", this.cbDhcpEnabled, "Checked");
            this.tbDhcpStartAddress.DataBindings.Add("Enabled", this.cbDhcpEnabled, "Checked");
            this.lblDhcpEndAddress.DataBindings.Add("Enabled", this.cbDhcpEnabled, "Checked");
            this.tbDhcpEndAddress.DataBindings.Add("Enabled", this.cbDhcpEnabled, "Checked");
            this.lblDnsAddress.DataBindings.Add("Enabled", this.cbDhcpEnabled, "Checked");
            this.tbDnsAddress.DataBindings.Add("Enabled", this.cbDhcpEnabled, "Checked");
            this.lblDefaultGw.DataBindings.Add("Enabled", this.cbDhcpEnabled, "Checked");
            this.tbDefaultGw.DataBindings.Add("Enabled", this.cbDhcpEnabled, "Checked");

            this.nudChannelNumber.Enabled = !this.cbChannelAutoselect.Checked;
            this.cbChannelBandwidth.SelectedIndex = 1;
            this.nudSignalPower.Maximum = uint.MaxValue;
        }

        public WifiConfig GetWifiConfig()
        {
            WifiConfig wifi = new WifiConfig();

            wifi.Mode = (byte)nudWifiMode.Value;

            wifi.BroadcastSsid = cbBroadcastSsid.Checked;
            wifi.Ssid = tbSsid.Text;

            try
            {
                wifi.AccessPointAddress = IPAddress.Parse(this.tbAccessPointAddress.Text);
                wifi.AccessPointNetmask = IPAddress.Parse(this.tbAccessPointNetmask.Text);
            }
            catch (FormatException e)
            {
                var caption = string.Empty;
                if (wifi.AccessPointAddress == null)
                {
                    caption = "Access Point Address invalid";
                }
                else if (wifi.AccessPointNetmask == null)
                {
                    caption = "Access Point Netmask invalid";
                }

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

            wifi.DhcpEnable = cbDhcpEnabled.Checked;
            if (cbDhcpEnabled.Checked)
            {
                try
                {

                    wifi.DhcpStartAddress = IPAddress.Parse(this.tbDhcpStartAddress.Text);
                    wifi.DhcpEndAddress = IPAddress.Parse(this.tbDhcpEndAddress.Text);
                    wifi.DnsAddress = IPAddress.Parse(this.tbDnsAddress.Text);
                    wifi.DefaultGw = IPAddress.Parse(this.tbDefaultGw.Text);
                }
                catch (FormatException e)
                {
                    var caption = string.Empty;
                    if (wifi.DhcpStartAddress == null)
                    {
                        caption = "DHCP Start Address invalid";
                    }
                    else if (wifi.DhcpEndAddress == null)
                    {
                        caption = "DHCP End Address invalid";
                    }
                    else if (wifi.DnsAddress == null)
                    {
                        caption = "DHCP DNS Address invalid";
                    }
                    else if (wifi.DefaultGw == null)
                    {
                        caption = "DHCP Default Gateway invalid";
                    }

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

            wifi.ChannelAutoselect = cbChannelAutoselect.Checked;
            if (!cbChannelAutoselect.Checked)
            {
                wifi.Channel = (byte)nudChannelNumber.Value;
            }

            try
            {
                wifi.ChannelBandwidth = ushort.Parse(cbChannelBandwidth.SelectedItem.ToString());
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Channel Bandwith invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }

            wifi.SignalPower = (uint)nudSignalPower.Value;

            wifi.EncryptionType = (byte)nudEncryptionType.Value;
            wifi.EncryptionParameters = (byte)nudEncryptionParameters.Value;
            wifi.Passphrase = tbPassphrase.Text;

            return wifi;
        }
        
        private void CbChannelAutoselectCheckedChanged(object sender, EventArgs e)
        {
            this.nudChannelNumber.Enabled = !this.cbChannelAutoselect.Checked;
        }
    }
}
