//------------------------------------------------------------------------------
// <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.Galleries.Components;
using CA = ComponentArt.Web.UI;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.PhotoAdmin
{
	/// <summary>
	///		Summary description for GroupListControl.
	/// </summary>
	public class GroupListControl : BaseGalleryAdminGridControl
	{

		#region Child Controls
		
		protected CheckBox chkConfirmInsert;
		protected CheckBox chkConfirmUpdate;
		protected CheckBox chkConfirmDelete;
		protected CA.Grid Grid1;
		protected ResourceControl Resourcecontrol1;
		protected ModalLink NewGroup;

		#endregion

		private bool IsEdit = true;

		private const string EditGroupUrl = "GroupForm.aspx?GroupID={0}";

		private void Page_Load(object sender, EventArgs e)
		{
			NewGroup.Text = Components.ResourceManager.GetString("CP_PhotosAdmin_GroupList_AddNew");
			NewGroup.Url = string.Format(EditGroupUrl,"-1");

			if(!Page.IsPostBack && !GalleryAdminPage.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);
			Grid.DeleteCommand += new CA.Grid.GridItemEventHandler(this.Grid_DeleteCommand);
		}
		
		/// <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);

		}
		#endregion

		private void UpdateDb(CA.GridItem item, string command)
		{

			//Check to see that the user can administer
			//Ideally we will add conditional formatting on the grid to hide these options too
			if(!CSContext.Current.User.IsGalleryAdministrator)
				throw new CSException( CSExceptionType.AdministrationAccessDenied );

			this.IsEdit = true;
			Group group;

			switch (command)
			{
				case "DELETE":
					
					group = GalleryGroups.GetGroup(int.Parse(item["GroupID"].ToString()), false, true, true);
					if(GalleryGroups.HasChildSections(group.GroupID))
						throw new ReadOnlyException(ResourceManager.GetString("CP_PhotosAdmin_GroupList_GroupNotEmptyException"));

					Groups.DeleteGroup(group);

					break;

				default: 
					throw new NotImplementedException(string.Concat("The {0} command has not yet been implemented!" ,command)); 
			}
		}


		private void Grid_DeleteCommand(object sender, CA.GridItemEventArgs e)
		{
			UpdateDb(e.Item, "DELETE"); 
		}

		protected override void buildGrid()
		{
			ArrayList groups = GetGroups();
			Grid.DataSource = CADataConverter.ToDataTable(groups, typeof(Group));
			this.RecordCount = groups.Count;

			base.buildGrid();
		}

		protected override void ConfigureGrid()
		{
			AddGridPagerTemplate("Name");
			AddGridHrefTemplate(@"gallerylist.aspx?GroupID=## DataItem.GetMember(""GroupID"").Text ##","Name");
		}


		protected ArrayList GetGroups()
		{
			return GalleryGroups.GetGroups(false, true, this.IsEdit);
		}


	}
}
