using System;
using System.Reflection;
using System.Web;
using CommunityServer.Components;

namespace CommunityServer
{
	/// <summary>
	/// Summary description for Global.
	/// </summary>
	public class Global : HttpApplication
	{
		protected void Application_Start(Object sender, EventArgs e)
		{
			Jobs.Instance().Start();
			EventLogs.Info("CS.Web Started", "Application", 200);
		}

		protected void Application_End(Object sender, EventArgs e)
		{
			string errorMessage = "CS.Web Stopped";

			HttpRuntime runtime = (HttpRuntime)typeof(HttpRuntime).InvokeMember("_theRuntime", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField, null, null, null);
			if (runtime != null)
			{
				string shutDownMessage = (string)runtime.GetType().InvokeMember("_shutDownMessage", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField, null, runtime, null);
				string shutDownStack = (string)runtime.GetType().InvokeMember("_shutDownStack", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField, null, runtime, null);
				errorMessage = string.Format("{0}\r\n\r\n_shutDownMessage={1}\r\n\r\n_shutDownStack={2}", errorMessage, shutDownMessage, shutDownStack);
			}

			EventLogs.Info("CS.Web Stopped " + errorMessage, "Application", 201);

			Jobs.Instance().Stop();
		}

		protected void Application_Error(Object sender, EventArgs e)
		{
		}

		protected void Session_Start(Object sender, EventArgs e)
		{
		}
		protected void Application_BeginRequest(Object sender, EventArgs e)
		{
		}
		protected void Application_EndRequest(Object sender, EventArgs e)
		{
		}
		protected void Application_AuthenticateRequest(Object sender, EventArgs e)
		{
		}
		protected void Session_End(Object sender, EventArgs e)
		{
		}
	}
}