using System.Net.NetworkInformation;
using System.Windows.Forms;

namespace Trigger.Tasks
{
	class ProxyHska : TaskPlugin
	{
		#region Properties
		private Main Main;
		private Log Log;
		private TreeNode tnProxy;

		private static System.Net.WebProxy proxySettings = new System.Net.WebProxy(
			"proxy.hs-karlsruhe.de:8888", true, 
			new string[]{@"\*\.hs-karlsruhe\.de", @"\*\.local", @"localhost"});
		private const string InterfaceVPN = "Cisco Systems VPN Adapter";
		private const string InterfaceWLAN = "{C08419E3-B090-4AE8-B17F-051FE8A882B6}";
		#endregion

		#region Methods
		public override bool Init(Main Main)
		{
			return Init(Main, new System.Diagnostics.Stopwatch());
		}
		public override bool Init(Main Main, System.Diagnostics.Stopwatch swInit)
		{
			if (!Main.EventMgr.PluginExists<Events.Network>())
			{
				this.Log.LogLine("Task \"ProxyHsKA\" is missing EventPlugin \"Network\"!", Log.Type.Error);
				return false;
			}

			this.Main = Main;
			this.Log = Main.Log;

			swInit.Stop();
			Events.Network networkEvents = Main.EventMgr.GetPlugin<Events.Network>();
			swInit.Start();

			networkEvents.NetworkAvailabilityChanged += event_AvailabilityChanged;
			networkEvents.NetworkInterfaceAdded += event_InterfaceAdded;
			networkEvents.NetworkInterfaceRemoved += event_InterfaceRemoved;

			ProxyCheck();
			return true;
		}
		public override void Dispose()
		{
			Events.Network networkEvents = this.Main.EventMgr.GetPlugin<Events.Network>();

			networkEvents.NetworkAvailabilityChanged -= event_AvailabilityChanged;
			networkEvents.NetworkInterfaceAdded -= event_InterfaceAdded;
			networkEvents.NetworkInterfaceRemoved -= event_InterfaceRemoved;
		}

		private void ProxyEnable()
		{
			Actions.Network.ProxyEnable(proxySettings);
			this.Log.LogLineDate("--> Proxy enabled", Log.Type.Action);
		}

		private void ProxyDisable()
		{
			Actions.Network.ProxyDisable();
			this.Log.LogLineDate("--> Proxy disabled", Log.Type.Action);
		}

		/// <summary>
		/// <para>Checks the current connections (<see cref="NetworkInterface"/>s) and sets Proxy accordingly</para>
		/// </summary>
		private bool ProxyCheck()
		{
			bool proxy = Status.Network.ProxyConfiguration.Address != null;
			foreach (NetworkInterface ni in Status.Network.AllNetworkInterfaces)
			{
				if (Status.Network.NetworkInterfaceHasDnsSuffix(ni, "HS-Karlsruhe.de"))
				{
					if (!proxy)
						ProxyEnable();
					return true;
				}
			}

			if (proxy)
				ProxyDisable();
			return false;
		}

		public override TreeNode GetStatus(TreeView tv)
		{
			TreeNode tn = new TreeNode("ProxyHsKa");

			if (Status.Network.ProxyConfiguration.Address != null)
				tnProxy = tn.Nodes.Add("Proxy is enabled");
			else
				tnProxy = tn.Nodes.Add("Proxy is disabled");

			return tn;
		}
		#endregion

		#region Event handlers
		private void event_InterfaceAdded(object sender, Events.EventArgsValue<NetworkInterface> e)
		{
			if (Status.Network.NetworkInterfaceHasDnsSuffix(e.Value, "HS-Karlsruhe.de"))
				ProxyEnable();
		}

		private void event_InterfaceRemoved(object sender, Events.EventArgsValue<NetworkInterface> e)
		{
			if (Status.Network.NetworkInterfaceHasDnsSuffix(e.Value, "HS-Karlsruhe.de"))
				ProxyDisable();
		}


		void event_AvailabilityChanged(object sender, Events.EventArgsValue<bool> e)
		{
			if (e.Value == false)		// only proceed when connecting to a network
				return;

			if (/*Status.Network.NetworkInterfaceHasIP(InterfaceWLAN, "193.196.") || */Status.Network.NetworkInterfaceHasDnsSuffix(InterfaceWLAN, "HS-Karlsruhe.de"))
				ProxyEnable();
			else
				ProxyDisable();
		}



		void TreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			if (e.Node != tnProxy)
				return;

			if (ProxyCheck())
				tnProxy.Text = "Proxy is enabled";
			else
				tnProxy.Text = "Proxy is disabled";
		}
		#endregion
	}
}
