import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.SwingConstants;

public class Login {

	private JFrame loginFrame;
	private static Connection connection = null;

	private JPasswordField passwordHeslo;
	public static JTextField textLoginJmeno;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login window = new Login();
					window.loginFrame.setVisible(true);
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null, e);
				}
			}
		});
	}

	public static String JmenoUzivatele() {

		String Jmeno = textLoginJmeno.getText();
		return Jmeno;
	}

	/**
	 * Create the application.
	 */
	public Login() {
		initialize();
		connection = sqliteConnection.dbConnector();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		loginFrame = new JFrame();
		loginFrame.setBounds(100, 100, 550, 345);
		loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		loginFrame.getContentPane().setLayout(null);

		passwordHeslo = new JPasswordField();
		passwordHeslo.setBounds(239, 110, 86, 20);
		loginFrame.getContentPane().add(passwordHeslo);

		textLoginJmeno = new JTextField();
		textLoginJmeno.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				if (textLoginJmeno.getText().length() >= 15) {
					Toolkit.getDefaultToolkit().beep();
					e.consume();
				}
			}
		});
		textLoginJmeno.setBounds(239, 66, 86, 20);
		loginFrame.getContentPane().add(textLoginJmeno);
		textLoginJmeno.setColumns(10);

		JLabel lblLoginJmeno = new JLabel("P\u0159ihla\u0161ovac\u00ED jm\u00E9no");
		lblLoginJmeno.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLoginJmeno.setBounds(82, 69, 147, 14);
		loginFrame.getContentPane().add(lblLoginJmeno);

		JLabel lblLoginHeslo = new JLabel("Heslo");
		lblLoginHeslo.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLoginHeslo.setBounds(140, 113, 89, 14);
		loginFrame.getContentPane().add(lblLoginHeslo);

		JButton btnLogin = new JButton("P\u0159ihl\u00E1sit");
		btnLogin.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent arg0) {
				try {

					String querry = "select * from Uzivatele where jmeno = ? and heslo = ?";
					PreparedStatement pst = connection.prepareStatement(querry);
					pst.setString(1, textLoginJmeno.getText());
					pst.setString(2, passwordHeslo.getText());

					ResultSet rs = pst.executeQuery();
					int count = 0;
					while (rs.next()) {
						count = count + 1;

					}
					if (count == 1) {
						JOptionPane.showMessageDialog(null, "Přihlášení proběhlo úspěšně");

						loginFrame.dispose();
						JmenoUzivatele();
						FitnessKalkulačka.Fit();

					} else if (count > 1) {
						JOptionPane.showMessageDialog(null, "Duplicitní přihlašovací jméno a heslo");
					} else {
						JOptionPane.showMessageDialog(null, "Kombinace přihlašovacího jména a hesla není správná");
					}

					rs.close();
					pst.close();
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null, e);
				}

			}

		});
		btnLogin.setBounds(140, 215, 109, 29);
		loginFrame.getContentPane().add(btnLogin);

		JButton btnRegistrace = new JButton("Registrovat");
		btnRegistrace.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {

					Registration.Registrace();

				} catch (Exception e) {
					JOptionPane.showMessageDialog(null, e);
				}
			}
		});
		btnRegistrace.setBounds(289, 215, 109, 29);
		loginFrame.getContentPane().add(btnRegistrace);

	}
}
