package ru.nsu.ccfit.bogush.view;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import ru.nsu.ccfit.bogush.LoginPayload;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class LoginView extends JFrame {
	private static final Logger logger = LogManager.getLogger(LoginView.class.getSimpleName());

	private static final String TITLE = "Login";
	private static final String LOGIN_BUTTON_TEXT = "Login";
	private static final String NICK_LABEL_NAME = "Enter your nickname: ";

	private static final Dimension NICK_TEXT_FIELD_MIN_SIZE = new Dimension(50, 30);
	private static final Dimension NICK_TEXT_FIELD_PREF_SIZE = new Dimension(100, 30);
	private static final Dimension NICK_TEXT_FIELD_MAX_SIZE = new Dimension(100, 30);

	private static final Border ROOT_BORDER = BorderFactory.createEmptyBorder(10, 10, 10, 10);

	private ViewController viewController;

	private JPanel rootPanel;
	private JTextField nickTextField;

	public LoginView(ViewController viewController) throws HeadlessException {
		super(TITLE);
		this.viewController = viewController;
		createComponents();
		this.setContentPane(rootPanel);
		this.setResizable(false);
		this.setAlwaysOnTop(true);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}

	private void createComponents() {
		rootPanel = new JPanel();
		rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.Y_AXIS));
		rootPanel.setBorder(ROOT_BORDER);
		rootPanel.setOpaque(true);

		nickTextField = new JTextField();
		nickTextField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					logger.trace("Enter pressed while typing");
					login();
				}
			}
		});
		nickTextField.setMinimumSize(NICK_TEXT_FIELD_MIN_SIZE);
		nickTextField.setPreferredSize(NICK_TEXT_FIELD_PREF_SIZE);
		nickTextField.setMaximumSize(NICK_TEXT_FIELD_MAX_SIZE);

		JLabel nickLabel = new JLabel(NICK_LABEL_NAME);

		JButton loginButton = new JButton(new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				logger.trace("Login button clicked");
				login();
			}
		});
		loginButton.setText(LOGIN_BUTTON_TEXT);

		nickLabel.setAlignmentX(CENTER_ALIGNMENT);
		nickTextField.setAlignmentX(CENTER_ALIGNMENT);
		loginButton.setAlignmentX(CENTER_ALIGNMENT);

		rootPanel.add(nickLabel);
		rootPanel.add(nickTextField);
		rootPanel.add(loginButton);
	}

	private void alert(String title, String description) {
		new AlertDialog(this, title, description);
	}

	private void login() {
		String nickname = nickTextField.getText().trim();
		if (nickname.isEmpty()) {
			logger.trace("nickname is empty");
			alert("Login", "Nickname is empty!");
		} else {
			viewController.login(new LoginPayload(nickname));
		}
	}
}

@@ -267,74 +267,8 @@
 ent;
-%0Aimport java.awt.event.KeyAdapter;%0Aimport java.awt.event.KeyEvent;
 %0A%0Apu
@@ -1597,157 +1597,113 @@
 ;%0A%09%09
-nickTextField.addKeyListener(new KeyAdapter() %7B%0A%09%09%09@Override%0A%09%09%09public void keyPressed(KeyEvent e) %7B%0A%09%09%09%09if (e.getKeyCode() == KeyEvent.VK_ENTER
+Action loginAction = new AbstractAction() %7B%0A%09%09%09@Override%0A%09%09%09public void actionPerformed(ActionEvent e
 ) %7B%0A
-%09
 %09%09%09%09
@@ -1720,39 +1720,34 @@
 ce(%22
-Enter pressed while typing
+Login action performed
 %22);%0A
-%09
 %09%09%09%09
@@ -1762,19 +1762,61 @@
 %0A%09%09%09
-%09
 %7D%0A%09%09
-%09%7D%0A%09%09%7D
+%7D;%0A%0A%09%09nickTextField.addActionListener(loginAction
 );%0A%09
@@ -2083,155 +2083,19 @@
 ton(
-new AbstractAction() %7B%0A%09%09%09@Override%0A%09%09%09public void actionPerformed(ActionEvent e) %7B%0A%09%09%09%09logger.trace(%22Login button clicked%22);%0A%09%09%09%09login();%0A%09%09%09%7D%0A%09%09%7D
+loginAction
 );%0A%09
@@ -2598,17 +2598,17 @@
 .trace(%22
-n
+N
 ickname 

Fix login on enter pressed