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

namespace MECF.Framework.UI.Core.Accounts
{
	public class AccountServiceClient : IAccountService
	{
		#region Constructors

		public AccountServiceClient()
		{
		}

		#endregion

		#region Accounts Operations

		public GetAccountInfoResult GetAccountInfo(string accountId)
		{
			GetAccountInfoResult result = null;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.GetAccountInfo(accountId);
			});
			return result;
		}
		
		public List<AccountEx> GetAccountsIncludeSuper()
		{
			var result = new List<AccountEx>();
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.GetAccountsIncludeSuper();
			});
			return result;
		}

		public List<AccountEx> GetAccounts()
		{
			var result = new List<AccountEx>();
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.GetAccounts();
			});
			return result;
		}

		public ChangePwdResult ChangePassword(string accountId, string newPassword)
		{
			ChangePwdResult result = null;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.ChangePassword(accountId, newPassword);
			});
			return result;
		}

		public bool CheckPassword(string accountId, string passwordMD5)
		{
			var result = false;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.CheckPassword(accountId, passwordMD5);
			});
			return result;
		}

		public CreateAccountResult CreateAccount(Account newAccount)
		{
			CreateAccountResult result = null;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.CreateAccount(newAccount);
			});
			return result;
		}

		public DeleteAccountResult DeleteAccount(string accountId)
		{
			DeleteAccountResult result = null;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.DeleteAccount(accountId);
			});
			return result;
		}

		public UpdateAccountResult UpdateAccount(Account account)
		{
			UpdateAccountResult result = null;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.UpdateAccount(account);
			});
			return result;
		}
		
		public bool UpdateAccountEx(AccountEx account)
		{
			var result = false;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.UpdateAccountEx(account);
			});
			return result;
		}

		public bool DeleteAccountEx(string accountId)
		{
			var result = false;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.DeleteAccountEx(accountId);
			});
			return result;
		}
		
		#endregion

		#region Login Sessions Operations

		public List<Account> GetLoginUsers()
		{
			List<Account> result = null;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.GetLoginUsers();
			});
			return result;
		}

		public void KickUserOut(string accountId, string kickoutReason)
		{
			WCFProxy.Using(delegate(IAccountService svc)
			{
				svc.KickUserOut(accountId, kickoutReason);
			});
		}

		public async Task<LoginResult> LoginEx(string userName, string password, string role,
			LoginClientInfo clientInfo)
		{
			LoginResult result = default;
			await WCFProxy.AsyncUsing(async delegate(IAccountService svc)
			{
				result = await svc.LoginEx(userName, password, role, clientInfo);
			});
			
			return result;
		}

		public void CancelLoginRequest(string userName)
		{
			WCFProxy.Using(delegate(IAccountService svc)
			{
				svc.CancelLoginRequest(userName);
			});
		}

		public void ConfirmLoginRequest(string userName)
		{
			WCFProxy.Using(delegate(IAccountService svc)
			{
				svc.ConfirmLoginRequest(userName);
			});
		}
		
		public void RejectLoginRequest(string userName)
		{
			WCFProxy.Using(delegate(IAccountService svc)
			{
				svc.RejectLoginRequest(userName);
			});
		}

		public void LogoutEx(Guid myToken)
		{
			WCFProxy.Using(delegate(IAccountService svc)
			{
				svc.LogoutEx(myToken);
			});
		}

		public CredentialKeepAliveCheckResult KeepAlive(Guid myToken)
		{
			var result = new CredentialKeepAliveCheckResult();
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.KeepAlive(myToken);
			});
			return result;
		}
		
		#endregion

		#region Roles Operations

		public IEnumerable<Role> GetRolesIncludeSuper()
		{
			IEnumerable<Role> result = null;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.GetRolesIncludeSuper();
			});
			return result;
		}
		
		public List<Role> GetRoles()
		{
			var result = new List<Role>();
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.GetRoles();
			});
			return result;
		}
		
		public bool UpdateRole(Role role)
		{
			var result = false;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.UpdateRole(role);
			});
			return result;
		}

		public bool DeleteRole(string roleId)
		{
			var result = false;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.DeleteRole(roleId);
			});
			return result;
		}

		/// <inheritdoc />
		public string GetAvailableRoleID()
		{
			var result = "";
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.GetAvailableRoleID();
			});
			return result;
		}

		public Role GetRoleByID(string id)
		{
			Role result = null;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.GetRoleByID(id);
			});
			return result;
		}
		
		public Role GetRoleByName(string name)
		{
			Role result = null;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.GetRoleByName(name);
			});
			return result;
		}

		public bool CheckRoleNotDuplicated(string id, string name)
		{
			var result = false;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.CheckRoleNotDuplicated(id, name);
			});
			return result;
		}
		
		#endregion

		#region Common Operations
		
		public List<AppMenu> GetAllMenus()
		{
			var result = new List<AppMenu>();
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.GetAllMenus();
			});
			return result;
		}


		public void RegisterViews(List<string> views)
		{
			WCFProxy.Using(delegate(IAccountService svc)
			{
				svc.RegisterViews(views);
			});
		}
		
		public SerializableDictionary<string, string> GetAllViewList()
		{
			SerializableDictionary<string, string> result = null;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.GetAllViewList();
			});
			return result;
		}
		
		public bool SaveProcessViewPermission(string viewXML)
		{
			var result = false;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.SaveProcessViewPermission(viewXML);
			});
			return result;
		}

		public string GetProcessViewPermission()
		{
			string result = null;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.GetProcessViewPermission();
			});
			return result;
		}

		#endregion

		#region Permission Operations
		
		public bool SaveAllRolesPermission(Dictionary<string, Dictionary<string, ViewPermission>> data)
		{
			var result = false;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.SaveAllRolesPermission(data);
			});
			return result;
		}

		public List<AppMenu> GetAccessibleMenusByRole(string roleid)
		{
			var result = new List<AppMenu>();
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.GetAccessibleMenusByRole(roleid);
			});
			return result;
		}

		public MenuPermissionEnum GetMenuPermission(string roleId, string menuName)
		{
			var result = MenuPermissionEnum.MP_NONE;
			WCFProxy.Using(delegate(IAccountService svc)
			{
				result = svc.GetMenuPermission(roleId, menuName);
			});
			return result;
		}
		
		#endregion

	}
}
