package jguard.client.gui.dialog;

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;
import jguard.client.Config;
import jguard.client.Client;
import static jguard.client.Client.config;
import jguard.client.network.NetworkClient;

public class LoginDialog extends JDialog{

    public LoginDialog() {
        
        setTitle("Přihlásit");
        setSize(300,250); 
        setLocationRelativeTo(null);
        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(new Config().get("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(new Config().get("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(new Config().get("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(new Config().get("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) {
                // pripojeni klienta
                Client.client = new NetworkClient(serverText.getText(), Integer.parseInt(portText.getText()));
                Client.client.start();
                // ulozeni prihlasovacich udaju
                config.setProperty("server", serverText.getText());
                config.setProperty("port", new String(portText.getText()));
                config.setProperty("user", userText.getText());
                config.setProperty("password", new String(passwordText.getPassword()));
                new Config().store(config);
                // 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);

	}
    
}
