package retail;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.*;

import database_comm.UserDatabase;
import login.SecurityHelper;
import login.User;

public class AddUserFrame extends JFrame {
	
	private String first;
	private String last;
	private String username;
	private String password;
	private String password2;
	private boolean admin = false;
	private String error;
	private JFrame User;
	ArrayList<String> push = new ArrayList<String>(4);
	
	public AddUserFrame() {
	    User = new JFrame("Add User");
		User.setResizable(false);
		User.setTitle("Add User");
		User.setSize(400, 225);
		User.setLocationRelativeTo(null);
		
		Panel greeting = new Panel();
		JTextArea greet = new JTextArea("Please enter the following information");
		greet.setBackground(null);
		greet.setEditable(false);
		greeting.add(greet, BorderLayout.CENTER);
		
		Panel information = new Panel();
		information.setLayout(new GridLayout(6,2));
		
		JTextArea firstName = new JTextArea("  First Name:");
		firstName.setEditable(false);
		firstName.setBackground(null);
		JTextField fName = new JTextField();
		fName.setColumns(7);
		information.add(firstName);
		information.add(fName);
		
		JTextArea lastName = new JTextArea("  Last Name:");
		lastName.setEditable(false);
		lastName.setBackground(null);
		JTextField lName = new JTextField();
		lName.setColumns(7);
		information.add(lastName);
		information.add(lName);
		
		JTextArea USERNAME = new JTextArea("  Create Username:");
		USERNAME.setEditable(false);
		USERNAME.setBackground(null);
		JTextField userID = new JTextField();
		userID.setColumns(7);
		information.add(USERNAME);
		information.add(userID);
		
		JTextArea PASSWORD = new JTextArea("  Create Password:");
		PASSWORD.setEditable(false);
		PASSWORD.setBackground(null);
		JPasswordField pass = new JPasswordField();
		pass.setColumns(7);
		information.add(PASSWORD);
		information.add(pass);
		
		JTextArea PASSWORD1 = new JTextArea("  Re-Enter Password:");
		PASSWORD1.setEditable(false);
		PASSWORD1.setBackground(null);
		JPasswordField pass2 = new JPasswordField();
		pass2.setColumns(7);
		information.add(PASSWORD1);
		information.add(pass2);
		
		JCheckBox ADMIN = new JCheckBox("Are you an Admin?");
		information.add(ADMIN);
		
		Panel buttons = new Panel();
		buttons.setLayout(new GridLayout(1,2));
		
		JButton exit = new JButton("Exit");
		buttons.add(exit);
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				User.dispose();
			}
		});
		
		JButton enter = new JButton("Create User");
		buttons.add(enter);
		enter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				error = "";
				

				if(ADMIN.isSelected()) {
					admin = true;
				}
				first = fName.getText();
				last = lName.getText();
				username = userID.getText();
				password = new String(pass.getPassword());
				password2 = new String(pass2.getPassword());
				
				vData();
				
			}
		});
		
		Panel combo = new Panel();
		combo.setLayout(new BorderLayout());
		combo.add(greeting, BorderLayout.NORTH);
		combo.add(information, BorderLayout.CENTER);
		combo.add(buttons, BorderLayout.SOUTH);
		
		User.add(combo);
		User.setVisible(true);
		User.setAlwaysOnTop(true);
		
	}

	public void pushUser(String FN, String LN, String userID, String pass, boolean admin) {
		// TODO Auto-generated method stub
		
		User addNew = new User(userID);
		addNew.setFirstName(FN);
		addNew.setLastName(LN);
		addNew.setAdmin(admin);
		addNew.setPaswordHash(SecurityHelper.generatePasswordHash(pass));
		try {
			UserDatabase.addUser(addNew);
			JOptionPane.showMessageDialog(null, "User Successfully Added");
			User.dispose();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	void vData() {
		
		User.setAlwaysOnTop(false);

		validFN();
		validLN();
		validUser();
		validPass();

		if (error.isEmpty()) {
			pushUser(first, last, username, password, admin);
		}
		else {
			error = error.substring(0, error.length() - 3);
			JOptionPane.showMessageDialog(null, "Invalid Entry: " + error);
		}

		error = "";
		User.setAlwaysOnTop(true);
		
	}
	
	void validFN() {
		if (first.length() < 2) {
			error = error + "Invalid First Name | ";
		}
	}
	
	void validLN() {
		if (last.length() < 2) {
			error = error + "Invalid Last Name | ";
		}
	}
	
	void validUser() {
		if(username.length() <= 2) {
			
			error = error + "Invalid Username | ";
		}
	}
	
	void validPass() {
		if(password.length() < 4) {
			error = error + "Invalid Password | ";
		} else {
			if(password.equals(password2)) {
				
			} else {
				error = error + "Passwords do not match | ";
			}
		}
	}

}

@@ -4611,32 +4611,368 @@
 ername %7C %22;%0A%09%09%7D%0A
+%09%09else%0A%09%09%7B%0A%09%09%09try %7B%0A%09%09%09%09String%5B%5D allUsernames = UserDatabase.getAllUsernames();%0A%09%09%09%09for(int i = 0; i %3C allUsernames.length; i++) %7B%0A%09%09%09%09%09if (allUsernames%5Bi%5D.toLowerCase().equals(username.toLowerCase())) %7B%0A%09%09%09%09%09%09error = error + %22Username already exists %7C %22;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D catch (SQLException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A
 %09%7D%0A%09%0A%09void valid

Add check for if username already exiss on create user