package jnet.client.gui;

import jnet.client.Client;
import jnet.client.gui.dialog.AddDeviceTypeDialog;
import jnet.client.gui.dialog.UserDialog;
import jnet.client.network.NettyClient;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
import jnet.lib.LogFile;
import jnet.lib.Message;
import jnet.lib.object.SnmpProfile;
import jnet.lib.object.User;

public class ServerSettingsPanel extends JTabbedPane {

    public static final int TAB_DEFAULT = 0;
    public static final int TAB_DEVICE_TYPE = 1;
    public static final int TAB_USERS = 2;
    public static final int TAB_SNMP_PROFILE = 3;

    public ServerSettingsPanel() {

        this.addTab("Základní", tabZakladni());
        this.addTab("Typy zařízení", tabZarizeni());
        this.addTab("Uživatelé", tabUsers());
        this.addTab("SNMP", tabSnmpProfile());

        // vola se pri zmene zalozky
        this.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {

            }
        });

    }

    public void setTable(int table) {
        this.setSelectedIndex(table);
    }

    private Component tabZakladni() {

        JPanel panel = new JPanel();

        panel.setLayout(null);

        int x1 = 80;
        int x2 = 150;

        JLabel pingLabel = new JLabel("Ping");
        pingLabel.setFont(pingLabel.getFont().deriveFont(20f));
        pingLabel.setBounds(20, 20, 150, 25);
        panel.add(pingLabel);
        
        JTextField repeateField = new JTextField(String.valueOf(Client.serverConfig.getPingAttempt()));
        repeateField.setBounds(x1, 60, 50, 25);
        panel.add(repeateField);

        JLabel repeateLabel = new JLabel("Opakovat ping každých ... sekund");
        repeateLabel.setBounds(x2, 60, 300, 25);
        panel.add(repeateLabel);

        JTextField pokusuField = new JTextField(String.valueOf(Client.serverConfig.getPingAttempt()));
        pokusuField.setBounds(x1, 100, 50, 25);
        panel.add(pokusuField);

        JLabel pokusuLabel = new JLabel("Počet pokusů před vyhodnocenim offline");
        pokusuLabel.setBounds(x2, 100, 300, 25);
        panel.add(pokusuLabel);

        JTextField timeoutField = new JTextField(String.valueOf(Client.serverConfig.getPingTimeout()));
        timeoutField.setBounds(x1, 140, 50, 25);
        panel.add(timeoutField);

        JLabel timeoutLabel = new JLabel("Úroveň timeout (s)");
        timeoutLabel.setBounds(x2, 140, 300, 25);
        panel.add(timeoutLabel);

        JTextField pokusuNestField = new JTextField(String.valueOf(Client.serverConfig.getInstabilityAttempt()));
        pokusuNestField.setBounds(x1, 180, 50, 25);
        panel.add(pokusuNestField);

        JLabel pokusuNestLabel = new JLabel("Počet pokusů před vyhodnocením nestability");
        pokusuNestLabel.setBounds(x2, 180, 300, 25);
        panel.add(pokusuNestLabel);

        JTextField nestField = new JTextField(String.valueOf(Client.serverConfig.getInstabilityLimit()));
        nestField.setBounds(x1, 220, 50, 25);
        panel.add(nestField);

        JLabel nestLabel = new JLabel("Úroveň nestability (s)");
        nestLabel.setBounds(x2, 220, 300, 25);
        panel.add(nestLabel);

        JButton button = new JButton("Uložit");
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Client.serverConfig.setPingRepeat(Integer.valueOf(repeateField.getText()));
                Client.serverConfig.setPingAttempt(Integer.valueOf(pokusuField.getText()));
                Client.serverConfig.setPingTimeout(Integer.valueOf(timeoutField.getText()));
                Client.serverConfig.setInstabilityAttempt(Integer.valueOf(pokusuNestField.getText()));
                Client.serverConfig.setInstabilityLimit(Integer.valueOf(nestField.getText()));
                NettyClient.send(Message.SERVER_CONFIG, Client.serverConfig);
            }
        });
        button.setBounds(x1, 300, 100, 40);
        panel.add(button);

        JButton buttonRestart = new JButton("Restartovat server");
        //buttonRestart.setAlignmentX(Component.CENTER_ALIGNMENT);
        buttonRestart.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int result = JOptionPane.showConfirmDialog(null,
                        "Opravdu chceš pokračovat?",
                        "Restart serveru",
                        JOptionPane.YES_NO_OPTION);

                if (result == JOptionPane.YES_OPTION) {
                    LogFile.printInfo("Odeslan pozadavek na restart serveru");
                    NettyClient.send(Message.RESTART, null);
                } 
            }
        });
        buttonRestart.setBounds(x1+200, 300, 150, 40);
        panel.add(buttonRestart);

        return new JScrollPane(panel);
    }

    private JPanel tabZarizeni() {

        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // Vertikální zarovnání

        JTable table = new TableDeviceType();
        JScrollPane scrollPane = new JScrollPane(table);
        panel.add(new JScrollPane(scrollPane));

        JButton button = new JButton("Přidat typ");
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new AddDeviceTypeDialog().setVisible(true);
            }
        });
        panel.add(button);

        return panel;
    }

    private JPanel tabUsers() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // Vertikální zarovnání

        JTable tableUsers = new TableUsers();
        panel.add(new JScrollPane(tableUsers));

        JButton button = new JButton("Přidat uživatele");
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new UserDialog(null).setVisible(true);
            }
        });
        panel.add(button);

        return panel;
    }

    private Component tabSnmpProfile() {

        JTable table = new JTable(new TableModelSnmpProfile());
        return new JScrollPane(table);
    }

    private static class TableModelSnmpProfile extends DefaultTableModel {

        public TableModelSnmpProfile() {

            // sloupce
            addColumn("Nazev");
            addColumn("Verze");
            addColumn("Community Read");
            addColumn("Community Write");
            addColumn("Port");

            // radky
            for (SnmpProfile sp : Client.snmpProfile) {
                addRow(new Object[]{
                    sp.getName(),
                    sp.getVersion(),
                    sp.getCommunityRead(),
                    sp.getCommunityWrite(),
                    sp.getPort()});
            }

        }
    }

    private static class TableModelUsers extends DefaultTableModel {

        public TableModelUsers() {

            // sloupce
            addColumn("Jméno");
            addColumn("Nastaveni serveru");
            addColumn("Přidat mapu");
            addColumn("Odebrat mapu");
            // radky
            for (User sp : Client.users) {
                addRow(new Object[]{
                    sp.getUsername(),
                    "",
                    "",
                    sp.isAddMap(), //sp.isSetServer(),
                //sp.isAddMap(),
                //sp.isRemoveMap()
                });
            }

        }
    }

}
