package jrummikub.view.impl;

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

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import jrummikub.util.Event;
import jrummikub.util.Event1;
import jrummikub.util.IEvent;
import jrummikub.util.IEvent1;
import jrummikub.util.LoginData;
import jrummikub.view.ILoginPanel;

@SuppressWarnings("serial")
class LoginPanel extends JPanel implements ILoginPanel {
	private Event1<LoginData> loginEvent = new Event1<LoginData>();
	private Event cancelEvent = new Event();
	private JTextField userNameField;
	private JTextField serverNameField;
	private JTextField passwordField;
	private JTextField channelNameField;

	LoginPanel() {
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.weightx = 1;
		c.weighty = 1;

		userNameField = addInputRow("Benutzername:", new JTextField());
		userNameField.setText("test1");
		serverNameField = addInputRow("Server:", new JTextField());
		serverNameField.setText("universe-factory.net");
		passwordField = addInputRow("Passwort:", new JPasswordField());
		channelNameField = addInputRow("Channel:", new JTextField());
		channelNameField.setText("jrummikub@muc.universe-factory.net");
		add(Box.createVerticalGlue(), c);

		c.gridwidth = 1;
		c.weighty = 0;
		JButton loginButton = new JButton("Login");
		loginButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				loginEvent.emit(new LoginData(userNameField.getText(), serverNameField
						.getText(), passwordField.getText(), channelNameField.getText()));
			}
		});
		add(loginButton, c);

		c.weightx = 0;
		add(Box.createHorizontalStrut(10), c);

		c.gridwidth = GridBagConstraints.REMAINDER;
		c.weightx = 1;
		JButton cancelButton = new JButton("Abbrechen");
		cancelButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				cancelEvent.emit();
			}
		});
		add(cancelButton, c);

		setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(
				10, 10, 10, 10)));
	}

	@Override
	public IEvent1<LoginData> getLoginEvent() {
		return loginEvent;
	}

	@Override
	public IEvent getCancelEvent() {
		return cancelEvent;
	}

	private JTextField addInputRow(String label, JTextField textField) {
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.gridwidth = 1;
		c.weightx = 1;
		c.weighty = 0;

		add(new JLabel(label), c);

		c.gridwidth = GridBagConstraints.REMAINDER;
		add(textField, c);
		return textField;
	}
}
