package ecse321.fall2014.group3.bomberman;

import javax.swing.JButton;
import java.util.Arrays;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.Semaphore;

import ecse321.fall2014.group3.bomberman.database.Login;

import ecse321.fall2014.group3.bomberman.database.Session;
import org.spout.renderer.lwjgl.LWJGLUtil;

public class App {
    private static final Semaphore loginWait = new Semaphore(0);

    public static void main(String[] args) {
        LWJGLUtil.deployNatives(null);

       // final Connection connection = Login.openDB();
        final Session session = new Session();
        final JFrame frame = createLoginScreen(session);

        loginWait.acquireUninterruptibly();

        frame.dispose();
        try {
            session.disconnect();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        new Game().open();
    }

    private static JFrame createLoginScreen(final Session session) {

        final JFrame frame = new JFrame("Login");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JPanel panel = new JPanel(new GridLayout(6, 2));
        frame.add(panel, BorderLayout.CENTER);

        JLabel realNameLabel = new JLabel("Real Name");
        panel.add(realNameLabel);

        final JTextField realNameText = new JTextField(10);
        panel.add(realNameText);

        JLabel userLabel = new JLabel("User");
        panel.add(userLabel);

        final JTextField userText = new JTextField(10);
        panel.add(userText);

        JLabel passwordLabel = new JLabel("Password");
        panel.add(passwordLabel);

        final JPasswordField passwordText = new JPasswordField(10);
        panel.add(passwordText);

        JLabel verifyLabel = new JLabel("Verify Password");
        panel.add(verifyLabel);

        final JPasswordField verifyText = new JPasswordField(10);
        panel.add(verifyText);

        JButton loginButton = new JButton("login");
        panel.add(loginButton);

        JButton createButton = new JButton("New");
        panel.add(createButton);

        final JLabel error = new JLabel("user already exists or missing password");
        error.setForeground(Color.RED);
        error.setVisible(false);
        panel.add(error);

        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if (!Arrays.equals(passwordText.getPassword(), verifyText.getPassword())) {
                    passwordText.setText("");
                    verifyText.setText("");
                    error.setText("passwords dont match");
                    error.setVisible(true);
                } else {
                    //TODO: add real name to database
                    //if(Login.login(realNameText.getText(),userText.getText(), String.valueOf(passwordText.getPassword()), connection))
                    if (Login.login(userText.getText(), String.valueOf(passwordText.getPassword()), session)) {
                        loginWait.release();
                    } else {
                        passwordText.setText("");
                        verifyText.setText("");
                        error.setText("wrong username or password");
                        error.setVisible(true);
                    }
                }
            }
        });

        createButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!Arrays.equals(passwordText.getPassword(), verifyText.getPassword())) {
                    passwordText.setText("");
                    verifyText.setText("");
                    error.setText("passwords dont match");
                    error.setVisible(true);
                } else {
                    //TODO: add real name to database
                    //if(Login.login(realNameText.getText(),userText.getText(), String.valueOf(passwordText.getPassword()), connection))
                    if (Login.createAccount(userText.getText(), String.valueOf(passwordText.getPassword()), session)) {
                        loginWait.release();
                    } else {
                        realNameText.setText("");
                        userText.setText("");
                        passwordText.setText("");
                        verifyText.setText("");
                        error.setText("user already exist");
                        error.setVisible(true);
                    }
                }
            }
        });

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        return frame;
    }
}

@@ -69,33 +69,8 @@
 on;%0A
-import java.util.Arrays;%0A
 impo
@@ -418,22 +418,24 @@
 ava.sql.
-Connec
+SQLExcep
 tion;%0Aim
@@ -444,32 +444,27 @@
 rt java.
-sql.SQLException
+util.Arrays
 ;%0Aimport
@@ -554,17 +554,16 @@
 .Login;%0A
-%0A
 import e
@@ -609,24 +609,25 @@
 se.Session;%0A
+%0A
 import org.s
@@ -2323,32 +2323,91 @@
 dd(loginButton);
+%0A        panel.getRootPane().setDefaultButton(loginButton);
 %0A%0A        JButto

Set login button as default button