package experiments;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.sql.*;

/**
 * Created by Bayar
 * Date: 07-Mar-2012
 * Time: 13:11:17
 * <p/>
 * Editted by Sam
 * 18/03/2012
 * 12:47
 */
public class LoginPanel extends BorderPanel {

    String user;
    String password;
    JLabel log = new JLabel("Username:");
    JTextField username = new JTextField(10);
    JPasswordField passwordField = new JPasswordField(10);
    JLabel pass = new JLabel("PASSWORD:");
    JButton processLogin = new JButton("Log me now");


    GridBagFrame parent;

    LoginPanel(GridBagFrame parent, String s) {
        /*
        Adding the components to the frame...
         */
        super(s);
        this.parent = parent;
        processLogin.addActionListener(new VerifyLogin());
        this.add(log);
        username.setMaximumSize(new Dimension(1000, 50));
        this.add(username, BorderLayout.CENTER);
        this.add(pass, BorderLayout.CENTER);
        passwordField.setMaximumSize(new Dimension(1000, 50));
        this.add(passwordField, BorderLayout.CENTER);
        this.add(processLogin, BorderLayout.SOUTH);
    }

    class VerifyLogin implements ActionListener {

        JFrame frame;

        public VerifyLogin() {
            // this.frame=frame;
        }

        /*
        Verifying user login details...
         */

        public void actionPerformed(ActionEvent e) {
            user = username.getText();
            password = passwordField.getText();
            Connection conn = null;
            boolean loggedIn = false;

            try {
                /*
                Creating connection with the database.
                 */
                conn = SimpleDataSource.getConnection();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            try {
                /*
                Creating a statement to query the database, returns the details of a user with the inputted Username and Password.
                 */
                PreparedStatement stat = conn.prepareStatement("SELECT * FROM Users WHERE Username=? AND Password=?", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
                stat.setString(1, user);
                stat.setString(2, password);
                ResultSet result = stat.executeQuery();
                System.out.println("Query sent");

                /*
                If there is such a user (if the table returns at least one result)...
                 */
                if (result.next()) {
                    passwordField.setText("");
                    loggedIn = true;
                    parent.theUser.updateUser(user);

                } else {
                    parent.panelHistory.welcome.setText("Incorrect username and/or password.");
                }
                conn.close();
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            } finally {
                try {
                    conn.close();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }

            /*
            If login was successful...
             */
            if (loggedIn) {
                System.out.println("logged in");
                CardLayout cl = (CardLayout) (parent.cards.getLayout());

                cl.show(parent.cards, "Graph Panel");
                parent.panelHistory.showFiles(parent.theUser.getHistory());
                parent.panelLoginRegister.loggedIn();
            }
        }
    }
}
