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

using System;
using System.Collections;
using System.Data;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Galleries.Components;
using CA = ComponentArt.Web.UI;

namespace CommunityServer.ControlPanel.Photos
{
	/// <summary>
	///		Summary description for SwitchControl.
	/// </summary>
	public class SwitchControl : BaseGalleryGridControl
	{

		protected CA.Grid Grid1;

		private void Page_Load(object sender, EventArgs e)
		{
			if(!Page.IsPostBack  && !base.GalleryPage.IsCallBack)
			{
				buildGrid();
				Grid.DataBind();
			}
		}

		#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.Load += new EventHandler(this.Page_Load);
			this.EnableSearchBox =true;

		}
		#endregion

		protected override void buildGrid()
		{
			CSContext context = CSContext.Current;

			ArrayList sections;

			sections = CommunityServer.Galleries.Galleries.GetGalleries(context.User.UserID, false, true, false);

			sections = Sections.FilterByAccessControl(sections, Permission.Post, context.User);

			DataTable dt = CADataConverter.ToDataTable(sections, typeof(Section));

			
			dt.DefaultView.Sort = "ApplicationKey";

			//If we actually have grouping turned on, we predefine the group to GroupName ASC
			if(this.EnableGrouping)
			{
				//This is the pre-defined group by for the switch page
				Grid.GroupBy = "GroupName ASC";
				dt.DefaultView.Sort = "GroupName";
			}

			Grid.DataSource = dt;

			this.RecordCount = dt.Rows.Count;
			
			ApplyUserSettings();
		}

		protected override void ConfigureGrid()
		{
			AddGridCheckMarkTemplate("IsActive");

			//See if the user has a reutrn URL so we can send them back to their original requested page
			string sectionURL = Globals.AppendQuerystring(GalleryUrls.Instance().ControlPanel_Photos, @"sectionid=## DataItem.GetMember(""SectionID"").Text ##", true);
			string returnUrl = CSContext.Current.ReturnUrl;
			if(!Globals.IsNullorEmpty(returnUrl) )
				sectionURL = Globals.AppendQuerystring(returnUrl, @"sectionid=## DataItem.GetMember(""SectionID"").Text ##", true); 
			AddGridHrefTemplate(sectionURL,"","Name", @"## DataItem.GetMember(""Description"").Text ##");
			AddGridHrefTemplate(sectionURL, "ApplicationKey") ;

			AddGridPagerTemplate("Name", "GroupName","IsActive");
		}
	}
}
