/// <summary>
        /// Mapping user with UserViewModel
        /// </summary>
        /// <param name="userEntity"></param>
        /// <returns></returns>
        private static UserViewModel GetUserModel(User userEntity, HasanEntities database)
        {
            if (userEntity != null)
            {
                UserViewModel model = new UserViewModel();
                model.Id = userEntity.Id;
                model.gKey = userEntity.gKey;
                model.UserTypeRef = userEntity.UserTypeRef;
                model.UserTypeName = userEntity.UserType.Name;
                model.GroupCompanyRef = userEntity.GroupCompanyRef;

                model.EMail = userEntity.EMail;
                model.Password = userEntity.Password;
                model.PIN = userEntity.PIN;
                model.Name = userEntity.Name;
                model.Surname = userEntity.Surname;
                model.ChangePasswordAtNextLogon = userEntity.ChangePasswordAtNextLogon;
                model.MailNotification = userEntity.MailNotification;
                model.Note = userEntity.Note;
                model.Sort = userEntity.Sort;
                model.Active = userEntity.Active;
                model.Deleted = userEntity.Deleted;
                return model;
            }
            else
                return null;
        }
        /// <summary>
        /// Create new user
        /// </summary>
        /// <param name="model"></param>
        /// <param name="database"></param>
        /// <returns></returns>
        public static FocusConstants.FocusResultCode CreateUser(UserViewModel model, HasanEntities database)
        {
            try
            {

                if (DoesUserNameExist(model.EMail, database))
                    return FocusConstants.FocusResultCode.DuplicateUser;

                User entity = new User();
                entity.UserTypeRef = model.UserTypeRef;
                entity.GroupCompanyRef = model.GroupCompanyRef;
                entity.CustomerRef = model.CustomerRef;
                entity.FactoryRef = model.FactoryRef;
                entity.PersonRef = model.PersonRef;
                entity.EmployeeRef = model.EmployeeRef;
                entity.EMail = model.EMail;
                entity.Password = model.Password;
                entity.PIN = model.PIN;
                entity.Name = model.Name;
                entity.Surname = model.Surname;
                entity.ChangePasswordAtNextLogon = model.ChangePasswordAtNextLogon;
                entity.MailNotification = model.MailNotification;
                entity.Note = model.Note;
                entity.Sort = model.Sort;
                entity.Active = model.Active;
                entity.Deleted = false;
                database.User.Add(entity);
                database.SaveChanges();
            }
            catch (Exception ex)
            {
                Logger logger = LogManager.GetCurrentClassLogger();
                logger.ErrorException("CreateUserType", ex);
                return FocusConstants.FocusResultCode.Exception;
            }
            return FocusConstants.FocusResultCode.Success;
        }
        private static bool DoLoginBygkey(string gkey, HasanEntities database, out UserViewModel userModel)
        {
            userModel = new UserViewModel();
            string gkeyToken="";//= AESCriptography.AesDecryption(gkey);
            Guid usergkey = new Guid(gkeyToken);

            var user = database.User.Where(it => it.gKey == usergkey && !it.Deleted).SingleOrDefault();
            if (user != null)
            {
                userModel.Id = user.Id;
                userModel.Password = user.Password;
                userModel.EMail = user.EMail;
                userModel.Name = user.Name;
                userModel.Surname = user.Surname;
                userModel.gKey = user.gKey;
                userModel.aKey = user.aKey;
                userModel.ChangePasswordAtNextLogon = user.ChangePasswordAtNextLogon;

                UserProfileSessionDTO sessionUser = new UserProfileSessionDTO();
                sessionUser.EMail = user.EMail;
                sessionUser.UserId = user.Id;
                sessionUser.FullName = user.Name + " " + user.Surname;
                HttpContext.Current.Session["UserSession"] = sessionUser;
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// Mapping userdata to UserViewModel
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="loadClass"></param>
        /// <returns></returns>
        private static UserViewModel GetUserDTO(User entity, bool loadClass)
        {
            try
            {
                if (entity != null)
                {
                    UserViewModel model = new UserViewModel();
                    model.Id = entity.Id;
                    model.gKey = entity.gKey;
                    model.EMail = entity.EMail;
                    model.Name = entity.Name;
                    model.Note = entity.Note;
                    model.Sort = entity.Sort;
                    model.Active = entity.Active;
                    model.Deleted = entity.Deleted;
                    return model;
                }
                else
                    return null;
            }
            catch (Exception ex)
            {
                Logger logger = LogManager.GetCurrentClassLogger();
                logger.ErrorException("GetUserDTO", ex);
                throw ex;

            }
        }
        public static LoginResult UserLoginBygkey(string gkey, bool RememberMe, HasanEntities database, out UserViewModel userModel)
        {
            LoginResult res = new LoginResult();
            userModel = new UserViewModel();
            try
            {

                if (DoLoginBygkey(gkey, database, out userModel))
                {

                    res.ResponseCode = FocusConstants.FocusResultCode.Success;
                    res.ResponseMessage = FocusMessage.GetResourceResultCodeValue(FocusConstants.FocusResultCode.Success);
                }
                return res;

            }
            catch (Exception ex)
            {
                Logger logger = LogManager.GetCurrentClassLogger();
                logger.ErrorException("UserLoginBygkey", ex);
                res.ResponseCode = FocusConstants.FocusResultCode.Exception;
                res.ResponseMessage = FocusMessage.GetResourceResultCodeValue(FocusConstants.FocusResultCode.Exception);
                return res;
            }
        }
        /// <summary>
        /// Login user & keep data into session
        /// </summary>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <param name="rememberMe"></param>
        /// <param name="database"></param>
        /// <param name="userModel"></param>
        /// <returns></returns>
        private static bool DoLogin(string email, string password, bool rememberMe, HasanEntities database, out UserViewModel userModel)
        {
            try
            {
                userModel = new UserViewModel();

                if (email == null || email.Trim() == "")
                    return false;
                var user = database.User.Where(it => it.EMail == email && it.Password == password && it.Active && !it.Deleted).SingleOrDefault();
                if (user != null)
                {
                    userModel.Id = user.Id;
                    userModel.Password = user.Password;
                    userModel.EMail = user.EMail;
                    userModel.Name = user.Name;
                    userModel.Surname = user.Surname;
                    userModel.gKey = user.gKey;
                    userModel.aKey = user.aKey;
                    userModel.ChangePasswordAtNextLogon = user.ChangePasswordAtNextLogon;

                    UserProfileSessionDTO sessionUser = new UserProfileSessionDTO();
                    sessionUser.EMail = user.EMail;
                    sessionUser.UserId = user.Id;
                    sessionUser.FullName = user.Name + " " + user.Surname;
                    sessionUser.isLocked = false;
                    HttpContext.Current.Session["UserSession"] = sessionUser;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                Logger logger = LogManager.GetCurrentClassLogger();
                logger.ErrorException("DoLogin", ex);
                throw ex;
            }
        }
        /// <summary>
        /// User login from controller
        /// </summary>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <param name="rememberMe"></param>
        /// <param name="database"></param>
        /// <param name="userModel"></param>
        /// <returns></returns>
        public static LoginResult UserLogin(string email, string password, bool rememberMe, HasanEntities database, out UserViewModel userModel)
        {
            userModel = new UserViewModel();

            LoginResult res = new LoginResult();
            try
            {
                User mem = database.User.Where(it => it.EMail == email && !it.Deleted).FirstOrDefault(); // get data for the user
                if (mem == null)
                {
                    res.ResponseCode = FocusConstants.FocusResultCode.MemberNotExist;
                    res.ResponseMessage = FocusMessage.GetResourceResultCodeValue(FocusConstants.FocusResultCode.MemberNotExist);
                    return res;
                }
                if (mem.Password != password) // check user password is valid or not
                {
                    res.ResponseCode = FocusConstants.FocusResultCode.InvalidPassword;
                    res.ResponseMessage = FocusMessage.GetResourceResultCodeValue(FocusConstants.FocusResultCode.InvalidPassword);
                    return res;
                }
                if (!mem.Active) // check user activess
                {
                    res.ResponseCode = FocusConstants.FocusResultCode.MemberIsNotActive;
                    res.ResponseMessage = FocusMessage.GetResourceResultCodeValue(FocusConstants.FocusResultCode.MemberIsNotActive);
                    return res;
                }

                if (DoLogin(email, password, rememberMe, database, out userModel))
                {
                    // var p = Enum.Parse(typeof(FocusConstants.FocusResultCode), FocusConstants.FocusResultCode.Success.ToString());
                    //res.ResponseCode = (FocusConstants)Enum.Parse(typeof(FocusConstants), FocusConstants.FocusResultCode.Success);
                     res.ResponseCode = FocusConstants.FocusResultCode.Success;

                    res.ResponseMessage = FocusMessage.GetResourceResultCodeValue(FocusConstants.FocusResultCode.Success);
                }

                return res;
            }
            catch (Exception ex)
            {
                Logger logger = LogManager.GetCurrentClassLogger();
                logger.ErrorException("UserLogin", ex);
                res.ResponseCode = FocusConstants.FocusResultCode.Exception;
                res.ResponseMessage = FocusMessage.GetResourceResultCodeValue(FocusConstants.FocusResultCode.Exception);
                return res;

            }
        }
        /// <summary>
        /// Update existing user
        /// </summary>
        /// <param name="model"></param>
        /// <param name="database"></param>
        /// <returns></returns>
        public static FocusConstants.FocusResultCode UpdateUser(UserViewModel model, HasanEntities database)
        {
            try
            {
                User entity = database.User.Where(it => it.Id == model.Id).SingleOrDefault();
                entity.UserTypeRef = model.UserTypeRef;
                entity.GroupCompanyRef = model.GroupCompanyRef;
                entity.CustomerRef = model.CustomerRef;
                entity.FactoryRef = model.FactoryRef;
                entity.PersonRef = model.PersonRef;
                entity.EmployeeRef = model.EmployeeRef;
                entity.EMail = model.EMail;
                entity.Password = model.Password;
                entity.PIN = model.PIN;
                entity.Name = model.Name;
                entity.Surname = model.Surname;
                entity.ChangePasswordAtNextLogon = model.ChangePasswordAtNextLogon;
                entity.MailNotification = model.MailNotification;
                entity.Note = model.Note;
                entity.Sort = model.Sort;
                entity.Active = model.Active;
                entity.Deleted = model.Deleted;
                database.SaveChanges();
            }
            catch (Exception ex)
            {
                Logger logger = LogManager.GetCurrentClassLogger();
                logger.ErrorException("UpdateUserType", ex);
                return FocusConstants.FocusResultCode.Exception;
            }

            return FocusConstants.FocusResultCode.Success;
        }
        public static bool HasUIPermission(int MenuId, Guid UserakeyId, HasanEntities database)
        {
            UserViewModel model = new UserViewModel();
            var user = database.User.Where(it => it.aKey == UserakeyId).SingleOrDefault();
            if (user != null)
            {
                model.UserTypeRef = user.UserType.Id;
                var UserTypePermission = database.UserTypePermission.Where(it => it.PermissionRef == MenuId && it.UserTypeRef == model.UserTypeRef).SingleOrDefault();

                if (UserTypePermission != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        public static List<MenuPrivilegeViewModel> GetAllMenuByUser(string UserId, HasanEntities database)
        {
            List<MenuPrivilegeViewModel> menuList = new List<MenuPrivilegeViewModel>();
            int UserRefId = Convert.ToInt32(UserId);
            UserViewModel model = new UserViewModel();
            var user = database.User.Where(it => it.Id == UserRefId).SingleOrDefault();
            model.UserTypeRef = user.UserType.Id;
            List<UserTypePermission> entityList = GetAllUserTypePermission(model.UserTypeRef, database);
            //var permi
            if (entityList != null && entityList.Count > 0)
            {
                foreach (UserTypePermission entity in entityList)
                {
                    menuList.Add(GetUserTypePermissionModel(entity));
                }
            }

            return menuList;
        }
 /// <summary>
 /// Delete user
 /// </summary>
 /// <param name="model"></param>
 /// <param name="database"></param>
 /// <returns></returns>
 public static FocusConstants.FocusResultCode DeleteUser(UserViewModel model, HasanEntities database)
 {
     User entity = database.User.Where(it => it.Id == model.Id).SingleOrDefault();
     entity.Deleted = true;
     database.SaveChanges();
     return FocusConstants.FocusResultCode.Success;
 }