/*

Frontend page: Reset Password page
Title:  Update Password
Designed: Irfan Mam
User story: 
Developed: Irfan MAM
Date created: 1/17/2016

*/
        public bool resetPassword(int userId, ResetPassword resetPasswordModel)
        {
            DataHandler dataHandler = new DataHandler();
            string newSalt = PasswordEncryption.RandomString();
            resetPasswordModel.Password = PasswordEncryption.encryptPassword(resetPasswordModel.Password, newSalt);

            List<object[]> paramertList = new List<object[]>();
            paramertList.Add(new object[] { "@user_id", userId });
            paramertList.Add(new object[] { "@password", resetPasswordModel.Password });
            

            try
            {
                return dataHandler.ExecuteSQLWithReturnVal("spUpdatePassword", paramertList) > 0 ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public ActionResult ResetPassword(ResetPassword resetPasswordModel)
        {

            // check the session, is exists allow otherwise don't
            int userId;
            try
            {
                userId = int.Parse(Session["forgotId"].ToString());
            }
            catch (Exception)
            {
                return new HttpStatusCodeResult(404);
            }

            // reset the password
            bool isSuccess = (new forgotPasswordTokenAccess()).resetPassword(userId, resetPasswordModel);


            // if failed to reset, return error
            if (!isSuccess)
            {
                return new HttpStatusCodeResult(404);
            }

            
            ViewBag.message = "Your Password Successfully Updated"; // pass success message

            // return the same page
            return RedirectToAction("ResetPassword", new { message = ViewBag.message });
        }
        public ActionResult ChangePassword(ResetPassword resetPasswordModel)
        {
            int userId = int.Parse(Session["editId"].ToString());
            bool isSuccess = (new forgotPasswordTokenAccess()).resetPassword(userId, resetPasswordModel);

            if (!isSuccess)
            {
                return RedirectToAction("UserLogin", "Login");
            }

            ViewBag.message = "Password Sucessfully Changed";

            return PartialView();
        }