package jguard.client.gui.dialog;

import cucky.jGuard.lib.Message;
import cucky.jGuard.lib.object.Map;
import cucky.jGuard.lib.object.MapObject;
import cucky.jGuard.lib.object.ObjectConnection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import jguard.client.Client;

public class ObjectConnectionDialog extends JDialog {

    private static ObjectConnection connection;
    private MapObject sObj;
    private MapObject eObj;
    private JComboBox<String> typComboBox = new JComboBox<String>(new DefaultComboBoxModel(new String[]{"Neznámé", "1000 Mbit/s", "100 Mbit/s", "10 Mbit/s", "VLAN", "Bezdrát", "10G Optic", "1G Optic", "100M Optic" }));
    private static JComboBox<String> zdrojComboBox = new JComboBox<String>();
    private static int zdrojSelectedIndex = 0;
    private static JComboBox<String> rozhraniComboBox = new JComboBox<String>();
    private JCheckBox read = new JCheckBox("", false);
    private boolean edit = false;

    public ObjectConnectionDialog(ObjectConnection c) {
        this.edit = true;
        this.connection = c;

        // nastavani hodnot do polí
        typComboBox.setSelectedIndex(this.connection.getType());
        if (this.connection.isRead()) {
            read.setSelected(true);
            zdrojComboBox.setEnabled(true);
            rozhraniComboBox.setEnabled(true);
        } else {
            read.setSelected(false);
            zdrojComboBox.setEnabled(false);
            rozhraniComboBox.setEnabled(false);
        }

        getPanel();
    }

    public ObjectConnectionDialog(int startObjId, int endObjId) {

        this.connection = new ObjectConnection();
        this.connection.setFrom(startObjId);
        this.connection.setTo(endObjId);

        zdrojComboBox.setEnabled(false);
        rozhraniComboBox.setEnabled(false);

        getPanel();
    }

    private void getPanel() {

        setTitle("Vlastnosti propojení");
        setSize(450, 400);
        setResizable(false);
        setLocationRelativeTo(null);

        // ziskani informaci o objektech
        for (Map map : Client.map) {
            for (MapObject object : map.getObjects()) {
                if (object.getId() == this.connection.getFrom()) {
                    sObj = object;
                    if (object.getId() == this.connection.getSourceObjectId()) {
                       zdrojSelectedIndex = 0; 
//                       Main.client.send(Message.TYPE_SNMP_INTERFACE, sObj);
                    }
                }
                if (object.getId() == this.connection.getTo()) {
                    eObj = object;
                    if (object.getId() == this.connection.getSourceObjectId()) {
                       zdrojSelectedIndex = 1; 
//                       Main.client.send(Message.TYPE_SNMP_INTERFACE, eObj);
                    }
                }
            }
        }

        JPanel panel = new JPanel();
        panel.setLayout(null);

        int stred = getWidth() / 2;

        JLabel firstLabel = new JLabel("1: " + sObj.getName());
        firstLabel.setBounds(20, 10, 500, 25);
        panel.add(firstLabel);

        JLabel secondLabel = new JLabel("2: " + eObj.getName());
        secondLabel.setBounds(20, 40, 500, 25);
        panel.add(secondLabel);

        JLabel typLabel = new JLabel("Typ spojení");
        typLabel.setBounds(20, 100, 140, 25);
        panel.add(typLabel);

        typComboBox.setBounds(200, 100, 200, 25);
        panel.add(typComboBox);

        JLabel cistLabel = new JLabel("Čtení dat");
        cistLabel.setBounds(20, 170, 200, 25);
        panel.add(cistLabel);

        read.setBounds(200, 170, 50, 25);
        read.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (read.isSelected()) {
                    zdrojComboBox.setEnabled(true);
                    rozhraniComboBox.setEnabled(true);
                } else {
                    zdrojComboBox.setEnabled(false);
                    rozhraniComboBox.setEnabled(false);
                }
            }
        });
        panel.add(read);

        JLabel zdrojLabel = new JLabel("Zdrojové zařízení");
        zdrojLabel.setBounds(20, 200, 200, 25);
        panel.add(zdrojLabel);

        zdrojComboBox.setModel(new DefaultComboBoxModel(new String[]{sObj.getName(), eObj.getName()}));
        zdrojComboBox.setSelectedIndex(zdrojSelectedIndex);
        zdrojComboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (zdrojComboBox.getSelectedIndex() == 0) {
//                   Main.client.send(Message.TYPE_SNMP_INTERFACE, sObj);
                } else {
//                    Main.client.send(Message.TYPE_SNMP_INTERFACE, eObj);
                }
                // dokud se nenactou hodnoty vypne combobox
                rozhraniComboBox.setEnabled(false);
            }
        });
        zdrojComboBox.setBounds(200, 200, 200, 25);
        panel.add(zdrojComboBox);

        JLabel rozhraniLabel = new JLabel("Rozhraní");
        rozhraniLabel.setBounds(20, 230, 200, 25);
        panel.add(rozhraniLabel);

        rozhraniComboBox.setModel(new DefaultComboBoxModel(new String[]{" "}));
        rozhraniComboBox.setBounds(200, 230, 200, 25);
        panel.add(rozhraniComboBox);

        JButton saveButton = new JButton("Uložit");
        saveButton.setBounds(stred - 120, 300, 90, 25);
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (zdrojComboBox.getSelectedIndex() == 0) {
                    connection.setSourceObjectId(sObj.getId());
                } else {
                    connection.setSourceObjectId(sObj.getId());
                }
                connection.setType(typComboBox.getSelectedIndex());
                connection.setRead(read.isSelected());
                connection.setMap(sObj.getMap());
                connection.setFrom(sObj.getId());
                connection.setTo(eObj.getId());
                connection.setIface(rozhraniComboBox.getSelectedIndex());
                         
                if (edit) {
//                    Main.client.send(Message.TYPE_EDIT_CONNECTION, connection);
                } else {
                    Client.client.send(Message.NEW_CONNECTION, connection);
                }
                
                dispose();
            }
        });
        panel.add(saveButton);

        JButton closeButton = new JButton("Storno");
        closeButton.setBounds(stred + 30, 300, 90, 25);
        closeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        panel.add(closeButton);

        add(panel);

        setVisible(true);
        setAlwaysOnTop(true);
    }

    public static void reloadInterfaceList(ArrayList<String> list) {
        rozhraniComboBox.removeAllItems();
        for (String string : list) {
            rozhraniComboBox.addItem(string);
        }
        if (zdrojComboBox.getSelectedIndex() == zdrojSelectedIndex && !list.isEmpty()) {
            rozhraniComboBox.setSelectedIndex(connection.getIface());
        }
        rozhraniComboBox.setEnabled(true);
    }
    
     
}
