using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NetTunnelApp
{
    internal class NetCardMenuItems
    {
        private static string _selItems = "";

        private List<ToolStripMenuItem> _menuItems = new List<ToolStripMenuItem>();

        private static List<NetCard> _menuDataItems = new List<NetCard>();

        public NetCardMenuItems(List<NetCard> netCardArray)
        {
            if (_menuItems.Count > 0)
            {
                _menuItems.Clear();
            }

            if (_selItems.Length == 0)
            {
                _selItems = netCardArray[0].UUID;
            }

            _menuDataItems.Clear();
            _menuDataItems.AddRange(netCardArray);

            foreach (var netCard in netCardArray)
            {
                IPNetwork ipnetwork = IPNetwork.Parse(netCard.IpAddr, netCard.NetMask);
                ToolStripMenuItem item = new ToolStripMenuItem();

                item.Text = netCard.Name + "-[" + netCard.MacAddr + "] " + ipnetwork.Network + "/" + ipnetwork.Cidr;
                item.Name = netCard.UUID;
                item.Click += delegate(object sender, EventArgs e)
                {
                    var control = (ToolStripMenuItem)sender;
                    _selItems = control.Name;

                    foreach (var v in _menuItems)
                    {
                        v.Checked = (_selItems == v.Name);
                    }
                };

                item.Checked = (_selItems == netCard.UUID);

                if (netCard.Description == "WireGuard Tunnel")
                {
                    item.Enabled = false;
                }

                _menuItems.Add(item);
            }
        }

        public List<ToolStripMenuItem> GetMenuItems()
        {
            return _menuItems;
        }

        public static NetCard? GetSelectItemData()
        {
            foreach (var k in _menuDataItems)
            {
                if (_selItems == k.UUID)
                {
                    return k;
                }
            }

            return null;
        }
    }
}