package de.david.gui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import de.david.saveOrganizer.Setup;

public class DataPane extends JPanel
{

	private static final String	EMAIL_PATTERN	= "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
			+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

	private JTextField			nameField;
	private JTextField			mailField;
	private JLabel				errorLabel;

	/**
	 * Create the panel.
	 */
	public DataPane()
	{

		JLabel taskLabel = new JLabel("Wir brauchen einige Daten über Sie.");

		JLabel nameLabel = new JLabel("Name");

		nameField = new JTextField();
		nameField.setColumns(10);

		JLabel mailLabel = new JLabel("Mail-Adresse");

		mailField = new JTextField();
		mailField.setColumns(10);

		JButton btnWeiter = new JButton("weiter");
		btnWeiter.setActionCommand("weiter");
		btnWeiter.addActionListener(new ActionHandler());

		JButton btnAbbrechen = new JButton("abbrechen");
		btnAbbrechen.setActionCommand("abbrechen");
		btnAbbrechen.addActionListener(new ActionHandler());

		errorLabel = new JLabel("Error");
		errorLabel.setVisible(false);
		errorLabel.setForeground(Color.RED);
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.TRAILING).addGroup(groupLayout
				.createSequentialGroup()
				.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup().addContainerGap().addComponent(taskLabel))
						.addGroup(groupLayout.createSequentialGroup()
								.addGap(35).addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addComponent(nameLabel).addComponent(mailLabel))
								.addGap(91)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
										.addComponent(mailField).addComponent(nameField, 144, 144, Short.MAX_VALUE)))
						.addGroup(groupLayout.createSequentialGroup().addContainerGap().addComponent(btnAbbrechen)
								.addPreferredGap(ComponentPlacement.RELATED, 236, Short.MAX_VALUE)
								.addComponent(btnWeiter))
						.addGroup(groupLayout.createSequentialGroup().addContainerGap().addComponent(errorLabel)))
				.addContainerGap()));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup().addContainerGap().addComponent(taskLabel).addGap(53)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(nameLabel).addComponent(
						nameField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addGap(41)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(mailLabel).addComponent(
						mailField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(ComponentPlacement.RELATED, 63, Short.MAX_VALUE).addComponent(errorLabel).addGap(26)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(btnWeiter)
						.addComponent(btnAbbrechen))
				.addContainerGap()));
		setLayout(groupLayout);

	}

	private class ActionHandler implements ActionListener
	{

		public void actionPerformed(ActionEvent e)
		{
			if (e.getActionCommand().equals("weiter"))
			{
				String name = nameField.getText();
				String mail = mailField.getText();

				if (check(name, mail))
				{
					passPane pPane = new passPane(name, mail);
					Setup.welFrame.setContentPane(pPane);
					Setup.welFrame.revalidate();
				} else
					errorLabel.setVisible(true);
				return;
			}
			if (e.getActionCommand().equals("abbrechen"))
			{
				Setup.welFrame.cancel();
				return;
			}

		}

		private boolean check(String name, String mail)
		{
			if (name.isEmpty() || mail.isEmpty())
			{
				errorLabel.setText("Die Felder dürfen nicht leer sein!");
				return false;
			}
			if (!mail.matches(EMAIL_PATTERN))
			{
				errorLabel.setText("Keine gültige Mail-Addresse angegeben!");
				return false;
			}

			return true;
		}

	}
}