public Tuple <bool, string> Logout(string sname, User user = null)
        {
            Logger.logEvent(this, System.Reflection.MethodBase.GetCurrentMethod());
            Tuple <bool, string> ans = check_args(sname);

            if (!ans.Item1)
            {
                return(ans);
            }
            if (user is null)
            {
                user = GetAtiveUser(sname);
            }
            if (user == null)
            {
                return(new Tuple <bool, string>(false, sname + "is not Logged in\n"));
            }
            if (!user.LoggedStatus())
            {
                return(new Tuple <bool, string>(false, sname + "is not Logged in\n"));
            }
            if (user.isguest())
            {
                return(new Tuple <bool, string>(false, "Guest cannot Log out.\n"));
            }
            user.Logout();
            Active_users.Remove(user.getUserName());
            //Change LogInStatus at DB
            DbManager.Instance.UpdateUserLogInStatus(user.getUserName(), true);
            addGuest(false);
            return(new Tuple <bool, string>(true, sname + " Logged out succesuffly\n"));
        }
        //Login to Unlogged Register User with valid user name and pass.
        public Tuple <bool, string> Login(string username, string pass, bool isGuest = false)
        {
            Logger.logSensitive(this, System.Reflection.MethodBase.GetCurrentMethod());
            if (isGuest)
            {
                string Uname = addGuest();
                return(new Tuple <bool, string>(true, Uname));
                //No need to Insert Guest to DB
            }
            Tuple <bool, string> ans = check_args(username, pass);

            if (!ans.Item1)
            {
                return(ans);
            }
            string sha1 = SB.CalcSha1(pass);
            string temp_pass_hash;

            if (!Users_And_Hashes.TryGetValue(username, out temp_pass_hash))
            {
                return(new Tuple <bool, string>(false, "No such User: "******"\n"));
            }
            if (Users_And_Hashes.ContainsKey(username) && temp_pass_hash == sha1)
            {
                User tUser;
                if (!users.TryGetValue(username, out tUser))
                {
                    return(new Tuple <bool, string>(false, "Error occured User is not in the users_DB but is registered: " + username + " \n"));
                }
                if (tUser.LoggedStatus())
                {
                    return(new Tuple <bool, string>(false, "The user: "******" is already logged in\n"));
                }
                tUser.LogIn();
                //Update LogginStatus
                Statistics.Instance.InserRecord(username, DateTime.Now);
                DbManager.Instance.UpdateUserLogInStatus(tUser.getUserName(), false);
                Active_users.Add(tUser.getUserName(), tUser);
                if (tUser.HasPendingMessages())
                {
                    List <NotifyData> messages = tUser.GetPendingMessages();
                    foreach (NotifyData msg in messages)
                    {
                        //Remove From DB Message
                        DbNotifyData dbMsg = DbManager.Instance.GetDbNotification(username, msg.Context);
                        DbManager.Instance.DeleteSingleMessage(dbMsg);
                        //Try to send the message and if not recieved it will be enetred to DB Again inside Notify
                        Publisher.Instance.Notify(tUser.getUserName(), msg);
                        //tUser.RemovePendingMessage(msg);
                    }
                    tUser.RemoveAllPendingMessages();
                }
                DbManager.Instance.SaveChanges();
                return(new Tuple <bool, string>(true, username + " Logged int\n"));
            }
            DbManager.Instance.SaveChanges();
            return(new Tuple <bool, string>(false, "Wrong Credentials\n"));
        }
        //Tries to get user from logged in users.
        public virtual User GetAtiveUser(string username)
        {
            Logger.logEvent(this, System.Reflection.MethodBase.GetCurrentMethod());
            if (username is null)
            {
                Logger.logError(CommonStr.ArgsTypes.None, this, System.Reflection.MethodBase.GetCurrentMethod());
                return(null);
            }
            User tUser;

            if (Active_users.TryGetValue(username, out tUser))
            {
                return(tUser);
            }
            return(null);
        }
        //Add Guest user to the system and to the relevant lists.
        private string addGuest(bool islogout = true)
        {
            lock (this)
            {
                //Add Guest DO not Require DB Changes
                Logger.logEvent(this, System.Reflection.MethodBase.GetCurrentMethod());
                string tName = "Guest" + Available_ID;
                User   nUser = new User(Available_ID, tName);
                Console.WriteLine(tName);
                nUser.LogIn();
                Active_users.Add(tName, nUser);
                if (islogout)
                {
                    Statistics.Instance.InserRecord(tName, DateTime.Now);
                }
                Available_ID++;

                return(tName);
            }
        }
 public void Rtoactive(User u)
 {
     Active_users.Remove(u.getUserName());
     users.Add(u.getUserName(), u);
 }
 //Function for Tests only add to active user (simulate login)
 //And Remove from active users (Simulate logout).
 public void addtoactive(User u)
 {
     Active_users.Add(u.getUserName(), u);
     users.Add(u.getUserName(), u);
 }