package jnet.client.gui.dialog;

import jnet.client.gui.ServerSettingsPanel;
import jnet.client.gui.Window;
import jnet.client.network.NettyClient;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import jnet.lib.Message;
import jnet.lib.object.User;

public class UserDialog extends JDialog {

    JTextField userName = new JTextField();
    JLabel userNameLabel = new JLabel("Uživatelské jméno");
    JTextField password = new JTextField();
    JLabel passwordLabel = new JLabel("Heslo");
    JCheckBox setServer = new JCheckBox("Nastavit server");
    JCheckBox addMap = new JCheckBox("Přidat mapu");
    JCheckBox deleteMap = new JCheckBox("Odebrat mapu");
    JCheckBox editMap = new JCheckBox("Upravit mapu");
    JCheckBox addObjekt = new JCheckBox("Přidat objekt");
    JCheckBox deleteObjekt = new JCheckBox("Odebrat objekt");
    JCheckBox editObjekt = new JCheckBox("Upravit objekt");

    User user;

    public UserDialog(User user) {

        this.user = user;

        if (user == null) {
            setTitle("Přidat uživatele");
        } else {
            setTitle("Upravit uživatele");
        }

        setSize(400, 600);
        setLocationRelativeTo(null);
        setAlwaysOnTop(false);
        JPanel panel = new JPanel();
        add(panel);
        placeComponents(panel);
        
        if (user != null) {
            userName.setText(user.getUsername());
            setServer.setSelected(user.isEditServer());
            addMap.setSelected(user.isAddMap());
            deleteMap.setSelected(user.isRemoveMap());
            editMap.setSelected(user.isEditMap());
            addObjekt.setSelected(user.isAddObject());
            deleteObjekt.setSelected(user.isRemoveObject());
            editObjekt.setSelected(user.isEditObject());
        } else {
            password.setText(generateRandomString(8));
        }

    }

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

        userName.setBounds(20, 10, 180, 25);
        panel.add(userName);

        userNameLabel.setBounds(210, 10, 160, 25);
        panel.add(userNameLabel);

        password.setBounds(20, 60, 180, 25);
        panel.add(password);

        passwordLabel.setBounds(210, 60, 160, 25);
        panel.add(passwordLabel);

        setServer.setBounds(20, 110, 300, 25);
        panel.add(setServer);

        addMap.setBounds(20, 160, 300, 25);
        panel.add(addMap);

        deleteMap.setBounds(20, 210, 300, 25);
        panel.add(deleteMap);

        editMap.setBounds(20, 260, 300, 25);
        panel.add(editMap);

        addObjekt.setBounds(20, 310, 300, 25);
        panel.add(addObjekt);

        deleteObjekt.setBounds(20, 360, 300, 25);
        panel.add(deleteObjekt);

        editObjekt.setBounds(20, 410, 300, 25);
        panel.add(editObjekt);

        JButton button = new JButton();
        if (user == null) {
            button.setText("Přidat");
        } else {
            button.setText("Upravit");
        }
        button.setBounds(20, 460, 90, 25);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (userName.getText().length() > 3) {
                    User u = new User(
                            0,
                            userName.getText(),
                            password.getText(),
                            addMap.isSelected(),
                            deleteMap.isSelected(),
                            editMap.isSelected(),
                            addObjekt.isSelected(),
                            deleteObjekt.isSelected(),
                            editObjekt.isSelected(),
                            setServer.isSelected());

                    if (user == null) {
                        
                        NettyClient.send(Message.USER, u);
                    } else {
                        u.setId(user.getId());
                        NettyClient.send(Message.USER_EDIT, u);
                    }
                    Window.showServerSettings(ServerSettingsPanel.TAB_USERS);
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "Uživatelské jméno musí být delší než 3 znaky", "Chyba", JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        panel.add(button);

    }

    public static String generateRandomString(int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder sb = new StringBuilder();
        Random random = new Random();

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            sb.append(characters.charAt(index));
        }

        return sb.toString();
    }

}
