package com.smcpartners.shape.usecases.change_password;

import com.smcpartners.shape.crosscutting.security.RequestScopedUserId;
import com.smcpartners.shape.frameworks.data.dao.shape.UserDAO;
import com.smcpartners.shape.shared.dto.common.BooleanValueDTO;
import com.smcpartners.shape.shared.dto.shape.request.CreateUserRequestDTO;
import com.smcpartners.shape.shared.utils.SecurityUtils;
import com.smcpartners.shape.shared.usecasecommon.UseCaseException;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Responsible:</br>
 * 1. Request a password change. The user must provide the old password and a new password.</br>
 * <p>
 * Created by johndestefano on 3/15/16.
 * </p>
 * <p>
 * Changes:</br>
 * 1. </br>
 * </p>
 */
@RequestScoped
public class ChangePasswordServiceAdapter implements ChangePasswordService {
    @Inject
    private Logger log;

    @EJB
    private UserDAO dao;

    @Inject
    private RequestScopedUserId requestScopedUserId;

    /**
     * Constructor
     */
    public ChangePasswordServiceAdapter() {
    }

    @Override
    public BooleanValueDTO changeUserPassword(CreateUserRequestDTO user) throws UseCaseException {
        try {

            String userId = user.getId();
            String userPwd = user.getPassword();
            String newPwd = user.getNewPassword();

            //check new password validity
            boolean validPassword = SecurityUtils.checkPasswordCompliance(newPwd);
            //check if valid user
            if (dao.validateUser(userId, userPwd) != null) {
                //check if valid password with correct regex
                if (validPassword) {
                    dao.changePassword(userId, userPwd, newPwd);
                    dao.resetPasswordToggle(userId, false);
                    if (user.getQuestionOne() != null && user.getQuestionTwo() != null) {
                        dao.addUserSecurityQuestions(userId, user.getQuestionOne(),
                                user.getQuestionTwo(), user.getAnswerOne(), user.getAnswerTwo());
                    }
                }else{
                    throw new UseCaseException("The new password you entered did not meet the required format.");
                }
            }else{
                throw new UseCaseException("Your current password is not correct.");
            }
            //returns true if new password uses correct regex and is a valid user
            return new BooleanValueDTO(true);
        } catch (Exception e) {
            log.logp(Level.SEVERE, this.getClass().getName(), "changeUserPassword", e.getMessage(), e);
            throw new UseCaseException(e.getMessage());
        }
    }
}
@@ -322,24 +322,79 @@
 RequestDTO;%0A
+import com.smcpartners.shape.shared.dto.shape.UserDTO;%0A
 import com.s
@@ -1653,20 +1653,47 @@
         
-if (
+UserDTO userDataFromDatabase = 
 dao.vali
@@ -1717,16 +1717,54 @@
 userPwd)
+;%0A            if (userDataFromDatabase
  != null
@@ -1861,24 +1861,25 @@
 Password) %7B%0A
+%0A
             
@@ -1890,298 +1890,477 @@
     
-dao.changePassword(userId, userPwd, newPwd);%0A                    dao.resetPasswordToggle(userId, false);%0A                    if (user.getQuestionOne() != null && user.getQuestionTwo() != null) %7B%0A                        dao.addUserSecurityQuestions(userId, user.getQuestionOne(),%0A           
+// check if a correct security question and answer were sent%0A                    // receives as question 1 and answer 1 but could be either%0A                    // (question 1 and answer 1) or (question two and answer two)%0A                    if (%0A                            user.getQuestionOne().equals( userDataFromDatabase.getQuestionOne() ) &&%0A                            user.getAnswerOne().equals( userDataFromDatabase.getAnswerOne() )%0A                        %7C%7C%0A
     
@@ -2368,32 +2368,39 @@
                 
+       
 user.getQuestion
@@ -2403,55 +2403,435 @@
 tion
-Two(), user.getAnswerOne(), user.getAnswerTwo()
+One().equals( userDataFromDatabase.getQuestionTwo() ) &&%0A                            user.getAnswerOne().equals( userDataFromDatabase.getAnswerTwo() )%0A                    ) %7B%0A                        dao.changePassword(userId, userPwd, newPwd);%0A                        dao.resetPasswordToggle(userId, false);%0A                    %7D else %7B%0A                        throw new UseCaseException(%22Security question or answer incorrect%22
 );%0A 
@@ -2843,32 +2843,33 @@
               %7D%0A
+%0A
                 
@@ -3462,8 +3462,9 @@
 %0A    %7D%0A%7D
+%0A

require security question when changing password