public ActionResult Create(Contact model)
        {
            int id = WebSecurity.GetUserId(WebSecurity.CurrentUserName);
            var userProfile = _userContext.UserProfiles.First(x => x.UserId == id);

            if (string.IsNullOrWhiteSpace(userProfile.PrivateKey) || string.IsNullOrWhiteSpace(userProfile.PublicKey))
            {
                TempData["Notification"] = new Notification("Please provide access keys that have been sent you by email", Nature.warning);
                return RedirectToAction("Account", "Settings");
            }

            if (ModelState.IsValid)
            {
                UserData userData = new UserData();
                userData.PublicKey = userProfile.PublicKey;
                userData.Timestamp = DateTime.Now;
                userData.GenerateAuthenticationHash(userProfile.PrivateKey + userProfile.PublicKey + "POST/contact"+ userData.Timestamp + userProfile.PrivateKey);

                ContactEndpoint c = new ContactEndpoint();
                string message = c.CreateContact(model, userData);

                TempData["Notification"] = new Notification("Contact has been added" + message, Nature.success);
                Thread.Sleep(2500);
                return RedirectToAction("Index");

            } else
            {
                return View(model);
            }
        }
        public ActionResult Login(LoginViewModel model)
        {
            bool modelValid = true;
            if (string.IsNullOrWhiteSpace(model.UserName))
            {
                ModelState.AddModelError("UserName", "Please provide user name");
                modelValid = false;
            }

            if (string.IsNullOrWhiteSpace(model.Password))
            {
                ModelState.AddModelError("Password", "Please provide password");
                modelValid = false;
            }

            if (!modelValid)
                return View(model);

            try
            {
                bool authenticated = WebSecurity.Login(model.UserName, model.Password);
                if (authenticated)
                {
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    TempData["Notification"] = new Notification("Incorrect username/password pair.", Nature.warning);
                    return View();
                }
            }
            catch (Exception ex)
            {
                ViewBag.Notification = new Notification("Error occured when processing your request", Nature.danger);
                return View("Error");
            }
        }
        public ActionResult SignUp(SignUpViewModel model)
        {
            if (model.Password != model.ConfirmPassword)
            {
                ModelState.AddModelError("ConfirmPassword", "Passwords don't match");
                return View(model);
            }
            if (ModelState.IsValid)
            {
                try
                {
                    UserEndpoint ue = new UserEndpoint();
                    ue.Register(model.UserName);
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { PrivateKey = string.Empty, PublicKey = string.Empty });
                    TempData["Notification"] = new Notification("Please check your e-mail, we sent you access keys.", Nature.success);

                    return RedirectToAction("Login");
                }
                catch (Exception ex)
                {
                    return View("Error");
                }
            }
            else
            {
                return View(model);
            }
        }
        public ActionResult Settings(SettingsViewModel model)
        {
            try
            {

                if (!string.IsNullOrWhiteSpace(model.OldPassword) &&
                    !string.IsNullOrWhiteSpace(model.NewPassword) &&
                    !string.IsNullOrWhiteSpace(model.ConfirmNewPassword))
                {

                    if (model.NewPassword == model.ConfirmNewPassword)
                    {
                        if (!WebSecurity.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword))
                        {
                            ModelState.AddModelError("OldPassword", "Incorrect password");
                            return View(model);
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("NewPassword", "");
                        ModelState.AddModelError("ConfirmNewPassword", "Passwords don't match");
                        return View(model);
                    }

                }
                int id = WebSecurity.GetUserId(WebSecurity.CurrentUserName);
                UserProfile profile = _context.UserProfiles.First(x => x.UserId == id);
                profile.PublicKey = model.PublicKey;
                profile.PrivateKey = model.PrivateKey;
                _context.SaveChanges();

            }
            catch (Exception ex)
            {

                return View("Error");
            }

            TempData["Notification"] = new Notification("Changes have been saved successfuly.", Nature.success);
            return View(model);
        }
        public ActionResult Delete(string id)
        {
            int userId = WebSecurity.GetUserId(WebSecurity.CurrentUserName);
            var userProfile = _userContext.UserProfiles.First(x => x.UserId == userId);

            UserData userData = new UserData();
            userData.PublicKey = userProfile.PublicKey;
            userData.Timestamp = DateTime.Now;
            userData.GenerateAuthenticationHash(userProfile.PrivateKey + userProfile.PublicKey + "DELETE/contact/" + id + userData.Timestamp + userProfile.PrivateKey);

            ContactEndpoint c = new ContactEndpoint();

            string message = c.DeleteContact(id, userData);

            TempData["Notification"] = new Notification("Contact has been removed" + message, Nature.success);

            return RedirectToAction("Index");
        }
        public ActionResult Index(string searchQuery, string searchScope, int? pageNumber, int pageSize = 12)
        {
            int id = WebSecurity.GetUserId(WebSecurity.CurrentUserName);
            var userProfile = _userContext.UserProfiles.First(x => x.UserId == id);
            searchScope = "all";
            if (string.IsNullOrWhiteSpace(userProfile.PrivateKey) || string.IsNullOrWhiteSpace(userProfile.PublicKey))
            {
                TempData["Notification"] = new Notification("Please provide access keys that have been sent you by email", Nature.warning);
                return RedirectToAction("Settings", "Account");
            }

            pageNumber = pageNumber ?? 1;

            ContactEndpoint c = new ContactEndpoint();
            UserData userData = new UserData();
            userData.PublicKey = userProfile.PublicKey;
            userData.Timestamp = DateTime.Now;

            List<Contact> result;
            if (string.IsNullOrWhiteSpace(searchQuery) || searchScope == null)
            {
                userData.GenerateAuthenticationHash(userProfile.PrivateKey + userProfile.PublicKey + "GET/contact/" + pageNumber.Value + "/" + pageSize+"/false" + userData.Timestamp + userProfile.PrivateKey);
                result = c.GetContacts(pageNumber.Value, pageSize, userData);

            } else
            {
                userData.GenerateAuthenticationHash(userProfile.PrivateKey + userProfile.PublicKey + "GET/contact/"+searchScope+"/"+searchQuery+"/" + pageNumber.Value + "/" + pageSize+"/false" + userData.Timestamp + userProfile.PrivateKey);
                result = c.GetFilteredContacts(searchScope, searchQuery, pageNumber.Value, pageSize, userData);
            }

            ViewBag.SearchQuery = searchQuery;
            return View(result);
        }
        public ActionResult Edit(ContactViewModel model)
        {
            int userId = WebSecurity.GetUserId(WebSecurity.CurrentUserName);
            var userProfile = _userContext.UserProfiles.First(x => x.UserId == userId);
            model.contact.isContactGroup = false;

            UserData userData = new UserData();
            userData.PublicKey = userProfile.PublicKey;
            userData.Timestamp = DateTime.Now;
            userData.GenerateAuthenticationHash(userProfile.PrivateKey + userProfile.PublicKey + "POST/contact/" + model.contact._id + userData.Timestamp + userProfile.PrivateKey);

            ContactEndpoint c = new ContactEndpoint();
            string message = c.UpdateContact(model.contact, userData);

            TempData["Notification"] = new Notification("Contact has been modified" + message, Nature.success);
            Thread.Sleep(2500);

            return RedirectToAction("Index");
        }