package views;
import auth.DeleteUser;

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

import javax.swing.*; 
import java.sql.SQLException;

public class DeleteUserView extends View{
	private JComboBox choose_method_deletion; 
	private JTextField get_response; 
	public DeleteUserView(String env_type) {
		super(env_type);
	}
	public void display_graphical() {
		JFrame jf = new JFrame("Delete User");
		JPanel jp = new JPanel(); 
		jp.setLayout(new BoxLayout(jp, BoxLayout.Y_AXIS));
		
		String[] methods_deletion = {"Id", "Username"};
		choose_method_deletion = new JComboBox(methods_deletion); 
		
		get_response = new JTextField(); 
		
		JButton submit = new JButton("Submit");
		submit.addActionListener(new DeleteUserSubmitListener());
		jp.add(choose_method_deletion);
		jp.add(get_response);
		jp.add(submit);
		
		jf.add(jp);
		jf.setSize(300, 300);
		jf.setVisible(true);
	}
	
	public class DeleteUserSubmitListener implements ActionListener{

		public void actionPerformed(ActionEvent arg0){
			DeleteUser du = null;
			try {
				du = new DeleteUser();
			} catch (ClassNotFoundException e1) {
				JOptionPane.showMessageDialog(null, "Sorry, an internal error occurred. POSReady will now exit this component.");
				return;
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(null, "Sorry, an internal error occurred. POSReady will now exit this component.");
				return;
			}
			String method_of_deletion = (String) choose_method_deletion.getSelectedItem();
			if (method_of_deletion.equals("Id")) {
				int id_del = 0; 
				try {
					id_del = Integer.parseInt(get_response.getText());
				}
				catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(null, "Sorry, but your input was invalid. \n REASON: The id specified was not a valid integer.");
					return;
				}
				
				try {
					du.delete(id_del);
				} catch (ClassNotFoundException e) {
					JOptionPane.showMessageDialog(null, "Sorry, an internal error occurred. POSReady will now exit this component.");
					return;
				} catch (SQLException e) {
					JOptionPane.showMessageDialog(null, "Sorry, an internal error occurred. POSReady will now exit this component.");
					return;
				} 
			}
			else if (method_of_deletion.equals("Username")) {
				String username_del = get_response.getText(); 
				if (username_del.contains(" ")) {
					JOptionPane.showMessageDialog(null, "Sorry, but your input was invalid. \nREASON: Your input contained spaces");
				}
				try {
					du.delete(username_del);
				} catch (ClassNotFoundException e) {
					JOptionPane.showMessageDialog(null, "Sorry, an internal error occurred. POSReady will now exit this component.");
					return;
				} catch (SQLException e) {
					JOptionPane.showMessageDialog(null, "Sorry, an internal error occurred. POSReady will now exit this component.");
					return; 
				}
			}
			
			JOptionPane.showMessageDialog(null, "The user was deleted successfully.");
			
		}
	
	}
}

@@ -971,16 +971,17 @@
 Listener
+ 
 %7B%0A%0A%09%09pub
@@ -2965,10 +2965,329 @@
 %09%7D%0A%09%0A%09%7D%0A
+%09%0A%09public class DeleteUserFormValidator impelements Runnable%7B%0A%09%09public void run()%7B%0A%09%09%09String response = get_response.getText();%0A%09%09%09String method_deletion = get_method_deletetion.getSelectedItem().toString(); %0A%09%09%09if (method_deletion.equals(%22Id%22) && response.matches(%22%5E-?%5C%5Cd+$%22))%7B%0A%09%09%09%09%09%0A%09%09%09%09%0A%09%09%09%7D%0A%09%09%09%09%0A%09%09%7D%0A%09%09%0A%09%09%0A%09%7D%0A%09%0A%09%0A%09%0A
 %7D%0A

update deleteuserview with concurrent form validations