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

namespace ASPNET.StarterKit.TimeTracker.Web
{
	//*********************************************************************
	//
	// ProjectList.aspx
	//
	// The ProjectList.aspx is used to display the list of projects available.
	// Only members of the Admin or Manager Role can access this page.
	// Members of the Admin group will see all projects and members of the
	// Managers group will see only the projects they are managing.
	//
	//*********************************************************************

	public class ProjectList : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.LinkButton	LinkButton1;
		protected System.Web.UI.WebControls.Button		NewProjectButton;
		protected System.Web.UI.WebControls.DataGrid	ProjectsGrid;

		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);
			}

			if (!IsPostBack)
			{
				BindProjects();
			}
		}
		
		//*********************************************************************
		//
		// The BindProjects method retrieves the list of projects the current user
		// can view based on their role and then databinds them to the ProjectsGrid
		//
		//*********************************************************************

		private void BindProjects()
		{
			ProjectsCollection projectList = Project.GetProjects(TTSecurity.GetUserID(), TTSecurity.GetUserRole());

			// Call method to sort the data before databinding
			SortGridData(projectList, SortField, SortAscending);

			ProjectsGrid.DataSource = projectList;
			ProjectsGrid.DataBind();
		}
		
		//*********************************************************************
		//
		// The SortGridData method is a helper method called when databinding the Projects grid 
		// to sort the columns of the grid.
		//
		//*********************************************************************

		private void SortGridData(ProjectsCollection list, string sortField, bool asc)
		{
			ProjectsCollection.ProjectFields sortCol = ProjectsCollection.ProjectFields.InitValue;

			switch(sortField)
			{
				case "Name":
					sortCol = ProjectsCollection.ProjectFields.Name;
					break;
				case "Manager":
					sortCol = ProjectsCollection.ProjectFields.ManagerUserName;
					break;
				case "Completion":
					sortCol = ProjectsCollection.ProjectFields.CompletionDate;
					break;
				case "Duration":
					sortCol = ProjectsCollection.ProjectFields.Duration;
					break;
				default:
					break;
			}

			list.Sort(sortCol, asc);
		}

		#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.NewProjectButton.Click += new System.EventHandler(this.NewProjectButton_Click);
			this.ProjectsGrid.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.ProjectsGrid_Sort);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		//*********************************************************************
		//
		// The NewProjectButton_Click event handler redirects to the ProjectDetails.aspx 
		// page so the user can create a new project.
		//
		//*********************************************************************

		private void NewProjectButton_Click(object sender, System.EventArgs e)
		{
			int mainIndex = (Request["index"]==null) ? 0 : Convert.ToInt32(Request["index"]);
			int adminIndex = (Request["adminindex"]==null) ? 0 : Convert.ToInt32(Request["adminindex"]);
			Response.Redirect(String.Format("ProjectDetails.aspx?index={0}&adminindex={1}", mainIndex, adminIndex), false);
		}

		//*********************************************************************
		//
		// The ProjectsGrid_Page event handler sets the index of the currently displayed 
		// page for the Projects grid and re-binds it.
		//
		//*********************************************************************

		protected void ProjectsGrid_Page(Object sender, DataGridPageChangedEventArgs e) 
		{
			ProjectsGrid.CurrentPageIndex = e.NewPageIndex;

			BindProjects();
		}

		//*********************************************************************
		//
		// The ProjectGrid_Sort event handler changes the sortfield for the Projects grid 
		// and re-binds it.
		//
		//*********************************************************************

		private void ProjectsGrid_Sort(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			SortField = e.SortExpression;

			BindProjects();
		}

		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;
			}
		}
	}
}
