using System;
using System.ComponentModel;
using System.Diagnostics;
using System.ServiceProcess;
using Minntech.Logging;

namespace Minntech.Listener
{
	public class Listener : System.ServiceProcess.ServiceBase
	{
		private System.ComponentModel.Container components = null;

		#region construct/init/dispose

		public Listener()
		{
			InitializeComponent();
		}
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// Listener
			// 
			this.CanShutdown = true;
			this.ServiceName = "Listener";

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#endregion

		// The main entry point for the process
		static void Main()
		{
			System.ServiceProcess.ServiceBase[] ServicesToRun;
			ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Listener() };
			System.ServiceProcess.ServiceBase.Run(ServicesToRun);
		}


		/// <summary>
		/// Set things in motion so your service can do its work.
		/// </summary>
		protected override void OnStart(string[] args)
		{
			InitializeLogging();
			Log.GetInstance().WriteLog(this.GetType().Name + " Start");
		}
 
		/// <summary>
		/// Stop this service.
		/// </summary>
		protected override void OnStop()
		{
			Log.GetInstance().WriteLog(this.GetType().Name + " Stop");
		}

		/// <summary>
		/// Flush everything in the txQUEUE to database
		/// </summary>
		protected override void OnShutdown()
		{
			//TODO: flush everything in the txQUEUE to database
		}

		#region private helpers

		private void InitializeLogging()
		{
			// listener uses windows application log for logging
			Log.GetInstance().Provider = (ILogProvider) new Logging.ApplicationLog();
		}

		#endregion
	}
}
