using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Threading;
using System.Threading.Tasks;
using Aitex.Core.Util;
using MECF.Framework.Common.Account.Extends;
using MECF.Framework.Common.Account.Permissions;
using MECF.Framework.Common.CommonData;

namespace Aitex.Core.Account
{
    [ServiceKnownType(typeof(CredentialKeepAliveStates))]
    [ServiceKnownType(typeof(CredentialState))]
    [ServiceKnownType(typeof(LoginRequestResults))]
    [ServiceKnownType(typeof(MenuPermissionEnum))]
    [ServiceKnownType(typeof(NotifiableItem))]
    [ServiceKnownType(typeof(Role))]
    [ServiceKnownType(typeof(List<Role>))]
    [ServiceKnownType(typeof(AppMenu))]
    [ServiceKnownType(typeof(List<AppMenu>))]
    [ServiceKnownType(typeof(Credential))]
    [ServiceKnownType(typeof(LoginClientInfo))]
    [ServiceKnownType(typeof(AccountEx))]
    [ServiceKnownType(typeof(LoginResult))]
    [ServiceKnownType(typeof(CredentialKeepAliveCheckResult))]
    [ServiceKnownType(typeof(CancellationTokenSource))]
    [ServiceContract]
    public interface IAccountService
    {
        #region Common Operations

        /// <summary>
        /// 注册视图。
        /// </summary>
        /// <param name="views"></param>
        [OperationContract]
        void RegisterViews(List<string> views);


        /// <summary>
        /// 获取所有视图。
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        SerializableDictionary<string, string> GetAllViewList();

        /// <summary>
        /// TBD
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        string GetProcessViewPermission();

        /// <summary>
        /// TBD
        /// </summary>
        /// <param name="viewXML"></param>
        /// <returns></returns>
        [OperationContract]
        bool SaveProcessViewPermission(string viewXML);

        /// <summary>
        /// 获取系统支持的所有菜单。
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        List<AppMenu> GetAllMenus();

        #endregion

        #region Permission Operations

        /// <summary>
        /// 获取指定角色ID和菜单名称的访问权限。
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="menuName">菜单名称</param>
        /// <returns></returns>
        [OperationContract]
        MenuPermissionEnum GetMenuPermission(string roleId, string menuName);

        /// <summary>
        /// 保存所有角色的权限。
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [OperationContract]
        bool SaveAllRolesPermission(Dictionary<string, Dictionary<string, ViewPermission>> data);

        #endregion

        #region Role Operations

        /// <summary>
        ///获取包含超级角色的角色列表。
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        IEnumerable<Role> GetRolesIncludeSuper();

        /// <summary>
        /// 获取不包含超级角色的角色列表。
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        List<Role> GetRoles();

        /// <summary>
        /// 获取指定ID的角色。
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [OperationContract]
        Role GetRoleByID(string id);

        /// <summary>
        /// 获取指定名称的角色。
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [OperationContract]
        Role GetRoleByName(string name);

        /// <summary>
        /// 更新指定的角色。
        /// </summary>
        /// <param name="role">角色信息</param>
        /// <returns></returns>
        [OperationContract]
        bool UpdateRole(Role role);

        /// <summary>
        /// 删除指定的角色。
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns></returns>
        [OperationContract]
        bool DeleteRole(string roleId);

        /// <summary>
        /// 获取指定角色的可访问菜单列表。
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [OperationContract]
        List<AppMenu> GetAccessibleMenusByRole(string roleId);

        /// <summary>
        /// 获取一个可用的角色ID。
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        string GetAvailableRoleID();

        /// <summary>
        /// 检查指定名称的角色是否存在；如果存在，它们的ID是否相同，如果ID不同，则表示当前角色名称已存在，不允许保存。
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [OperationContract]
        bool CheckRoleNotDuplicated(string id, string name);

        #endregion

        #region Account Operations

        /// <summary>
        /// 获取指定账号信息。
        /// </summary>
        /// <param name="accountId">账号ID</param>
        /// <returns></returns>
        [OperationContract]
        GetAccountInfoResult GetAccountInfo(string accountId);

        /// <summary>
        /// 获取包含超级账号的列表。
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        List<AccountEx> GetAccountsIncludeSuper();

        /// <summary>
        /// 获取账号列表。
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        List<AccountEx> GetAccounts();
        
        /// <summary>
        /// 创建账号。
        /// </summary>
        /// <param name="newAccount">新账号</param>
        /// <returns></returns>
        [OperationContract]
        CreateAccountResult CreateAccount(Account newAccount);

        /// <summary>
        /// 删除账号。
        /// </summary>
        /// <param name="accountId">账号ID</param>
        /// <returns></returns>
        [OperationContract]
        DeleteAccountResult DeleteAccount(string accountId);

        /// <summary>
        /// 更新指定的账号。
        /// </summary>
        /// <param name="account">账号信息</param>
        /// <returns></returns>
        [OperationContract]
        UpdateAccountResult UpdateAccount(Account account);

        /// <summary>
        /// 更新指定的账号。
        /// </summary>
        /// <param name="account">账号</param>
        /// <returns></returns>
        [OperationContract]
        bool UpdateAccountEx(AccountEx account);

        /// <summary>
        /// 删除指定的账号。
        /// </summary>
        /// <param name="loginName">账号名</param>
        /// <returns></returns>
        [OperationContract]
        bool DeleteAccountEx(string loginName);

        /// <summary>
        /// 修改账号密码。
        /// </summary>
        /// <param name="accountId">账号ID</param>
        /// <param name="newPassword">新密码</param>
        /// <returns></returns>
        [OperationContract]
        ChangePwdResult ChangePassword(string accountId, string newPassword);

        /// <summary>
        /// 校验指定账户的密码。
        /// </summary>
        /// <param name="accountId"></param>
        /// <param name="passwordMD5"></param>
        /// <returns></returns>
        [OperationContract]
        bool CheckPassword(string accountId, string passwordMD5);

        /// <summary>
        /// 获取登录用户列表。
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        List<Account> GetLoginUsers();

        /// <summary>
        /// 踢出指定的账号。
        /// </summary>
        /// <param name="accountId">账号ID</param>
        /// <param name="reason">踢出原因</param>
        [OperationContract]
        void KickUserOut(string accountId, string reason);

        #endregion

        #region Login/Logout Operations

        /// <summary>
        /// 登录RT。
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="roleID">角色ID</param>
        /// <param name="clientInfo">登录客户端信息</param>
        /// <returns></returns>
        [OperationContract]
        Task<LoginResult> LoginEx(string userName, string password, string roleID,
            LoginClientInfo clientInfo);

        /// <summary>
        /// 注销登录。
        /// </summary>
        /// <param name="myToken">当前登录的令牌</param>
        [OperationContract]
        void LogoutEx(Guid myToken);

        /// <summary>
        /// 取消登录请求。
        /// </summary>
        /// <param name="userName"></param>
        [OperationContract]
        void CancelLoginRequest(string userName);

        /// <summary>
        /// 确认异地登录请求。
        /// </summary>
        /// <param name="userName">正在请求登录的令牌</param>
        [OperationContract]
        void ConfirmLoginRequest(string userName);

        /// <summary>
        /// 拒绝异地登录请求。
        /// </summary>
        /// <param name="userName">正在请求登录的令牌</param>
        [OperationContract]
        void RejectLoginRequest(string userName);

        /// <summary>
        /// 激活凭据。
        /// </summary>
        /// <param name="myToken">当前登录的令牌</param>
        /// <returns></returns>
        [OperationContract]
        CredentialKeepAliveCheckResult KeepAlive(Guid myToken);

        #endregion
    }
}
