using System;
using System.Collections.Generic;
using MECF.Framework.Common.Account.Permissions;

namespace MECF.Framework.Common.Account.Extends
{
	
	/// <summary>
	/// 角色对象。Sic系统中对于各个操作权限以“角色”为单位进行设置。
	/// </summary>
	[Serializable]
	public class Role
	{
		#region Variables

		private string _roleId;
		private string _roleName;
		private bool _autoLogout;
		private int _logoutTime;
		private string _permissionContent;
		private string _description;

        #endregion

        #region Constructors

		/// <summary>
		/// 构造角色的对象实例。
		/// </summary>
		/// <param name="id">角色ID</param>
		/// <param name="name">角色名称</param>
		/// <param name="isAutoLogout">是否启用超时自动注销功能</param>
		/// <param name="autoLogoutTime">自动注销的超时时间，单位秒</param>
		/// <param name="permString">角色权限字符串</param>
		/// <param name="isBuildIn">当前账户是否为系统保留账户。</param>
		/// <param name="description">角色描述</param>
		private Role(string id, string name, bool isAutoLogout, int autoLogoutTime, string permString, bool isBuildIn = false, string description = "")
		{
			RoleId = id;
			_roleName = name;
			_autoLogout = isAutoLogout;
			_logoutTime = autoLogoutTime;
			_description = description;
			_permissionContent = permString;
			IsSuper = false;
			IsBuildIn = isBuildIn;
		}
		
		/// <summary>
		/// 构造角色的对象实例。
		/// </summary>
		/// <param name="id">角色ID</param>
		/// <param name="name">角色名称</param>
		/// <param name="isAutoLogout">是否启用超时自动注销功能</param>
		/// <param name="autoLogoutTime">自动注销的超时时间，单位秒</param>
		/// <param name="permItemList">角色权限列表</param>
		/// <param name="isBuildIn">当前账户是否为系统保留账户。</param>
		/// <param name="description">角色描述</param>
		public Role(string id, string name, bool isAutoLogout, int autoLogoutTime, Dictionary<string, MenuPermissionEnum> permItemList, bool isBuildIn = false, string description = "")
		{
			RoleId = id;
			_roleName = name;
			_autoLogout = isAutoLogout;
			_logoutTime = autoLogoutTime;
			_description = description;
			PermissionControlItems = permItemList ?? new Dictionary<string, MenuPermissionEnum>();
			IsSuper = false;
			IsBuildIn = isBuildIn;
		}

		#endregion

		#region Properties

		/// <summary>
		/// 设置或返回角色ID。
		/// </summary>
		public string RoleId
		{
			get => _roleId;
			set => _roleId = value;
		}

		/// <summary>
		/// 设置或返回角色名称。
		/// </summary>
		public string RoleName
		{
			get => _roleName;
			set => _roleName = value;
		}

		/// <summary>
		/// 设置或返回是否启用超时自动注销功能。
		/// </summary>
		public bool IsAutoLogout
		{
			get => _autoLogout;
			set => _autoLogout = value;
		}

		/// <summary>
		/// 设置或返回自动注销的超时时间，单位秒。
		/// </summary>
		public int LogoutTime
		{
			get => _logoutTime;
			set => _logoutTime = value;
		}

		/// <summary>
		/// 设置或返回描述当前角色权限的字串。
		/// </summary>
		public string PermissionContent
		{
			get => _permissionContent;
			set => _permissionContent = value;
		}

		/// <summary>
		/// 权限控制项列表。
		/// </summary>
		public Dictionary<string, MenuPermissionEnum> PermissionControlItems { get; }
		
		/// <summary>
		/// 设置或返回当前角色描述。
		/// </summary>
		public string Description
		{
			get => _description;
			set => _description = value;
		}

		/// <summary>
		/// 设置或返回当前角色是否为超级用户。
		/// </summary>
		public bool IsSuper { get; set; }
		
		/// <summary>
		/// 返回当前账户是否为系统保留账户。
		/// </summary>
		public bool IsBuildIn { get; set; }

		#endregion

		#region Methods

		/// <summary>
		/// 获取指定项目的权限。
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		public virtual MenuPermissionEnum GetPermission(string name)
		{
			if (PermissionControlItems.TryGetValue(name, out var permissionEnum))
				return permissionEnum;

			return MenuPermissionEnum.MP_NONE;
		}

		/// <inheritdoc />
		public override string ToString()
		{
			return $"ID = {RoleId}, Name = {RoleName}, Permission Items = {PermissionControlItems.Count}";
		}

		#endregion
	}
}
