using System;
using System.Web.UI.WebControls;
using ASPNET.StarterKit.TimeTracker.BusinessLogicLayer;

namespace ASPNET.StarterKit.TimeTracker.Web
{
	//*********************************************************************
	//
	//	ResourceReport.aspx
	//
	//	This page is used by Administrator or Project Manager to monitor how
	//	consultants utilize their time.  Project managers can only view time 
	//	entries related to the projects they manage.
	//
	//*********************************************************************

	public class ResourceReportForm : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataList UserList;
		protected System.Web.UI.WebControls.HyperLink BackLink1;
		protected System.Web.UI.WebControls.HyperLink BackLink2;
		protected System.Web.UI.WebControls.Label NoData;

		protected DateTime _startDate = DateTime.MinValue;
		protected DateTime _endDate = DateTime.MinValue;
		
		private string _userIDs;

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Ensure that the visiting user has access to view the current page
			if (TTSecurity.IsInRole(TTUser.UserRoleAdminPMgr) == false) 
			{
				Response.Redirect("AccessDenied.aspx?Index=-1", true);
			}

			// Get user's input
			_userIDs = Request.QueryString["IDs"]==null? "0" : Request.QueryString["IDs"];
			string pageIndex = Request.QueryString["index"]==null? "0" : Request.QueryString["index"];

			_startDate = Convert.ToDateTime(Request.QueryString["Start"]==null? 
									DateTime.MinValue.ToShortDateString() : Request.QueryString["Start"]);
			_endDate = Convert.ToDateTime(Request.QueryString["End"]==null? 
									DateTime.MinValue.ToShortDateString() : Request.QueryString["End"]);

			BackLink1.NavigateUrl = "Reports.aspx?index=" + pageIndex;
			BackLink2.NavigateUrl = "Reports.aspx?index=" + pageIndex;

			BindList(_userIDs, _startDate, _endDate);
		}

		//*********************************************************************
		//
		//	BindList method retrieves all user summaries based on the User IDs passed in. 
		//	The appropriate user's information is retrieved only when the requesting user 
		//	has the appropriate authorization (i.e. Project Manager can only view his 
		//	team members information).  
		//
		//	The resulting collection is used to bind UserList web control.  This method also 
		//	display a friendly description when there is no data retrieved from the database.
		//
		//	Notice that User IDs are passed in as a comma delimited string.
		//
		//*********************************************************************

		private void BindList(string userIDs, DateTime start, DateTime end)
		{
			ResourceReportUserCollection userData = ResourceReportUser.GetUserSummary(TTSecurity.GetUserID(), userIDs, start, end);
			UserList.DataSource = userData;
			UserList.DataBind();

			// If there is no data, then display no data notification label.
			if (userData.Count == 0) 
			{
				UserList.Visible = false;
				NoData.Visible = true;
			}
		}

		//*********************************************************************
		//
		//	The SortGridData method is a helper method called when databinding the Time
		//	Entry grid to sort the columns of the grid.
		//
		//*********************************************************************

		private void SortGridData(TimeEntriesCollection list, string sortField, bool asc)
		{
			TimeEntriesCollection.TimeEntryFields sortCol = TimeEntriesCollection.TimeEntryFields.InitValue;
			switch(sortField)
			{
				case "EntryDate":
					sortCol = TimeEntriesCollection.TimeEntryFields.Day;
					break;
				case "ProjectName":
					sortCol = TimeEntriesCollection.TimeEntryFields.Project;
					break;
				case "CategoryName":
					sortCol = TimeEntriesCollection.TimeEntryFields.Category;
					break;
				case "Duration":
					sortCol = TimeEntriesCollection.TimeEntryFields.Hours;
					break;
				case "Description":
					sortCol = TimeEntriesCollection.TimeEntryFields.Description;
					break;
			}
			list.Sort(sortCol, asc);
		}

		//*********************************************************************
		//
		// ListTimeEntry retrieves sorted time entries for each user, and the result is
		// is used for returning TimeEntryGrid DataSource
		//
		//*********************************************************************

		protected TimeEntriesCollection ListTimeEntry(int mgrID, int userID, DateTime start, DateTime end)
		{
			TimeEntriesCollection entryList = BusinessLogicLayer.TimeEntry.GetEntries(mgrID, userID, start, end);

			//Always check for sorting when loading grid
			if (entryList.Count > 0) 
				SortGridData(entryList, SortField, SortAscending);
			else
				entryList = null;

			return entryList;
		}

		//*********************************************************************
		//
		//	This event fires when any header of the grid is clicked, saves the sort expression,
		//	and rebinds the Time Entry data grid with the correct sorting.
		//
		//*********************************************************************

		protected void TimeEntryGrid_Sort(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			SortField = e.SortExpression;
			BindList(_userIDs, _startDate, _endDate);
		}
		
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.ID = "ResourceReportForm";
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		string SortField 
		{
			get 
			{
				object o = ViewState["SortField"];
				if (o == null) 
				{
					return string.Empty;
				}
				return (string)o;
			}

			set 
			{
				if (value == SortField) 
				{
					// same as current sort file, toggle sort direction
					SortAscending = !SortAscending;
				}
				ViewState["SortField"] = value;
			}
		}

		//*********************************************************************
		//
		// SortAscending property is tracked in ViewState
		//
		//*********************************************************************

		bool SortAscending 
		{
			get 
			{
				object o = ViewState["SortAscending"];
				if (o == null) 
				{
					return true;
				}
				return (bool)o;
			}

			set 
			{
				ViewState["SortAscending"] = value;
			}
		}
	}
}
