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

using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
using CommunityServer.Galleries.Components;
using FormLabel = CommunityServer.ControlPanel.Controls.FormLabel;
using ResourceLabel = CommunityServer.ControlPanel.Controls.ResourceLabel;
namespace CommunityServer.ControlPanel.Controls
{
	/// <summary>
	///		Summary description for GroupEditControl.
	/// </summary>
	public class RoleQuotaEditControl : UserControl
	{

		#region Public Properties

		private Guid roleID;
		public Guid RoleID
		{
			get{return roleID;}
			set{roleID = value;}
		}
		private ApplicationType applicationType;
		public ApplicationType ApplicationType
		{
			get{return applicationType;}
			set{applicationType = value;}
		}

		#endregion

		#region Child Controls

		protected Label RoleName;
		protected RegularExpressionValidator ImageQuotaValidator;
		protected RegularExpressionValidator DiskQuotaValidator;
		protected FormLabel Formlabel1;
		protected FormLabel Formlabel2;
		protected TextBox ImageQuota;
		protected CommunityServer.ControlPanel.Controls.ResourceLabel Resourcelabel2;
		protected TextBox DiskQuota;
		protected Label MaxDiskQuotaLabel;
		protected Label MaxImageQuotaLabel;

		#endregion

		private string resourceFile = "ControlPanelResources.xml";

		private void Page_Load(object sender, EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				BindData();
			}

			RoleName.Text = Roles.GetRole(RoleID).Name;
		}

		#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 System.EventHandler(this.Page_Load);

		}
		#endregion

		protected void BindData() 
		{
			GalleryRoleQuota roleQuota = GalleryRoleQuotas.GetRoleQuota(RoleID);
			LoadRoleQuota( roleQuota );
		}

		protected virtual void LoadRoleQuota(GalleryRoleQuota roleQuota)
		{
			GalleryConfiguration config = GalleryConfiguration.Instance();

			
			if(config.ImageQuota > -1)
				MaxImageQuotaLabel.Text = ResourceManager.GetString("CP_PhotosShared_Quotas_MaxImageQuota", resourceFile) + " " + config.GetFormattedImageQuota();
			if(config.DiskQuota > -1)
				MaxDiskQuotaLabel.Text = ResourceManager.GetString("CP_PhotosShared_Quotas_MaxDiskQuota", resourceFile) + " " + config.GetFormattedDiskQuota();

			if(roleQuota != null)
			{
				ImageQuota.Text = config.GetFormattedImageQuota(roleQuota.ImageQuota);
				DiskQuota.Text = config.GetFormattedDiskQuota(roleQuota.DiskQuota);
			}
		}
		
		public bool Save()
		{
			if(Page.IsValid)
			{
				GalleryConfiguration config = GalleryConfiguration.Instance();
				GalleryRoleQuota roleQuota = new GalleryRoleQuota(RoleID);

				roleQuota.ImageQuota = config.ParseQuota(this.ImageQuota.Text,GalleryQuotaType.Image);
				roleQuota.DiskQuota = config.ParseQuota(this.DiskQuota.Text,GalleryQuotaType.Disk);
				return SaveRoleQuota(roleQuota);

			}
			return false;
		}

		protected bool SaveRoleQuota(GalleryRoleQuota roleQuota)
		{

			if(Page.IsValid)
			{
				GalleryRoleQuotas.UpdateRoleQuota(roleQuota);
				return true;
			}
			else
				return false;
		}
	}
}
