using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
using System.Resources;

namespace EduNetworkBuilder
{
    public partial class NetworkCardEditor : Form
    {
        NetworkCard MyNicToEdit;
        Button btnVPNEnd = null;
        TextBox tbVPNEncrypt = null;
        TextBox tbSSID = null;
        Label lblVPNEnd = null;
        Label lblVPNEncrypt = null;

        private NetworkCardEditor()
        {
            InitializeComponent();
            LanguagifyComponents();
        }

        public NetworkCardEditor(NetworkCard NicToEdit)
        {
            InitializeComponent();
            LanguagifyComponents();
            //***************
            //****  VPN  ****
            //***************
            if (NicToEdit.GetNicType == NicType.tun || NicToEdit.GetNicType == NicType.vpn)
            {
                AutoSize = false;
                btnVPNEnd = new Button();
                btnVPNEnd.Location = new Point(TBMacAddress.Location.X, cbDHCP.Location.Y + 28);
                string btnval = "0.0.0.0";
                if (NicToEdit.TunnelEndpoint != null)
                    btnval = NicToEdit.TunnelEndpoint.GetIPString;
                btnVPNEnd.Text = btnval;
                btnVPNEnd.Click += btnVPNEnd_Click;

                lblVPNEnd = new Label();
                lblVPNEnd.Location = new Point(10, btnVPNEnd.Location.Y);
                lblVPNEnd.Text = NB.Translate("NCE_NCEVPNEnd");
                Controls.Add(btnVPNEnd);
                Controls.Add(lblVPNEnd);
                this.Height = lblVPNEnd.Location.Y + lblVPNEnd.Height + 80;

                if (NicToEdit.GetNicType == NicType.vpn)
                {
                    tbVPNEncrypt = new TextBox();
                    tbVPNEncrypt.Location = new Point(TBMacAddress.Location.X, btnVPNEnd.Location.Y + 28);

                    lblVPNEncrypt = new Label();
                    lblVPNEncrypt.Location = new Point(10, tbVPNEncrypt.Location.Y);
                    lblVPNEncrypt.Text = NB.Translate("NCE_NCEEncrypt");
                    Controls.Add(tbVPNEncrypt);
                    Controls.Add(lblVPNEncrypt);
                    this.Height = tbVPNEncrypt.Location.Y + tbVPNEncrypt.Height + 80;
                }
                Refresh();
                //AutoSize = true;
            }
            //******************
            //***  WIRELESS  ***
            //******************
            if (NicToEdit.GetNicType == NicType.wlan || NicToEdit.GetNicType == NicType.wport)
            {
                AutoSize = false;
                tbSSID = new TextBox();
                tbSSID.Location = new Point(TBMacAddress.Location.X, cbDHCP.Location.Y + 28);

                lblVPNEnd = new Label();
                lblVPNEnd.Location = new Point(10, tbSSID.Location.Y);
                lblVPNEnd.Text = "SSID:";
                Controls.Add(tbSSID);
                Controls.Add(lblVPNEnd);
                this.Height = lblVPNEnd.Location.Y + lblVPNEnd.Height + 80;

                tbVPNEncrypt = new TextBox();
                tbVPNEncrypt.Location = new Point(TBMacAddress.Location.X, tbSSID.Location.Y + 28);

                lblVPNEncrypt = new Label();
                lblVPNEncrypt.Location = new Point(10, tbVPNEncrypt.Location.Y);
                lblVPNEncrypt.Text = NB.Translate("NCE_NCEWireless");
                Controls.Add(tbVPNEncrypt);
                Controls.Add(lblVPNEncrypt);
                this.Height = tbVPNEncrypt.Location.Y + tbVPNEncrypt.Height + 80;
                Refresh();
                //AutoSize = true;
            }
            TBMacAddress.Enabled = false;
            tbNicName.Enabled = false;
            tbNicType.Enabled = false;
            cbCanUseDHCP.Enabled = false;

            TBMacAddress.Text = NicToEdit.MAC;
            tbNicName.Text = NicToEdit.NicName();
            tbNicType.Text = NicToEdit.GetNicType.ToString();

            MyNicToEdit = NicToEdit;
            cbCanUseDHCP.Checked = NicToEdit.CanUseDHCP;
            cbDHCP.Checked = NicToEdit.UsesDHCP;
            if (!NicToEdit.CanUseDHCP) cbDHCP.Enabled = false;
            if(NicToEdit.MustUseDHCP)
            {
                cbDHCP.Enabled = false;
                cbCanUseDHCP.Enabled = false;
            }
            Network theNet = NB.GetNetwork();
            NetworkDevice nd = theNet.GetDeviceFromID(NicToEdit.myID);
            if(theNet.ItemIsLocked(nd.hostname, NicToEdit.NicName(), NetTestType.LockNic))
            {
                cbDHCP.Enabled = false;
                cbCanUseDHCP.Enabled = false;
                TBMacAddress.Enabled = false;
                tbNicName.Enabled = false;
                tbNicType.Enabled = false;
            }
            UpdateForm();
        }

