//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Configuration;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.ControlPanel.UI;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Tools
{
	/// <summary>
	/// Summary description for JobsReport.
	/// </summary>
	public class JobsReport : BaseToolsPage
	{
		#region Members

		protected System.Web.UI.WebControls.Label Created;
		protected System.Web.UI.WebControls.Label LastStart;
		protected System.Web.UI.WebControls.Label LastStop;
		protected System.Web.UI.WebControls.Label IsRunning;
		protected System.Web.UI.WebControls.Label Minutes;

		protected System.Web.UI.WebControls.Repeater SharedThreadJobRepeater;
		protected CommunityServer.ControlPanel.Controls.ControlPanelSelectedNavigation SelectedNavigation1;
		protected CommunityServer.ControlPanel.Controls.ResourceControl RegionTitle;
		protected CommunityServer.Controls.MPContent DescriptionRegion;
		protected CommunityServer.ControlPanel.Controls.ResourceControl Resourcecontrol1;
		protected CommunityServer.ControlPanel.Controls.ResourceControl Resourcecontrol21;
		protected CommunityServer.ControlPanel.Controls.ResourceControl Resourcecontrol10;
		protected CommunityServer.ControlPanel.Controls.ResourceControl Resourcecontrol6;
		protected CommunityServer.ControlPanel.Controls.ResourceControl Resourcecontrol7;
		protected CommunityServer.ControlPanel.Controls.ResourceControl Resourcecontrol8;
		protected CommunityServer.ControlPanel.Controls.ResourceControl Resourcecontrol9;
		protected CommunityServer.ControlPanel.Controls.ResourceControl Resourcecontrol22;
		protected CommunityServer.Controls.MPContent TaskRegion;
		protected CommunityServer.Controls.MPContainer MPContainer;
		protected System.Web.UI.WebControls.Repeater SingleThreadJobRepeater;


		#endregion

		override protected void OnInit(EventArgs e)
		{
			this.Load += new System.EventHandler(this.Page_Load);

			base.OnInit(e);
		}


		private void Page_Load(object sender, System.EventArgs e)
		{
			if ( !Page.IsPostBack ) 
			{
				this.Bind();
			}
		}

		private void InitializeComponent()
		{
		
		}

		protected void Bind()
		{

			ListDictionary stats = Jobs.Instance().CurrentStats;
			this.Created.Text = Convert.ToDateTime(stats["Created"]) == DateTime.MinValue ? "--" : Convert.ToDateTime(stats["Created"]).ToString();
			this.LastStart.Text = Convert.ToDateTime(stats["LastStart"]) == DateTime.MinValue ? "--" : Convert.ToDateTime(stats["LastStart"]).ToString();
			this.LastStop.Text = Convert.ToDateTime(stats["LastStop"]) == DateTime.MinValue ? "--" : Convert.ToDateTime(stats["LastStop"]).ToString();
			this.IsRunning.Text = Convert.ToBoolean(stats["IsRunning"]).ToString();
			this.Minutes.Text = Convert.ToString(stats["Minutes"]);

			ArrayList sharedThreadJobs = new ArrayList();
			ArrayList singleThreadJobs = new ArrayList();
			Hashtable allJobs = Jobs.Instance().CurrentJobs;
			foreach(Job j in allJobs.Values)
			{
				if(j.SingleThreaded)
					sharedThreadJobs.Add(j);
				else
					singleThreadJobs.Add(j);
			}

			SharedThreadJobRepeater.DataSource = sharedThreadJobs;
			SingleThreadJobRepeater.DataSource = singleThreadJobs;

			SharedThreadJobRepeater.DataBind();
			SingleThreadJobRepeater.DataBind();

		}
	}
}
