/* File: LoginPane.java
 * Author: Jason Gookins
 * Description: The login screen for the Belief web applet.
 */

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginPane extends JPanel implements ActionListener
{
	/************************
	 ** Instance Variables **
	 ************************/

	private Belief belief;
	private JTextField usernameTextField;
	private JPasswordField passwordField;
	private JButton okayButton, cancelButton;



	/**********************
	 ** Main Constructor **
	 **********************/

	public LoginPane(Belief belief)
	{
		this.belief = belief;

		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		JLabel beliefLabel = new JLabel("Belief", JLabel.CENTER);
		beliefLabel.setFont(new Font("SansSerif", Font.PLAIN, 24));
		beliefLabel.setPreferredSize(new Dimension(200, 60));
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 2;
		c.gridheight = 1;
		c.fill = GridBagConstraints.BOTH;
		add(beliefLabel, c);

		JPanel inputPanel = new JPanel(new GridBagLayout());
		GridBagConstraints d = new GridBagConstraints();

		JLabel usernameLabel = new JLabel("Username:");
		d.gridx = 0;
		d.gridy = 0;
		d.insets = new Insets(0, 0, 5, 5);
		d.fill = GridBagConstraints.NONE;
		inputPanel.add(usernameLabel, d);

		JLabel passwordLabel = new JLabel("Password:");
		d.gridy = 1;
		d.insets = new Insets(5, 0, 0, 5);
		inputPanel.add(passwordLabel, d);

		usernameTextField = new JTextField();
		usernameTextField.setPreferredSize(new Dimension(200, 20));
		usernameTextField.setMinimumSize(new Dimension(200, 20));
		usernameTextField.addActionListener(this);
		d.gridx = 1;
		d.gridy = 0;
		d.insets = new Insets(0, 10, 5, 5);
		d.fill = GridBagConstraints.BOTH;
		inputPanel.add(usernameTextField, d);

		passwordField = new JPasswordField();
		passwordField.setPreferredSize(new Dimension(200, 20));
		passwordField.setMinimumSize(new Dimension(200, 20));
		passwordField.addActionListener(this);
		d.gridy = 1;
		d.insets = new Insets(5, 10, 0, 5);
		inputPanel.add(passwordField, d);

		c.gridy = 1;
		add(inputPanel, c);

		okayButton = new JButton("Okay");
		okayButton.setPreferredSize(new Dimension(75, 30));
		okayButton.setMinimumSize(new Dimension(75, 30));
		okayButton.addActionListener(this);
		c.gridy = 2;
		c.gridwidth = 1;
		c.weightx = 0.5;
		c.insets = new Insets(15, 0, 0, 5);
		c.fill = GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.EAST;
		add(okayButton, c);

		cancelButton = new JButton("Cancel");
		cancelButton.setPreferredSize(new Dimension(75, 30));
		cancelButton.setMinimumSize(new Dimension(75, 30));
		cancelButton.addActionListener(this);
		c.gridx = 1;
		c.insets = new Insets(15, 5, 0, 0);
		c.anchor = GridBagConstraints.WEST;
		add(cancelButton, c);
	}



	/***************
	 ** Accessors **
	 ***************/

	public JTextField getUsernameTextField()
	{
		return this.usernameTextField;
	}



	/*******************
	 ** Action Events **
	 *******************/

	public void actionPerformed(ActionEvent evt)
	{
		if (evt.getSource().equals(usernameTextField))
		{
			passwordField.requestFocus();
		}
		else if (evt.getSource().equals(okayButton) || evt.getSource().equals(passwordField))
		{
			try
			{
				checkLogin();
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		else if (evt.getSource().equals(cancelButton))
		{
			clearLogin();
		}
	}



	/**********************
	 ** Instance Methods **
	 **********************/

	private void checkLogin() throws Exception
	{
		Class.forName("org.sqlite.JDBC");
		Connection conn = DriverManager.getConnection("jdbc:sqlite:belief.db");
		Statement statement = conn.createStatement();
		String username = usernameTextField.getText();
		String password = new String(passwordField.getPassword());
		ResultSet rs = statement.executeQuery("SELECT * FROM users WHERE name='" + username + "' and pass='" + password + "'");

		if (!rs.isClosed())
		{
			CardLayout mainLayout = (CardLayout)belief.getContentPane().getLayout();
			String userType = rs.getString("type");
			View currentView = new View(username);

			if (userType.equals("user"))
			{
				belief.logIn(username);
				mainLayout.show(belief.getContentPane(), "userChoicePane");

				clearLogin();
			}
			else if (userType.equals("super"))
			{
				belief.setCurrentUser(username);
				mainLayout.show(belief.getContentPane(), "adminChoicePane");

				clearLogin();
			}

			rs.close();
		}
		else
		{
			clearLogin();

			LoginDialog loginDialog = new LoginDialog("Login Error", new JOptionPane("Invalid username and/or password. Please try again.", JOptionPane.ERROR_MESSAGE));
		}

		conn.close();
	}

	private void clearLogin()
	{
		usernameTextField.setText(null);
		passwordField.setText(null);
		usernameTextField.requestFocus();
	}



	/**********************
	 ** Internal Classes **
	 **********************/

	private class LoginDialog extends InternalModalDialog
	{
		public LoginDialog(String title, JOptionPane pane)
		{
			super(title, belief, pane);
		}

		public void cleanUpAfterClosing()
		{
			usernameTextField.requestFocus();
		}
	}
}