using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Trigger.Tasks.Plugins
{
	class OnHibernate : TaskPlugin
	{
		#region Properties
		internal const string PathKeePass = @"P:\Program Files (x86)\KeePass\KeePass.exe";
		internal const string PathThunderbird = @"P:\Programme\Mozilla Thunderbird\thunderbird.exe";
		internal const string PathFirefox = @"P:\Program Files (x86)\Mozilla Firefox\firefox.exe";
		internal const string PathTrueCrypt = @"P:\Programme\TrueCrypt\TrueCrypt.exe";
		internal const string PathTrueCryptContainer = @"Z:\Sonstiges\proxy.pac";

		internal Main Main;
		internal Log Log;

		internal bool ThunderbirdRunning = false;
		internal bool FirefoxRunning = false;
		#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 (Status.System.UserName != "Jonny")		// wrong PC
				return false;

			if (!Main.EventMgr.PluginExists<Events.Power>())
			{
				this.Log.LogLineDate("Plugin \"OnHibernate\" misses Event \"Power\"!", Trigger.Log.Type.Error);
				return false;
			}

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

			swInit.Stop();
			Events.Power pwrEvent = Main.EventMgr.GetPlugin<Events.Power>(new object[]{Main}, true);
			swInit.Start();

			pwrEvent.Suspend += new Events.EventPlugin.Event(sysEvent_Suspend);
			pwrEvent.Resume += new Events.EventPlugin.Event(sysEvent_Resume);
			Main.CommandLineTrigger += new Events.EventPlugin.EventValue<int>(Main_CommandLineTrigger);
			return true;
		}

		public override void Dispose()
		{
			Events.Power pwrEvent = this.Main.EventMgr.GetPlugin<Events.Power>(new object[] {Main}, true);

			pwrEvent.Suspend += new Events.EventPlugin.Event(sysEvent_Suspend);
			pwrEvent.Resume += new Events.EventPlugin.Event(sysEvent_Resume);
			Main.CommandLineTrigger += new Events.EventPlugin.EventValue<int>(Main_CommandLineTrigger);
			
		}
		#endregion

		#region Event Handlers
		private void sysEvent_Suspend(object sender, EventArgs e)
		{
			Process Thunderbird = null;
			{
				Process[] Thunderbirds = Process.GetProcessesByName("thunderbird");
				if (Thunderbirds != null && Thunderbirds.Length > 0)
					Thunderbird = Thunderbirds[0];
			}
			Process Firefox = null;
			{
				Process[] Firefoxes = Process.GetProcessesByName("firefox");
				if (Firefoxes != null && Firefoxes.Length > 0)
					Firefox = Firefoxes[0];
			}

			if (Thunderbird != null)
			{
				ThunderbirdRunning = true;
				Thunderbird.Kill();
				this.Log.LogLineDate("--> Thunderbird killed", Trigger.Log.Type.Action);
			}
			if (Firefox != null)
			{
				FirefoxRunning = true;
				Firefox.Kill();
				this.Log.LogLineDate("--> Firefox killed", Trigger.Log.Type.Action);
			}


			Process.Start(PathTrueCrypt, "/d /f /s /q");						// force dismount
			this.Log.LogLineDate("--> TrueCrypt volumes dismounted", Trigger.Log.Type.Action);
		}

		private void sysEvent_Resume(object sender, EventArgs e)
		{
			ProcessStartInfo KeePass = new ProcessStartInfo(PathKeePass);
			Process.Start(KeePass);
			this.Log.LogLineDate("--> KeePass started", Trigger.Log.Type.Action);
		}

		

		private void Main_CommandLineTrigger(object sender, Events.EventArgsValue<int> e)
		{
			if (e.Value == "Suspend".GetHashCode())
			{
				this.sysEvent_Suspend(sender, e);
				return;
			}
			if (e.Value == "Resume".GetHashCode())
			{
				this.sysEvent_Resume(sender, e);
				return;
			}
			if (e.Value != "OnHibernate/KeePassStarted".GetHashCode())							// this is not our event
				return;

			while (!System.IO.File.Exists(@"J:\Sonstiges\ping"))
				System.Threading.Thread.Sleep(500);

			if (ThunderbirdRunning)
			{
				ProcessStartInfo Thunderbird = new ProcessStartInfo(PathThunderbird);
				Process.Start(Thunderbird);
				this.Log.LogLineDate("--> Thunderbird started", Trigger.Log.Type.Action);
				ThunderbirdRunning = false;
			}

			if (FirefoxRunning)
			{
				ProcessStartInfo Firefox = new ProcessStartInfo(PathFirefox);
				Process.Start(Firefox);
				this.Log.LogLineDate("--> Firefox started", Trigger.Log.Type.Action);
				FirefoxRunning = false;
			}
		}
		#endregion
	}
}
