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

using System;
using System.Collections;

using CommunityServer.Components;

namespace CommunityServer.Galleries.Components
{
	/// <summary>
	/// Summary description for GalleryRoleQuotas.
	/// </summary>
	public class GalleryRoleQuotas
	{
		private static String cacheKey = "Gallery-RoleQuotas";

		private GalleryRoleQuotas(){}

		/// <summary>
		/// Returns a list of role quotas for all image galleries
		/// </summary>
		/// <returns></returns>
		public static ArrayList GetRoleQuotas()
		{			
			// Serve from the cache when possible
			//
			ArrayList quotasCollection = CSCache.Get(cacheKey) as ArrayList;
			if(quotasCollection == null) 
			{

				quotasCollection = GalleryDataProvider.Instance().GetRoleQuotas();
				if(quotasCollection != null) 
				{
					// Insert the collection into the cache for 10 minutes
					CSCache.Insert(cacheKey, quotasCollection,10 * CSCache.MinuteFactor);
				}
			}

			return quotasCollection;
		}

		public static GalleryRoleQuota GetRoleQuota(Guid roleID)
		{
			ArrayList roleQuotas = GetRoleQuotas();

			return GetRoleQuota(roleID, roleQuotas);
		}

		private static GalleryRoleQuota GetRoleQuota(Guid roleID, ArrayList roleQuotas)
		{
			foreach(GalleryRoleQuota roleQuota in roleQuotas)
			{
				if(roleQuota.RoleID == roleID) return roleQuota;
			}

			return null;
		}

		#region Manage role quotas: Create/Update/Delete
		// ********************************************************************/
		//  UpdateName
		//
		/// <summary>
		/// Update selected disallowed name with a new name in the data store.
		/// <param name="quota">The quota to be updated</param>
		/// <returns>No of updated rows</returns>
		/// </summary>
		/// 
		// ********************************************************************/
		public static void UpdateRoleQuota(GalleryRoleQuota quota) 
		{
			if(quota.ImageQuota == -1 && quota.DiskQuota == -1)
			{
				CreateUpdateDeleteRoleQuota(quota, DataProviderAction.Delete);
			}
			else
			{
				CreateUpdateDeleteRoleQuota(quota, DataProviderAction.Update);
			}
		}

		private static void CreateUpdateDeleteRoleQuota(GalleryRoleQuota quota, DataProviderAction action ) 
		{
			CSCache.Remove( cacheKey );
		
			GalleryDataProvider dp = GalleryDataProvider.Instance();

			dp.CreateUpdateDeleteGalleryRoleQuota(quota, action);
		}
		#endregion

		public static bool Exists(Guid roleID)
		{
			ArrayList roleQuotas = GetRoleQuotas();

			foreach(GalleryRoleQuota roleQuota in roleQuotas)
			{
				if(roleQuota.RoleID == roleID) return true;
			}
			return false;
		}

		public static int GetMaxRoleQuota(Section section, GalleryQuotaType quotaType)
		{
			int quota = -1;
			ArrayList distinctRoles = new ArrayList();
			ArrayList roles;
			ArrayList roleQuotas = GetRoleQuotas();
			GalleryRoleQuota roleQuota;
			User owner;

			// Check if section has any owners
			if(section.OwnerArray.Length > 0)
			{
				// Iterate through owners
				foreach(string ownerName in section.OwnerArray)
				{
					owner = Users.FindUserByUsername(ownerName);
					if(owner != null)
					{
						roles = Roles.GetRoles(owner.UserID, true);

						// Iterate through roles for current user
						foreach(Role role in roles)
						{
							if(!distinctRoles.Contains(role))
							{
								distinctRoles.Add(role);
							}
						}
					}
				}

				//Check quotas for all owner roles
				foreach(Role role in distinctRoles)
				{
					//Check if this role has any quota defined
					if(Exists(role.RoleID))
					{
						roleQuota = GetRoleQuota(role.RoleID, roleQuotas);

						if(quotaType==GalleryQuotaType.Disk)
						{
							if(roleQuota.DiskQuota > quota && roleQuota.DiskQuota > 0)
							{
								quota = roleQuota.DiskQuota;
							}
						}
						else
						{
							if(roleQuota.ImageQuota > quota && roleQuota.ImageQuota > 0)
							{
								quota = roleQuota.ImageQuota;
							}
						}
					}
				}
			}

			return quota;
		}
	}
}
