package com.infinite.focus.server.auth;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.http.HttpStatus;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.server.ResponseStatusException;

public class ChangePasswordRequest {

	private String oldPassword;
	private String newPassword;
	
	public String getOldPassword() {
		return oldPassword;
	}
	
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	
	public String getNewPassword() {
		return newPassword;
	}
	
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
	
	public boolean validatePassword(BCryptPasswordEncoder bCryptPasswordEncoder, String password) throws ResponseStatusException{
		
    	  
    	if(oldPassword == null) {
        	throw new ResponseStatusException(HttpStatus.CONFLICT, "The old password does not found.");
        }
    	
    	if(oldPassword.trim().isEmpty()) {
    		throw new ResponseStatusException(HttpStatus.CONFLICT, "The old password is empty.");
        }
    	
    	if(newPassword == null) {
    		throw new ResponseStatusException(HttpStatus.CONFLICT, "The new password does not found.");
        }
    	
    	if(newPassword.trim().isEmpty()) {
    		throw new ResponseStatusException(HttpStatus.CONFLICT, "The new password is empty.");
        }
    	
    	isValidPassword(newPassword);
    	
    	/*if (!newPassword.matches("[A-Za-z0-9]+")) {
    		throw new ResponseStatusException(HttpStatus.CONFLICT, "The new password should not contain spacial characters.");
        } 
    	
    	if (newPassword.length() < 6) {
    		throw new ResponseStatusException(HttpStatus.CONFLICT, "The new password should be six character long.");
        }*/
    	
    	boolean isValidPassword = bCryptPasswordEncoder.matches(oldPassword, password);

    	if(!isValidPassword) {
    		throw new ResponseStatusException(HttpStatus.CONFLICT, "The old password does not match.");
    	}
    	
    	return isValidPassword;
	}
	
	public static boolean isValidPassword(String password) {
        // Regex to check valid password.
       /* String regex = "(?=.*[a-z])(?=.*[A-Z])"
                + "(?=.*[@#$%^&+=])"
                + "(?=\\S+$).{6,25}$";*/
        
       // String regex = "^(?=.*[A-Z])(?=.*[a-z])(?=.*[$@$!%*#?&^-])[A-Za-z\\d$@$!%*#?&^-]{6,25}$";

		String regex = "^(?=.*[A-Z])(?=.*[a-z])(?=.*[@$!%*?&#^()\\[\\]<>;:.,~\\-_=\\/|{}\"'`+\\\\])[A-Za-z\\d@$!%*?&#^()\\[\\]<>;:.,~\\-_=\\/|{}\"'`+\\\\]{6,25}$";
		
        // Compile the ReGex
        Pattern p = Pattern.compile(regex);

        // If the password is empty
        // return false
        if (password == null || password.isEmpty()) {
            return false;
        }

        // Pattern class contains matcher() method
        // to find matching between given password
        // and regular expression.
        Matcher m = p.matcher(password);

        // Return if the password
        // matched the ReGex
        boolean isValid = m.matches();
        
        if(!isValid) {
        	throw new ResponseStatusException(HttpStatus.CONFLICT, "The password must contain 6-25 characters, including at least 1 upper case, lower case alphabets and a special character.");
        }
        
        return isValid;
    }

}

