package jguard.client.gui;

import cucky.jGuard.lib.object.ObjectType;
import cucky.jGuard.lib.object.SnmpProfile;
import cucky.jGuard.lib.object.User;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import jguard.client.Client;
import jguard.client.gui.dialog.AddDeviceType;

public class ServerSettingsPanel extends JTabbedPane {
    
    public static Component zarizeni;
    
    
    public ServerSettingsPanel() {

        
        this.addTab("Základní", tabZakladni());
        this.addTab("Typy zařízení", tabZarizeni());
        this.addTab("SNMP", tabSnmpProfile());
        this.addTab("Uživatelé", tabUsers());
        
        // vola se pri zmene zalozky
        /*
        this.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                System.out.println("Tab: " + tabbedPane.getSelectedIndex());
            }
        });
        */

    }
    
    private Component tabZakladni() {
        
        JPanel panel = new JPanel();
        
        panel.setLayout(null);

        JLabel nestLabel = new JLabel("Úroveň nestability (ms)");
        nestLabel.setBounds(20, 20, 150, 25);
        panel.add(nestLabel);
        JTextField nestField = new JTextField();
        nestField.setBounds(170, 20, 80, 25);
        panel.add(nestField);
        
        JLabel timeoutLabel = new JLabel("Úroveň timeout (ms)");
        timeoutLabel.setBounds(20, 60, 150, 25);
        panel.add(timeoutLabel);
        JTextField timeoutField = new JTextField();
        timeoutField.setBounds(170, 60, 80, 25);
        panel.add(timeoutField);
        
        return new JScrollPane(panel);
    }
    
    private Component tabZarizeni() {
        JButton button = new JButton("Přidat typ");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new AddDeviceType().show();
            }
        });
        
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        JTable tableDeviceType = new TableDeviceType();
        panel.add(new JScrollPane(tableDeviceType), BorderLayout.CENTER);
        panel.add(button, BorderLayout.PAGE_END);
        return panel;
    }
    
    private Component tabUsers() {

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

    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");
            addColumn("Port");

            // radky
            for (SnmpProfile sp : Client.serverSettings.getSnmp()) {
                addRow(new Object[]{
                    sp.getName(),
                    sp.getVersion(),
                    sp.getCommunity(), 
                    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.serverSettings.getUsers()) {
                addRow(new Object[]{
                    sp.getUsername(),
                    sp.isSetServer(),
                    sp.isAddMap(), 
                    sp.isRemoveMap()
                });
            }

        }
    }
    
    private static class TableModelDeviceType extends DefaultTableModel {

        public TableModelDeviceType() {

            // sloupce
            addColumn("Jméno");
            addColumn("Ikona");

            // radky
            for (ObjectType ot : Client.deviceType) {
                addRow(new Object[]{
                    ot.getName(), 
                    ot.getImgStr()
                });
            }

        }
    }

}
