using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace EduNetworkBuilder
{

    public partial class FirewallEditor : Form
    {
        NetworkDevice FirewallDevice = null;

        public FirewallEditor(NetworkDevice WhatToEdit)
        {
            InitializeComponent();
            FirewallDevice = WhatToEdit;
            UpdateForm();
            Icon = Properties.Resources.NBIco;
        }

        string InterfaceVLAN(string device)
        {
            string answer = "";
            string basedevice = Regex.Replace(device, ":.*","");
            NetworkCard nic = FirewallDevice.NicFromName(basedevice);
            if (nic != null)
            {
                NetworkInterface nif = nic.InterfaceFromName(device);
                if(nif != null)
                {
                    Network myNet = NB.GetNetwork();
                    int id = nif.GetNonForbiddenVLANID();
                    foreach(VLANName vn in myNet.VlanNames)
                    {
                        if(vn.ID == id)
                        {
                            answer = vn.Name;
                            break;
                        }
                    }
                }
            }
            return answer;
    }

        void UpdateForm()
        {
            int selected = lbRules.SelectedIndex;
            lbRules.Items.Clear();
            foreach (FirewallRule FW in FirewallDevice.FirewallRules)
            {
                string svlan = InterfaceVLAN(FW.Source);
                if (svlan != "") svlan = svlan + "  ";
                string dvlan = InterfaceVLAN(FW.Destination);
                if (dvlan != "") dvlan = dvlan + "  ";
                string OneLine = FW.Action.ToString() + " " + FW.Source + " " + svlan + " -> " + FW.Destination + " " + dvlan;
                lbRules.Items.Add(OneLine);
            }
            if (selected >= 0 && selected < lbRules.Items.Count)
                lbRules.SelectedIndex = selected;
            else
            {
                if (lbRules.Items.Count > 0)
                    lbRules.SelectedIndex = 0;
            }
            //Make the buttons visible
            if (lbRules.SelectedIndex >= 0)
            {
                btnDel.Visible = true;
                btnEdit.Visible = true;
            }
            else
            {
                btnDel.Visible = false;
                btnEdit.Visible = false;
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            FirewallRule FW = new FirewallRule("","",FirewallRuleType.Drop);
            NetTestEditor NTE = new NetTestEditor(FW,FirewallDevice);
            NTE.ShowDialog();
            if(FW.Source!="" && FW.Destination != "")
            {
                FirewallDevice.FirewallRules.Add(FW);
            }
            UpdateForm();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {

            if (lbRules.SelectedIndex >= 0)
            {
                if (FirewallDevice.FirewallRules.Count > lbRules.SelectedIndex)
                {
                    int old = lbRules.SelectedIndex;
                    FirewallDevice.FirewallRules.RemoveAt(lbRules.SelectedIndex);
                    old--;
                    if (old > -1)
                        lbRules.SelectedIndex = old;
                    UpdateForm();
                }
            }
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            EditCurrent();
        }

        private void EditCurrent()
        {
            if (lbRules.SelectedIndex >= 0)
            {
                if (FirewallDevice.FirewallRules.Count > lbRules.SelectedIndex)
                {
                    FirewallRule FW = FirewallDevice.FirewallRules[lbRules.SelectedIndex];
                    NetTestEditor NTE = new NetTestEditor(FW, FirewallDevice);
                    NTE.ShowDialog();
                    UpdateForm();
                }
            }
        }

        private void lbRules_DoubleClick(object sender, EventArgs e)
        {
            EditCurrent();
        }

        private void lbRules_MouseDown(object sender, MouseEventArgs e)
        {
            //Do right-click stuff here
            if(e.Button == MouseButtons.Right)
            {
                if (lbRules.ContextMenuStrip == null)
                {
                    lbRules.ContextMenuStrip = new ContextMenuStrip();
                }
                lbRules.ContextMenuStrip.Items.Clear();
                //we can open a right-click menu
                int index = 0;
                lbRules.ContextMenuStrip.Items.Add(NB.Translate("_Add"));
                lbRules.ContextMenuStrip.Items[index++].Click += btnNew_Click;
                if (lbRules.SelectedIndex >= 0)
                {
                    lbRules.ContextMenuStrip.Items.Add(NB.Translate("_Edit"));
                    lbRules.ContextMenuStrip.Items[index++].Click += btnEdit_Click;

                    lbRules.ContextMenuStrip.Items.Add(NB.Translate("_Delete"));
                    lbRules.ContextMenuStrip.Items[index++].Click += btnDel_Click;                    
                }
                
                lbRules.ContextMenuStrip.Visible = true;
                lbRules.ContextMenuStrip.Show(Cursor.Position);
            }
        }

        private void FirewallEditor_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;
            }
        }
    }
}
