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

using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;
using CA = ComponentArt.Web.UI;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Forums
{
	/// <summary>
	///		Summary description for ForumListControl.
	/// </summary>
	public class ForumListControl : BaseForumGridControl
	{
		protected CA.Grid Grid1;
		protected ResourceControl FeedbackFilterLabel;
		protected DropDownList AdminGroupList;
		private int groupID;
		protected string DeleteWarning;

		override protected void OnInit(EventArgs e)
		{
			this.Load += new EventHandler(this.Page_Load);
			this.AdminGroupList.SelectedIndexChanged += new EventHandler(this.AdminGroupList_SelectedIndexChanged);
			this.EnableSearchBox = true;
			base.OnInit(e);
			this.Grid.DeleteCommand += new CA.Grid.GridItemEventHandler(this.Grid_DeleteCommand);
		}

		private void Page_Load(object sender, EventArgs e)
		{
			if(!Globals.IsNullorEmpty(AdminGroupList.SelectedValue))
				groupID = Globals.SafeInt(AdminGroupList.SelectedValue.ToString(), -1); 
			else
				groupID = CSContext.Current.GetIntFromQueryString("GroupID",-1) ;
			
			DeleteWarning = ResourceManager.GetString("CP_Forums_SectionEdit_DeleteForumVerify");

			if(!Page.IsPostBack && !ForumPage.IsCallBack)
			{
				BindGroupList();
				ConfigurePage();
				buildGrid();
				Grid.DataBind();
			}

		}


		private void BindGroupList()
		{
			ArrayList groups = ForumGroups.GetForumGroups(true, true, false); 
			foreach(Group group in groups)
				AdminGroupList.Items.Add(new ListItem(group.Name, group.GroupID.ToString()));
	
			AdminGroupList.Items.Insert(0,new ListItem(CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_Forums_Home_ForumGroup"),"-1"));
		}

		private void ConfigurePage()
		{
			ListItem li = AdminGroupList.Items.FindByValue(groupID.ToString());
			if(li != null)
				li.Selected = true;
		}

		protected override void buildGrid()
		{
			ArrayList sections = GetForums();

			sections = Sections.FilterByAccessControl(sections, Permission.Administer, CSContext.Current.User);

			//The grid will only show checkboxes in edit mode if the data source is a DataTable
			DataTable dt = CADataConverter.ToDataTable(sections, typeof(Section));
			dt.DefaultView.Sort = "GroupName";
			Grid.DataSource = dt;

			this.RecordCount = dt.Rows.Count;

			ApplyUserSettings();

			if(this.EnableGrouping)
			{
				Grid.GroupBy = "GroupName ASC";
			}

		}

		private void AdminGroupList_SelectedIndexChanged(object sender, EventArgs e)
		{
			//TODO: Figure out how to use a CA filter and dump this hack
			//Grid.CurrentPageIndex = 0;
			//Grid.DataBind();
			
			string url  = "{0}?GroupID={1}";
			url = string.Format(url, Request.Path, AdminGroupList.SelectedValue);
			Response.Redirect(url);

		}

		protected ArrayList GetForums()
		{
			ArrayList unfiltered = CommunityServer.Discussions.Components.Forums.GetForums(true, false, false, false);

			ArrayList filtered = new ArrayList();
			//HACK: We need to filter the list here until we can get a client side filter in place
			if(groupID == -1)
			{
				// We don't want to include the Private Messages pseudo section
				foreach(Forum f in unfiltered)
					if(f.SectionID != 0)
						filtered.Add(f);
			}
			else
			{
				foreach(Forum f in unfiltered)
					if(f.GroupID == groupID)
						filtered.Add(f);
			}

			return filtered;
		}

		protected ArrayList GetGroups()
		{
			return ForumGroups.GetForumGroups(false, true, false);
		}

		protected override void ConfigureGrid()
		{
			AddGridCheckMarkTemplate("IsActive");
		
			string forumEditorURL = Globals.GetSiteUrls().UrlData.FormatUrl("forums_ControlPanel_EditForum", "##  DataItem.GetMember('SectionID').Text ##");
			AddGridHrefTemplate(forumEditorURL, "Name");

			string[] urls = new string[] {forumEditorURL, "## DataItem.GetMember('Url').Text ##", @"javascript:deleteRow(""## DataItem.ClientId ##"");"};
			string[] labels = new string[] {ResourceManager.GetString("CP_Blog_GridCol_Edit"), ResourceManager.GetString("CP_Blog_GridCol_View"), ResourceManager.GetString("CP_Blog_GridCol_Delete")};
			string[] cssClasses = new string[] {"CommonTextButton", "CommonTextButton", "CommonTextButton"};

			this.AddGridHrefTemplate(urls, labels, cssClasses, "Url", " ");

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

		private void Grid_DeleteCommand(object sender, CA.GridItemEventArgs e)
		{
			Forum f = Discussions.Components.Forums.GetForum(Globals.SafeInt(e.Item["SectionID"].ToString(), 0), false, true);
			if ((f != null) && (f.ForumType != ForumType.Reporting) && (f.ForumType != ForumType.Deleted))
			{
				Sections.DeleteSection(f);
				return;
			}
		}


	}
}
