package controller.auth;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import view.auth.RegisterPanel;
import controller.Controller;
import model.ViewConstraints;

public class RegisterController implements ActionListener{
	private Controller mainController;
	
	public RegisterController(Controller mainController) {		
		this.mainController = mainController;
	}
		
	private void registerToServer(String userName, String email, String password, String rePassword){
		System.out.println("I try to register "+ userName +" " + email+ " "+ password + " "+ rePassword);
		mainController.getServerHandler().register(userName, password, email);
	}
	private boolean isPasswordEligible(String pass){
		return (!pass.equals(pass.toLowerCase()) && !pass.equals(pass.toUpperCase()) && pass.length()>=6 && pass.length() <=  12 && pass.matches(".*\\d+.*"));
	}
	public void actionPerformed(ActionEvent e) {
		JButton source = (JButton) e.getSource();
		RegisterPanel registerPanel = (RegisterPanel) source.getParent();
		switch (source.getText()) {
		case ViewConstraints.REGISTER_BUTTON_TEXT:
			if(isPasswordEligible(registerPanel.getPassword())){
				registerToServer(registerPanel.getUsername(), registerPanel.getEmail(), registerPanel.getPassword(), registerPanel.getRePassword());
			}else{
				JOptionPane.showMessageDialog(new JFrame("Error"),"Password has to contain at least one uppercase character, one lowercase character, a number, and has to be between 6 and 12 characters.");
			}
			break;
		case ViewConstraints.LOGIN_BUTTON_TEXT:
			mainController.loadRightPanel("LOGIN");
			break;		
		}
		
	}
	
}

@@ -797,16 +797,31 @@
 ing pass
+, String rePass
 )%7B%0A%09%09ret
@@ -860,17 +860,21 @@
 se()) &&
- 
+%0A%09%09%09%09
 !pass.eq
@@ -905,78 +905,973 @@
  && 
-pass.length()%3E=6 && pass.length() %3C=  12 && pass.matches(%22.*%5C%5Cd+.*%22));
+ //Check if it has lower and uppercase characters too%0A%09%09%09%09pass.length()%3E=6 && pass.length() %3C=  12 && //Check if it's between 6 and 12 characters long%0A%09%09%09%09pass.matches(%22.*%5C%5Cd+.*%22) && //Check if it contains number%0A%09%09%09%09pass.matches(%22%5C%5Cw+%22) && //Check if it contains only numbers, english characters or underscore%0A%09%09%09%09pass.equals(rePass)); //&& //Check if it is equal to the password the user gave the second time%0A%09%09%09%09//pass.matches(%22%5Ba-z%5B0-9%5D%5D%22));  //Contains only english characters OR numbers%0A%09%7D%0A%09private boolean isEmailEligible(String email)%7B%0A%09%09String tmpStr0%5B%5D = email.split(%22@%22);%0A%09%09String tmpStr1%5B%5D = tmpStr0%5B1%5D.split(%22%5C%5C.%22);%0A%09%09System.out.println(tmpStr0.length+%22 %22+tmpStr1.length+tmpStr0%5B0%5D);%0A%09%09return (tmpStr0%5B0%5D.matches(%22%5C%5Cw+%22) && tmpStr1%5B0%5D.matches(%22%5C%5Cw+%22) && tmpStr1%5B1%5D.matches(%22%5C%5Cw+%22) && //Contains only english characters OR numbers%0A%09%09%09%09tmpStr0.length == 2 && tmpStr1.length == 2); //Check if the format of the password is eligible (name@domain.suffix)%09  
 %0A%09%7D%0A
@@ -2155,16 +2155,91 @@
 ssword()
+,registerPanel.getRePassword()) && isEmailEligible(registerPanel.getEmail()
 ))%7B%0A%09%09%09%09
@@ -2445,16 +2445,72 @@
 assword 
+can contain only english characters, numbers and _.%5CnIt 
 has to c
@@ -2524,17 +2524,20 @@
 at least
- 
+: %5Cn
 one uppe
@@ -2552,17 +2552,18 @@
 aracter,
- 
+%5Cn
 one lowe
@@ -2578,17 +2578,18 @@
 aracter,
- 
+%5Cn
 a number
@@ -2589,17 +2589,18 @@
  number,
- 
+%5Cn
 and has 
@@ -2633,16 +2633,60 @@
 racters.
+%5CnMake sure you gave a valid e-mail address!
 %22);%0A%09%09%09%7D

Add client register input validation. Solves #13