package jnet.client.gui.dialog;

import jnet.client.network.NettyClient;
import java.awt.Color;
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.JTextField;
import jnet.lib.Message;


public class AddMapDialog extends JDialog {

    public AddMapDialog() {
        
        setTitle("Přidat mapu");
        setSize(320,180); 
        setLocationRelativeTo(null);
        setAlwaysOnTop(true);
        setResizable(false);
        JPanel panel = new JPanel();
        add(panel);
        placeComponents(panel);
        
    }
    
    @Override
    public void setVisible(boolean b) {
        super.setVisible(b); 
    }

    private JPanel placeComponents(JPanel panel) {
        
        panel.setLayout(null);

        JLabel serverLabel = new JLabel("Název");
        serverLabel.setBounds(20, 20, 80, 25);
        panel.add(serverLabel);

        JTextField nameText = new JTextField();
        nameText.setBounds(110, 20, 160, 25);
        panel.add(nameText);
        
        JLabel error = new JLabel("");
        error.setBounds(110, 45, 160, 25);
        error.setForeground(Color.red);
        panel.add(error);
        
        JButton button = new JButton("Přidat");
        button.setBounds(120, 80, 80, 25);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!nameText.getText().equals("")) {
                    NettyClient.send(Message.MAP_ADD, nameText.getText());
                    dispose();
                } else {
                    error.setText("Název nesmí být prázdný");
                }
            }
        });
        panel.add(button);
        
        return panel;
        
    }
    
    

}
