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

public class Registration {

	private JFrame registrationFrame;
	private JTextField textRegJmeno;
	private JPasswordField passwordRegHeslo;
	private static Connection connection = null;

	/**
	 * Launch the application.
	 */
	public static void Registrace() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Registration window = new Registration();
					window.registrationFrame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

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

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		registrationFrame = new JFrame();
		registrationFrame.setBounds(100, 100, 481, 354);
		registrationFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		registrationFrame.getContentPane().setLayout(null);

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

		JLabel lblRegJmeno = new JLabel("P\u0159ihla\u0161ovac\u00ED jm\u00E9no");
		lblRegJmeno.setHorizontalAlignment(SwingConstants.RIGHT);
		lblRegJmeno.setBounds(77, 76, 122, 14);
		registrationFrame.getContentPane().add(lblRegJmeno);

		JLabel lblRegHeslo = new JLabel("Heslo");
		lblRegHeslo.setHorizontalAlignment(SwingConstants.RIGHT);
		lblRegHeslo.setBounds(113, 119, 86, 14);
		registrationFrame.getContentPane().add(lblRegHeslo);

		JButton btnReg = new JButton("Registrovat");
		btnReg.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent arg0) {
				try {

					String querry1 = "insert into Uzivatele (jmeno,heslo) values (?,?)";
					PreparedStatement pst = connection.prepareStatement(querry1);
					pst.setString(1, textRegJmeno.getText());
					pst.setString(2, passwordRegHeslo.getText());

					pst.execute();

					JOptionPane.showMessageDialog(null, "Registrace proběhla úspěšně");

					pst.close();
					registrationFrame.dispose();

				} catch (org.sqlite.SQLiteException e1) {
					JOptionPane.showMessageDialog(null, "Uživatelské jméno již existuje, zvolte prosím jiné");
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null, e);
				}

			}
		});
		btnReg.setBounds(178, 220, 106, 28);
		registrationFrame.getContentPane().add(btnReg);

		passwordRegHeslo = new JPasswordField();
		passwordRegHeslo.addKeyListener(new KeyAdapter() {
			@SuppressWarnings("deprecation")
			@Override
			public void keyTyped(KeyEvent e) {
				if (passwordRegHeslo.getText().length() >= 20) {
					Toolkit.getDefaultToolkit().beep();
					e.consume();
				}
			}
		});
		passwordRegHeslo.setBounds(209, 116, 86, 20);
		registrationFrame.getContentPane().add(passwordRegHeslo);

		JButton btnRegZpet = new JButton("Zp\u011Bt");
		btnRegZpet.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				registrationFrame.dispose();
			}
		});
		btnRegZpet.setBounds(178, 259, 106, 28);
		registrationFrame.getContentPane().add(btnRegZpet);
	}
}
