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

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

namespace CommunityServer.ControlPanel.Controls.Galleries
{
	/// <summary>
	///		Summary description for DefaultPermissionListControl.
	/// </summary>
	public class RoleQuotasControl : BaseGalleryAdminGridControl
	{

		#region Child Controls
		
		protected CheckBox chkConfirmInsert;
		protected CheckBox chkConfirmUpdate;
		protected CA.Grid Grid1;
		protected CheckBox chkConfirmDelete;

		#endregion

		protected ArrayList permissionChanges;
		private string resourceFile = "ControlPanelResources.xml";
		private GalleryConfiguration config = GalleryConfiguration.Instance();

		private void Page_Load(object sender, EventArgs e)
		{
			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);
			this.Grid.ItemDataBound += new CA.Grid.ItemDataBoundEventHandler(Grid_ItemDataBound);
		}
		
		/// <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 Grid_ItemDataBound(object sender, CA.GridItemDataBoundEventArgs e)
		{
			Literal image = (Literal)e.Content.FindControl("imageText");
			Literal disk = (Literal)e.Content.FindControl("diskText");
			string noQuota = CommunityServer.Components.ResourceManager.GetString("CP_PhotosAdmin_Quotas_None", resourceFile);

			if(image != null)
			{
				image.Text = Convert.ToInt32(e.Item["ImageQuota"]) == -1 ? noQuota : e.Item["ImageQuota"].ToString();
			}

			if(disk != null)
			{
				disk.Text = Convert.ToInt32(e.Item["DiskQuota"]) == -1 ? noQuota : config.GetFormattedDiskQuota(Convert.ToInt32(e.Item["DiskQuota"]));
			}
		}

		protected override void buildGrid()
		{
			ArrayList unsortedRoles = Roles.GetRoles();
			//ArrayList setRoleQuotas = GalleryRoleQuotas.GetRoleQuotas();

			SortedList roleQuotas = new SortedList();

			foreach(Role role in unsortedRoles)
			{
				if(!roleQuotas.ContainsKey(role.Name))
				{
					if(GalleryRoleQuotas.Exists(role.RoleID))
					{
						roleQuotas.Add(role.Name, GalleryRoleQuotas.GetRoleQuota(role.RoleID));
					}
					else
					{
						roleQuotas.Add(role.Name, new GalleryRoleQuota(role.RoleID));
					}
				}
			}
			
			//The grid will only show checkboxes in edit mode if the data source is a DataTable
			Grid.DataSource = CADataConverter.ToDataTable((IList)roleQuotas.Values, typeof(GalleryRoleQuota));

			this.RecordCount = roleQuotas.Count;

			ApplyUserSettings();
			
			//Override the default page size because we don't have a pager
			Grid.PageSize = 99999;
			Grid.ShowFooter = false;

		}
	}
}
