//------------------------------------------------------------------------------
// <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 ForumGroupListControl : BaseForumGridControl
	{
		protected CA.Grid Grid1;
		protected string DeleteWarning;

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

		private void Page_Load(object sender, EventArgs e)
		{
			DeleteWarning = ResourceManager.GetString("CP_Forums_TreeView_DeleteGroupConfirmation");

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

		protected override void buildGrid()
		{
			ArrayList groups = GetGroups();

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

			this.RecordCount = dt.Rows.Count;

			ApplyUserSettings();

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

		}

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

		protected override void ConfigureGrid()
		{
			string groupEditorURL = Globals.GetSiteUrls().UrlData.FormatUrl("forums_ControlPanel_EditGroup", "##  DataItem.GetMember('GroupID').Text ##");
			AddGridHrefTemplate(groupEditorURL, "Name");

			string manageGroupURL = Globals.GetSiteUrls().UrlData.FormatUrl("forums_ControlPanel_ForumListByGroup", "##  DataItem.GetMember('GroupID').Text ##");
			string[] urls = new string[] {groupEditorURL, manageGroupURL, @"javascript:deleteRow(""## DataItem.ClientId ##"");"};
			string[] labels = new string[] {ResourceManager.GetString("CP_Blog_GridCol_Edit"), ResourceManager.GetString("CP_Photos_Manage"), ResourceManager.GetString("CP_Blog_GridCol_Delete")};
			string[] cssClasses = new string[] {"CommonTextButton", "CommonTextButton", "CommonTextButton"};

			this.AddGridHrefTemplate(urls, labels, cssClasses, "GroupID", " ");
			
			AddGridPagerTemplate("Name");
		}

		private void Grid_DeleteCommand(object sender, CA.GridItemEventArgs e)
		{
			CommunityServer.Components.Group group = ForumGroups.GetForumGroup(Globals.SafeInt(e.Item["GroupID"].ToString(), 0), false, true, CSContext.Current.UserID);
			if(group != null)
			{
				// Don't delete a group if it has sections
				if (ForumGroups.HasChildSections(group.GroupID))
					throw new ReadOnlyException(ResourceManager.GetString("CP_Forums_TreeView_GroupNotEmptyException"));

				// Delete empty group
				Groups.DeleteGroup(group);

				// Flush cache
				ForumGroups.GetForumGroups(false, true, true);
			}
		}
	}
}
