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

using System;

namespace CommunityServer.Components
{
	/// <summary>
	/// Provides core validation and data access to creating and managing Permissions
	/// </summary>
	public class Permissions
	{
		private Permissions()
		{
			//
			// TODO: Add constructor logic here
			//
		}

        /// <summary>
        /// Validates a user's access to a specic section
        /// </summary>
        public static bool ValidatePermissions(Section section, Permission permission, User user)
        {
            return ValidatePermissions(section,permission,user,null);
        }

        /// <summary>
        /// Validates a user's access to a specic section
        /// </summary>
        public static bool ValidatePermissions(Section section, Permission permission, User user, Post p)
        {
            return section.ValidatePermissions(section,permission,user,p);
        }

        /// <summary>
        /// Validates a user's access to a specific section. If the validation fails, the result 
        /// will generally be an exception
        /// </summary>
        public static void AccessCheck (Section section, Permission permission, User user)
        {
            AccessCheck(section,permission,user,null);
        }       

        /// <summary>
        /// Validates a user's access to a specific section. If the validation fails, the result 
        /// will generally be an exception
        /// </summary>
        public static void AccessCheck (Section section, Permission permission, User user, Post post)
        {
            if(section == null)
                throw new CSException(CSExceptionType.SectionNotFound,"Unknown Section Requested");

            section.AccessCheck(section,permission,user,post);
        }

        /// <summary>
        /// Update an existing permission
        /// </summary>
        /// <param name="pb"></param>
        public static void UpdatePermission (PermissionBase pb) 
        {
            Validate(pb);
            // Create Instance of the IDataProvider
            CommonDataProvider dp = CommonDataProvider.Instance();
            dp.CreateUpdateDeletePermission(pb, DataProviderAction.Update);

        }

        /// <summary>
        /// Remove an existing permission
        /// </summary>
        /// <param name="p"></param>
        public static void RemovePermission (PermissionBase p) 
        {
            Validate(p);
            // Create Instance of the IDataProvider
            CommonDataProvider dp = CommonDataProvider.Instance();
            dp.CreateUpdateDeletePermission(p, DataProviderAction.Delete);

        }

        /// <summary>
        /// Add a new Permission
        /// </summary>
        /// <param name="p"></param>
        public static void AddPermission (PermissionBase p) 
        {
            Validate(p);
            CommonDataProvider dp = CommonDataProvider.Instance();
            dp.CreateUpdateDeletePermission(p, DataProviderAction.Create);
        }

        private static void Validate(PermissionBase p)
        {
            if(p.ApplicationType == ApplicationType.Unknown)
                throw new CSException(CSExceptionType.PermissionApplicationUnknown,"Permission has an Unknown ApplicationType");
        }
	}
}
