/// <summary>
 /// Reece Maas 
 /// Created: 2015/02/18
 /// Adds a single Supplier to the database
 /// Throws any exceptions caught by the DAL
 /// </summary>
 /// <remarks>
 /// Matt Lapka 
 /// Updated:  2015/03/27
 /// Added supplier cache
 /// </remarks>
 /// <param name="supplierToAdd">Supplier object containing the information of the supplier to be added</param>
 /// <param name="userName">The username to be given to the Supplier</param>
 /// <returns>An enumerated result depicting pass or fail</returns>
 public SupplierResult AddANewSupplier(Supplier supplierToAdd, string userName)
 {
     try
     {
         PasswordManager myPass = new PasswordManager();
         string password = myPass.supplierHash(userName, "Password#1");
         if (SupplierAccessor.AddSupplier(supplierToAdd, userName, password) == 2)
         {
             //refresh cache
             DataCache._currentSupplierList = SupplierAccessor.GetSupplierList();
             DataCache._SupplierListTime = DateTime.Now;
             return SupplierResult.Success;
         }
         return SupplierResult.NotAdded;
     }
     catch (ApplicationException ex)
     {
         return ex.Message == "Concurrency Violation" ? SupplierResult.ChangedByOtherUser : SupplierResult.DatabaseError;
     }
     catch (Exception ex)
     {
         throw ex;
         //return SupplierResult.DatabaseError;
     }
 }
        /// <summary>
        /// Matt Lapka
        /// Created 2015/04/30
        /// validates & sends updated password info to the BLL 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnChange_Click(object sender, EventArgs e)
        {
            //doublecheck client side validation
            if (txtNewPassword.Text.Trim() != txtNewPassword2.Text.Trim())
            {
                return;
            }
            if (!txtNewPassword.Text.Trim().ValidatePassword())
            {
                passRules.Visible = true;
                return;
            }
            passRules.Visible = false;
            PasswordManager myPass = new PasswordManager();
            string pass = myPass.supplierHash(_currentLogin.UserName, txtCurrent.Text);
            if (pass != _currentLogin.UserPassword)
            {

                lblError.Text = "Invalid Password";
                return;
            }
            lblError.Text = "";
            try
            {

                var result = _myMan.UpdateSupplierLogin(txtNewPassword.Text, _currentLogin);
                if (result == ResultsEdit.Success)
                {
                    _currentLogin.UserPassword = txtNewPassword.Text;
                    Session["login"] = _currentLogin;
                    //show alert!
                    Response.Redirect("~/portal");
                }
                else
                {
                    Label errorLabel = (Label)Master.FindControl("lblErrorMessage");
                    errorLabel.Text = "Error Updating Password. Please try again.";
                    Control c = Master.FindControl("ErrorMess");
                    c.Visible = true;

                    return;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
 /// <summary>
 /// Rose Steffensmeier
 /// Created:  2015/04/03
 /// Retrieves supplier login by username and password
 /// </summary>
 /// <param name="userPassword">The supplier's password to check against</param>
 /// <param name="userName">The supplier's username to check against</param>
 /// <returns>SupplierLogin object whose username and password match those of the parameters passed</returns>
 public SupplierLogin RetrieveSupplierLogin(string userPassword, string userName)
 {
     try
     {
         PasswordManager myPass = new PasswordManager();
         userPassword = myPass.supplierHash(userName, userPassword);
         return _access.RetrieveSupplierLogin(userPassword, userName);
     }
     catch (SqlException)
     {
         throw;
     }
     catch (Exception)
     {
         throw;
     }
 }
        /// <summary>
        /// Pat Banks
        /// Created:  2015/04/11
        /// Returns the result of approving a supplier application and adds records to the Supplier Table and SupplierLogin tables
        /// </summary>
        /// <param name="oldSupplierApp">The SupplierApplication object to be updated</param>
        /// <param name="updatedSupplierApp">The SupplierApplication object with the updated information</param>
        /// <param name="userName">The username of the Supplier</param>
        /// <param name="supplyCost">The supplier's portion of ticket proceeds</param>
        /// <returns>An enumerated result depicting pass or fail</returns>
        public SupplierResult ApproveSupplierApplication(SupplierApplication oldSupplierApp, SupplierApplication updatedSupplierApp, string userName, decimal supplyCost)
        {
            try
            {
                PasswordManager myPass = new PasswordManager();
                string password = myPass.supplierHash(userName, "Password#1");
                //Approving
                //update db with approval, add supplier record, add supplier login
                int numRows = SupplierApplicationAccessor.UpdateSupplierApplication(oldSupplierApp, updatedSupplierApp, userName, supplyCost, password);

                if (numRows == 3)
                {
                    //refresh cache
                    DataCache._currentSupplierList = SupplierAccessor.GetSupplierList();
                    DataCache._SupplierListTime = DateTime.Now;
                    return SupplierResult.Success;
                }
                return SupplierResult.ChangedByOtherUser;
            }
            catch (SqlException)
            {
                throw;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// Rose Steffensmeier
        /// Created:  2015/04/03
        /// Updates a supplierLogin information with a new password
        /// </summary>
        /// <param name="newPassword">The string representation of the new password requested</param>
        /// <param name="oldLogin">The SupplierLogin object to have the password updated for</param>
        /// <returns>An enumerated result depicting pass or fail</returns>
        public ResultsEdit UpdateSupplierLogin(string newPassword, SupplierLogin oldLogin)
        {
            try
            {
                //checks if user name already exits.. returns false if it does
                bool result1 = CheckSupplierUserName(oldLogin.UserName);

                if (!result1)
                {
                    PasswordManager myPass = new PasswordManager();
                    //oldLogin.UserPassword = myPass.supplierHash(oldLogin.UserName, oldLogin.UserPassword);
                    newPassword= myPass.supplierHash(oldLogin.UserName, newPassword);
                    int result = _access.UpdateSupplierPassword(newPassword, oldLogin);

                    return result == 1 ? ResultsEdit.Success : ResultsEdit.ChangedByOtherUser;
                }
                return ResultsEdit.DatabaseError;
            }
            catch (SqlException)
            {
                return ResultsEdit.DatabaseError;
            }
            catch (Exception)
            {
                return ResultsEdit.DatabaseError;
            }
        }