/// <summary>
 /// Used to delete token based on user id
 /// </summary>
 /// <param name="userId"></param>
 /// <returns></returns>
 public bool DeleteTokensByUserId(int userId)
 {
     try
     {
         using (var tde = new TimeDifferenceEntities())
         {
             var tokens = tde.Tokens.Where(m => m.UserId == userId);
             tde.Tokens.RemoveRange(tokens);
             tde.SaveChanges();
             return true;
         }
     }
     catch (Exception ex)
     {
         return false;
     }
 }
 /// <summary>
 /// Used to delete Token
 /// </summary>
 /// <param name="token"></param>
 /// <returns></returns>
 public bool DeleteToken(string token)
 {
     try
     {
         using (var tde = new TimeDifferenceEntities())
         {
             var tokenInfo = tde.Tokens.FirstOrDefault(m => m.AuthToken == token);
             if (tokenInfo == null)
                 return false;
             tde.Tokens.Remove(tokenInfo);
             tde.SaveChanges();
             return true;
         }
     }
     catch (Exception ex)
     {
         return false;
     }
 }
        /// <summary>
        /// Used to delete timeZone entry
        /// </summary>
        /// <param name="timeZoneEntryId"></param>
        /// <returns></returns>
        public bool DeleteTimeZoneEntry(int timeZoneEntryId)
        {
            try
            {
                using (var tde = new TimeDifferenceEntities())
                {
                    var entry = tde.TimeZoneEntries.FirstOrDefault(m => m.Id == timeZoneEntryId);
                    if (entry == null)
                        return false;

                    entry.IsActive = false;
                    tde.SaveChanges();
                    return true;
                }

            }
            catch (Exception ex)
            {
                throw new Exception("Exception Occured", ex.InnerException);
            }
        }
        /// <summary>
        /// Used to edit a timeZone Entry
        /// </summary>
        /// <returns></returns>
        public int ModifyTimeZoneEntry(TimeZoneEntry entry)
        {
            try
            {
                using (var tde = new TimeDifferenceEntities())
                {
                    var timeZoneEntry =
                        tde.TimeZoneEntries.FirstOrDefault(m => m.IsActive && m.Id == entry.Id);

                    if (timeZoneEntry == null)
                        return -1;
                    timeZoneEntry.City = entry.City;
                    timeZoneEntry.Difference = entry.Difference;
                    timeZoneEntry.EntryName = entry.EntryName;
                    tde.SaveChanges();
                    return timeZoneEntry.Id;
                }

            }
            catch (Exception ex)
            {
                throw new Exception("Exception Occured", ex.InnerException);
            }
        }
        /// <summary>
        /// Used to save a timeZone Entry
        /// </summary>
        /// <returns></returns>
        public int SaveNewTimeZoneEntry(TimeZoneEntry entry)
        {
            try
            {
                using (var tde = new TimeDifferenceEntities())
                {
                    tde.TimeZoneEntries.Add(entry);
                    tde.SaveChanges();
                    return entry.Id;
                }

            }
            catch (Exception ex)
            {
                throw new Exception("Exception Occured", ex.InnerException);
            }
        }
        /// <summary>
        /// Used to remove user from the database
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool RemoveUser(int userId)
        {
            try
            {

                using (var tde = new TimeDifferenceEntities())
                {

                    var userInfo = tde.Users.FirstOrDefault(m => m.IsActive && m.Id == userId);
                    if (userInfo == null)
                        return false;
                    userInfo.IsActive = false;
                    tde.SaveChanges();
                    return true;

                }
            }
            catch (Exception ex)
            {
                throw new Exception("Exception Occured", ex.InnerException);
            }
        }
        /// <summary>
        /// Used to register User
        /// </summary>
        /// <param name="registrationModel"></param>
        /// <returns></returns>
        public int RegisterUser(RegistrationModel registrationModel)
        {
            try
            {

                using (var tde = new TimeDifferenceEntities())
                {
                    var userData = new User
                    {
                        Email = registrationModel.Email,
                        UserName = registrationModel.UserName,
                        Password = registrationModel.Password,
                        RoleId = Convert.ToInt32(registrationModel.RoleId),
                        IsActive = true

                    };
                    tde.Users.Add(userData);
                    tde.SaveChanges();
                    return userData.Id;

                }
            }
            catch (Exception ex)
            {
                throw new Exception("Exception Occured", ex.InnerException);
            }
        }
        /// <summary>
        /// Used to modify user Information
        /// </summary>
        /// <returns></returns>
        public bool ModifyUserInformationAdmin(UserModel userInformation)
        {
            try
            {

                using (var tde = new TimeDifferenceEntities())
                {

                    var userInfo = tde.Users.FirstOrDefault(m => m.IsActive && m.Id == userInformation.UserId);
                    if (userInfo == null)
                        return false;
                    userInfo.Email = userInformation.Email;
                    userInfo.UserName = userInformation.UserName;
                    userInfo.RoleId = Convert.ToInt32(userInformation.Role);
                    if (!string.IsNullOrEmpty(userInformation.Password))
                    {
                        userInfo.Password = userInformation.Password;
                    }
                    tde.SaveChanges();
                    return true;

                }
            }
            catch (Exception ex)
            {
                throw new Exception("Exception Occured", ex.InnerException);
            }
        }
        /// <summary>
        /// Used to update the token information
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public bool UpdateToken(Token token)
        {
            try
            {
                using (var tde = new TimeDifferenceEntities())
                {
                    var tokenInfo = tde.Tokens.FirstOrDefault(m => m.AuthToken == token.AuthToken);
                    if (tokenInfo == null)
                        return false;

                    tokenInfo.AuthToken = token.AuthToken;
                    tokenInfo.ExpiresOn = token.ExpiresOn;
                    tokenInfo.IssuedOn = token.IssuedOn;
                    tokenInfo.UserId = token.UserId;
                    tde.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
 /// <summary>
 /// Used to add token in the database
 /// </summary>
 /// <returns></returns>
 public bool InsertToken(Token token)
 {
     try
     {
         using (var tde = new TimeDifferenceEntities())
         {
             tde.Tokens.Add(new Token
             {
                 AuthToken = token.AuthToken,
                 ExpiresOn = token.ExpiresOn,
                 IssuedOn = token.IssuedOn,
                 UserId = token.UserId,
             });
             tde.SaveChanges();
             return true;
         }
     }
     catch (Exception ex)
     {
         return false;
     }
 }