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

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CA = ComponentArt.Web.UI;
using ResourceLabel = CommunityServer.ControlPanel.Controls.ResourceLabel;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Membership
{
	/// <summary>
	/// Summary description for MemberSearch.
	/// </summary>
	public class MemberSearch : BaseMembershipPage
	{

		override protected void OnInit(EventArgs e)
		{
			Grid1.PageIndexChanged += new CA.Grid.PageIndexChangedEventHandler(OnPageIndexChanged);
			Grid1.SortCommand += new CA.Grid.SortCommandEventHandler(OnSortCommand);
			Grid1.NeedRebind += new CA.Grid.NeedRebindEventHandler(OnNeedRebind);
			Grid1.NeedDataSource += new CA.Grid.NeedDataSourceEventHandler(OnNeedDataSource);
			this.PageList.SelectedIndexChanged +=new EventHandler(PageList_SelectedIndexChanged);
			this.Load += new EventHandler(this.Page_Load);
			base.OnInit(e);
		}
		

		#region Members

		protected CA.Grid Grid1;
		protected DropDownList PageList;
		protected Modal Modal1;
		protected UserSearch UserSearchControl;
		protected ControlPanelSelectedNavigation SelectedNavigation1;
		protected ContextMenu MemberContextMenu;
		int recordCount = 0;

		#endregion


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

		protected override void OnPreRender(EventArgs e)
		{
			string path = this.Request.Url.AbsolutePath.Substring(0, this.Request.Url.AbsolutePath.LastIndexOf('/') + 1);
			MemberContextMenu.Menu.Items[2].ClientSideCommand = MemberContextMenu.Menu.Items[2].ClientSideCommand.Replace(path, "");
			base.OnPreRender(e);
		}

		private void Bind()
		{

			CSContext context = CSContext.Current;
			int ps = context.GetIntFromQueryString("ps", context.SiteSettings.MembersPerPage);

			ListItem li = PageList.Items.FindByValue(ps.ToString());
			if(li != null)
				li.Selected = true;

			Grid1.PageSize = Int32.Parse(PageList.SelectedValue);
			UserSet users = BuildGridData();
			Grid1.DataSource = users.Users;
			this.recordCount = users.TotalRecords;
			Grid1.DataBind();
			Grid1.RecordCount = recordCount;

			//Taken from the BaseGridControl Helper function - In the future this page should inherit from that base class
			foreach(CA.GridColumn gc in Grid1.Levels[0].Columns)
			{
				if(gc.Visible)
				{
					gc.SortedDataCellCssClass = "SortedDataCell";
					//Allows for mock resource manager calls in heading text
					//eg HeadingText="ResourceManager.CP_Photos_GridCol_Name"
					if(gc.HeadingText.StartsWith("ResourceManager."))
						gc.HeadingText = ResourceManager.GetString(gc.HeadingText.Replace("ResourceManager.",""));
			
				}
			}
		}


		private UserSet BuildGridData()
		{
			CSContext context = CSContext.Current;
			int pageSize = context.GetIntFromQueryString("ps", context.SiteSettings.MembersPerPage);
			Grid1.PageSize = pageSize;
        
			return UserSearchControl.GetUsersAndBindControl(Grid1.CurrentPageIndex, pageSize, Grid1.Sort);

		}

		#region Events
		private void PageList_SelectedIndexChanged(object sender, EventArgs e)
		{
  			string url  = "{0}?tab={1}&su={2}&ps={3}&t={4}&se={5}&r={6}&s={7}&jc={8}&jd={9}&pc={10}&pd={11}&sb={12}&so={13}";
			url = string.Format(url, Request.Path, Request.QueryString["tab"], Request.QueryString["su"], PageList.SelectedValue, Request.QueryString["t"], Request.QueryString["se"], Request.QueryString["r"], Request.QueryString["s"], Request.QueryString["jc"], Request.QueryString["jd"], Request.QueryString["pc"], Request.QueryString["pd"], Request.QueryString["sb"], Request.QueryString["so"]);
			Response.Redirect(url);
		}

		public void OnPageIndexChanged(object sender, CA.GridPageIndexChangedEventArgs args)
		{
			Grid1.CurrentPageIndex = args.NewIndex;
		}

		public void OnSortCommand(object sender, CA.GridSortCommandEventArgs args)
		{
			Grid1.Sort = args.SortExpression;
		}

		public void OnNeedDataSource(object sender, EventArgs oArgs)
		{
			UserSet users = BuildGridData();
			Grid1.DataSource = users.Users;
			this.recordCount = users.TotalRecords;
		}

		private void InitializeComponent()
		{

		}

		public void OnNeedRebind(object sender, EventArgs oArgs)
		{
			Grid1.DataBind();
			Grid1.RecordCount = recordCount;
		}

		#endregion

	}
}