        private void LanguagifyComponents()
        {
            Text = NB.Translate("NCE_cbCanUseDHCP");
            Text = NB.Translate("NCE_cbDHCP");
            Text = NB.Translate("NCE_label1");
            Text = NB.Translate("NCE_label2");
            Text = NB.Translate("NCE_label3");
            Text = NB.Translate("_Done");
            Text = NB.Translate("NCE_lblLinkStatus");
            Text = NB.Translate("NCE_Form");
        }

        private void UpdateForm()
        {
            if (btnVPNEnd != null)
            {
                string btnval = "0.0.0.0";
                if (MyNicToEdit.TunnelEndpoint != null)
                    btnval = MyNicToEdit.TunnelEndpoint.GetIPString;
                btnVPNEnd.Text = btnval;
            }
            if(tbSSID != null)
            {
                tbSSID.Text = MyNicToEdit.SSID;
            }
            if(tbVPNEncrypt != null)
            {
                tbVPNEncrypt.Text = MyNicToEdit.EncryptionKey;
            }
            if (MyNicToEdit.GetNicType == NicType.tun || MyNicToEdit.GetNicType == NicType.vpn || 
                MyNicToEdit.GetNicType == NicType.management_interface || MyNicToEdit.GetNicType == NicType.lo)
            {
                lblLinkStatus.Visible = false;
            }
            else
                lblLinkStatus.Visible = true;
            if (MyNicToEdit.isConnected(true))
                lblLinkStatus.Text = NB.Translate("NCE_UdfrmCon");
            else
                lblLinkStatus.Text = NB.Translate("NCE_UdfrmDiscon");
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            MyNicToEdit.CanUseDHCP = cbCanUseDHCP.Checked;
            MyNicToEdit.UsesDHCP = cbDHCP.Checked;
            SaveDataToNic();
            Close();
        }

        private void SaveDataToNic()
        {
            if (tbVPNEncrypt != null)
            {
                MyNicToEdit.EncryptionKey = tbVPNEncrypt.Text;
            }
            if (tbSSID != null)
            {
                MyNicToEdit.SSID = tbSSID.Text;
            }
        }

        private void btnVPNEnd_Click(object sender, EventArgs e)
        {
            Network theNet = NB.GetNetwork();
            NetworkDevice nd = theNet.GetDeviceFromID(MyNicToEdit.myID);
            if(MyNicToEdit.TunnelEndpoint == null)
            {
                MyNicToEdit.TunnelEndpoint = new NB_IPAddress(NB.ZeroIPString, "255.255.255.0", IPAddressType.ip_only);
            }
            MyNicToEdit.TunnelEndpoint.Edit(nd, this, NB.Translate("_Endpt"));
            SaveDataToNic();
            UpdateForm();
        }

        private void NetworkCardEditor_Shown(object sender, EventArgs e)
        {
            BuilderWindow BW = NB.GetBuilderWin();
            if (BW != null)
            {
                Point tLocation = new Point(BW.Location.X + ((BW.Width - Width) / 2), BW.Location.Y + ((BW.Height - Height) / 2));
                Location = tLocation;
            }
        }
    }
}
