package jnet.client.gui.dialog;

import jnet.client.Client;
import jnet.client.network.NettyClient;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginDialog extends JDialog {

    public LoginDialog() {

        setTitle("Přihlásit");
        setSize(300, 250);
        setLocationRelativeTo(null);
        setResizable(false);
        setAlwaysOnTop(true);
        JPanel panel = new JPanel();
        add(panel);
        placeComponents(panel);

    }

    @Override
    public void setVisible(boolean b) {
        super.setVisible(b);
    }

    private void placeComponents(JPanel panel) {

        panel.setLayout(null);

        JLabel serverLabel = new JLabel("Server");
        serverLabel.setBounds(20, 10, 80, 25);
        panel.add(serverLabel);

        JTextField serverText = new JTextField(Client.config.getString("server"));
        serverText.setBounds(110, 10, 160, 25);
        panel.add(serverText);

        JLabel portLabel = new JLabel("Port");
        portLabel.setBounds(20, 40, 80, 25);
        panel.add(portLabel);

        JTextField portText = new JTextField(Client.config.getString("port"));
        portText.setBounds(110, 40, 160, 25);
        panel.add(portText);

        JLabel userLabel = new JLabel("Uživatel");
        userLabel.setBounds(20, 70, 80, 25);
        panel.add(userLabel);

        JTextField userText = new JTextField(Client.config.getString("user"));
        userText.setBounds(110, 70, 160, 25);
        panel.add(userText);

        JLabel passwordLabel = new JLabel("Heslo");
        passwordLabel.setBounds(20, 100, 80, 25);
        panel.add(passwordLabel);

        JPasswordField passwordText = new JPasswordField(Client.config.getString("password"));
        passwordText.setBounds(110, 100, 160, 25);
        panel.add(passwordText);

        JButton loginButton = new JButton("Přihlásit");
        loginButton.setBounds(20, 155, 90, 25);
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                // ulozeni prihlasovacich udaju
                Client.config.setString("server", serverText.getText());
                Client.config.setString("port", new String(portText.getText()));
                Client.config.setString("user", userText.getText());
                Client.config.setString("password", new String(passwordText.getPassword()));
                Client.config.save();

                // pripojeni klienta
                NettyClient client = new NettyClient(serverText.getText(), Integer.valueOf(portText.getText()));
                Client.clientThread = new Thread(client);
                Client.clientThread.start();
                // zavreni dialogu
                dispose();
            }
        });
        panel.add(loginButton);

        JButton closeButton = new JButton("Storno");
        closeButton.setBounds(185, 155, 80, 25);
        closeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        panel.add(closeButton);

    }

}
