/*
 * ClientTunnelWindow.java
 *
 * Created on 10-jun-2009, 16:49:12
 */

package net.i2p.desktopgui.gui;

import net.i2p.i2ptunnel.web.EditBean;
import java.awt.event.ActionListener;

/**
 *
 * @author mathias
 */
public class ClientTunnelWindow extends javax.swing.JFrame {

    /** Creates new form ClientTunnelWindow */
    public ClientTunnelWindow(int tunnelNumber, ActionListener al) {
        initComponents();
        this.tunnelNumber = tunnelNumber;
        this.al = al;
        extraInitComponents();
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setSize(600, 600);
        this.setLocationRelativeTo(null);
        this.requestFocus();
        this.changeTunnelState.setVisible(false); //TODO: implement tunnel state change
        this.setVisible(true);
    }

    public void extraInitComponents() {
        EditBean bean = new EditBean();
        if(!bean.isClient(tunnelNumber)) {
            this.dispose();
        }
        else {
            this.tunnelName.setText(bean.getTunnelName(tunnelNumber));
            this.tunnelType.setText(bean.getTunnelType(tunnelNumber));
            this.tunnelPort.setText(bean.getClientPort(tunnelNumber));
            this.tunnelDestination.setText(bean.getClientDestination(tunnelNumber));

            if(bean.getTunnelType(tunnelNumber).equals(TYPE_STREAMR_CLIENT)) {
                tunnelProfile.setVisible(false);
                tunnelProfileLabel.setVisible(false);
                this.delayConnect.setVisible(false);
                this.sharedClient.setVisible(false);
                this.autoStart.setVisible(false);
            }
            else {
                if(bean.isInteractive(tunnelNumber)) {
                    tunnelProfile.setSelectedIndex(TUNNEL_INTERACTIVE);
                }
                else {
                    tunnelProfile.setSelectedIndex(TUNNEL_BULK);
                }

                this.delayConnect.setSelected(bean.shouldDelay(tunnelNumber));
                this.sharedClient.setSelected(bean.isSharedClient(tunnelNumber));
                this.autoStart.setSelected(bean.startAutomatically(tunnelNumber));
            }

            this.tunnelDepth.setSelectedIndex(bean.getTunnelDepth(tunnelNumber, 2));

            int variance = bean.getTunnelVariance(tunnelNumber, 0);
            if(variance == 0) {
                this.depthVariance.setSelectedIndex(0);
            }
            else if(variance == 1) {
                this.depthVariance.setSelectedIndex(1);
            }
            else if(variance == 2) {
                this.depthVariance.setSelectedIndex(2);
            }
            else if(variance == -1) {
                this.depthVariance.setSelectedIndex(3);
            }
            else if(variance == -2) {
                this.depthVariance.setSelectedIndex(4);
            }

            int tunnelQuantity = bean.getTunnelQuantity(tunnelNumber, 2) - 1;
            if(tunnelQuantity >= 0 && tunnelQuantity <= 2) {
                this.tunnelCount.setSelectedIndex(tunnelQuantity);
            }

            int backupTunnelQuantity = bean.getTunnelBackupQuantity(tunnelNumber, 0);
            if(backupTunnelQuantity >= 0 && backupTunnelQuantity <= 3) {
                this.backupTunnelCount.setSelectedIndex(backupTunnelQuantity);
            }

            
            if(bean.getTunnelType(tunnelNumber).equals(TYPE_STREAMR_CLIENT)) {
                this.reduceIdle.setVisible(false);
                this.closeIdle.setVisible(false);
                this.delayIdle.setVisible(false);
            }
            else {
                this.reduceIdle.setSelected(bean.getReduce(tunnelNumber));
                this.closeIdle.setSelected(bean.getClose(tunnelNumber));
                this.delayIdle.setSelected(bean.getDelayOpen(tunnelNumber));
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tunnelNameLabel = new javax.swing.JLabel();
        tunnelTypeLabel = new javax.swing.JLabel();
        tunnelPortLabel = new javax.swing.JLabel();
        tunnelDestinationLabel = new javax.swing.JLabel();
        tunnelProfileLabel = new javax.swing.JLabel();
        delayConnect = new javax.swing.JCheckBox();
        sharedClient = new javax.swing.JCheckBox();
        autoStart = new javax.swing.JCheckBox();
        jSeparator1 = new javax.swing.JSeparator();
        tunnelDepthLabel = new javax.swing.JLabel();
        depthVarianceLabel = new javax.swing.JLabel();
        tunnelCountLabel = new javax.swing.JLabel();
        backupTunnelCountLabel = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        reduceIdle = new javax.swing.JCheckBox();
        closeIdle = new javax.swing.JCheckBox();
        delayIdle = new javax.swing.JCheckBox();
        jSeparator3 = new javax.swing.JSeparator();
        save = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        tunnelName = new javax.swing.JTextField();
        tunnelType = new javax.swing.JLabel();
        tunnelPort = new javax.swing.JTextField();
        tunnelDestination = new javax.swing.JTextField();
        tunnelProfile = new javax.swing.JComboBox();
        tunnelDepth = new javax.swing.JComboBox();
        depthVariance = new javax.swing.JComboBox();
        tunnelCount = new javax.swing.JComboBox();
        backupTunnelCount = new javax.swing.JComboBox();
        changeTunnelState = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(net.i2p.desktopgui.desktopgui.Main.class).getContext().getResourceMap(ClientTunnelWindow.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        getContentPane().setLayout(null);

        tunnelNameLabel.setText(resourceMap.getString("tunnelNameLabel.text")); // NOI18N
        tunnelNameLabel.setName("tunnelNameLabel"); // NOI18N
        getContentPane().add(tunnelNameLabel);
        tunnelNameLabel.setBounds(20, 20, 120, 17);

        tunnelTypeLabel.setText(resourceMap.getString("tunnelTypeLabel.text")); // NOI18N
        tunnelTypeLabel.setName("tunnelTypeLabel"); // NOI18N
        getContentPane().add(tunnelTypeLabel);
        tunnelTypeLabel.setBounds(20, 50, 120, 17);

        tunnelPortLabel.setText(resourceMap.getString("tunnelPortLabel.text")); // NOI18N
        tunnelPortLabel.setName("tunnelPortLabel"); // NOI18N
        getContentPane().add(tunnelPortLabel);
        tunnelPortLabel.setBounds(20, 80, 110, 17);

        tunnelDestinationLabel.setText(resourceMap.getString("tunnelDestinationLabel.text")); // NOI18N
        tunnelDestinationLabel.setName("tunnelDestinationLabel"); // NOI18N
        getContentPane().add(tunnelDestinationLabel);
        tunnelDestinationLabel.setBounds(20, 110, 110, 17);

        tunnelProfileLabel.setText(resourceMap.getString("tunnelProfileLabel.text")); // NOI18N
        tunnelProfileLabel.setName("tunnelProfileLabel"); // NOI18N
        getContentPane().add(tunnelProfileLabel);
        tunnelProfileLabel.setBounds(20, 140, 110, 17);

        delayConnect.setText(resourceMap.getString("delayConnect.text")); // NOI18N
        delayConnect.setName("delayConnect"); // NOI18N
        getContentPane().add(delayConnect);
        delayConnect.setBounds(20, 170, 160, 22);

        sharedClient.setText(resourceMap.getString("sharedClient.text")); // NOI18N
        sharedClient.setName("sharedClient"); // NOI18N
        getContentPane().add(sharedClient);
        sharedClient.setBounds(20, 200, 160, 22);

        autoStart.setText(resourceMap.getString("autoStart.text")); // NOI18N
        autoStart.setName("autoStart"); // NOI18N
        getContentPane().add(autoStart);
        autoStart.setBounds(20, 230, 160, 22);

        jSeparator1.setName("jSeparator1"); // NOI18N
        getContentPane().add(jSeparator1);
        jSeparator1.setBounds(0, 510, 750, 10);

        tunnelDepthLabel.setText(resourceMap.getString("tunnelDepthLabel.text")); // NOI18N
        tunnelDepthLabel.setName("tunnelDepthLabel"); // NOI18N
        getContentPane().add(tunnelDepthLabel);
        tunnelDepthLabel.setBounds(20, 280, 160, 17);

        depthVarianceLabel.setText(resourceMap.getString("depthVarianceLabel.text")); // NOI18N
        depthVarianceLabel.setName("depthVarianceLabel"); // NOI18N
        getContentPane().add(depthVarianceLabel);
        depthVarianceLabel.setBounds(20, 310, 160, 17);

        tunnelCountLabel.setText(resourceMap.getString("tunnelCountLabel.text")); // NOI18N
        tunnelCountLabel.setName("tunnelCountLabel"); // NOI18N
        getContentPane().add(tunnelCountLabel);
        tunnelCountLabel.setBounds(20, 340, 160, 17);

        backupTunnelCountLabel.setText(resourceMap.getString("backupTunnelCountLabel.text")); // NOI18N
        backupTunnelCountLabel.setName("backupTunnelCountLabel"); // NOI18N
        getContentPane().add(backupTunnelCountLabel);
        backupTunnelCountLabel.setBounds(20, 370, 170, 17);

        jSeparator2.setName("jSeparator2"); // NOI18N
        getContentPane().add(jSeparator2);
        jSeparator2.setBounds(0, 260, 750, 10);

        reduceIdle.setText(resourceMap.getString("reduceIdle.text")); // NOI18N
        reduceIdle.setName("reduceIdle"); // NOI18N
        getContentPane().add(reduceIdle);
        reduceIdle.setBounds(20, 420, 300, 22);

        closeIdle.setText(resourceMap.getString("closeIdle.text")); // NOI18N
        closeIdle.setName("closeIdle"); // NOI18N
        getContentPane().add(closeIdle);
        closeIdle.setBounds(20, 450, 370, 22);

        delayIdle.setText(resourceMap.getString("delayIdle.text")); // NOI18N
        delayIdle.setName("delayIdle"); // NOI18N
        getContentPane().add(delayIdle);
        delayIdle.setBounds(20, 480, 400, 22);

        jSeparator3.setName("jSeparator3"); // NOI18N
        getContentPane().add(jSeparator3);
        jSeparator3.setBounds(0, 400, 760, 10);

        save.setText(resourceMap.getString("save.text")); // NOI18N
        save.setName("save"); // NOI18N
        save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });
        getContentPane().add(save);
        save.setBounds(10, 520, 44, 29);

        cancel.setText(resourceMap.getString("cancel.text")); // NOI18N
        cancel.setName("cancel"); // NOI18N
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });
        getContentPane().add(cancel);
        cancel.setBounds(60, 520, 55, 29);

        tunnelName.setText(resourceMap.getString("tunnelName.text")); // NOI18N
        tunnelName.setName("tunnelName"); // NOI18N
        getContentPane().add(tunnelName);
        tunnelName.setBounds(200, 20, 340, 27);

        tunnelType.setText(resourceMap.getString("tunnelType.text")); // NOI18N
        tunnelType.setName("tunnelType"); // NOI18N
        getContentPane().add(tunnelType);
        tunnelType.setBounds(200, 50, 340, 20);

        tunnelPort.setText(resourceMap.getString("tunnelPort.text")); // NOI18N
        tunnelPort.setName("tunnelPort"); // NOI18N
        getContentPane().add(tunnelPort);
        tunnelPort.setBounds(200, 70, 340, 27);

        tunnelDestination.setText(resourceMap.getString("tunnelDestination.text")); // NOI18N
        tunnelDestination.setName("tunnelDestination"); // NOI18N
        getContentPane().add(tunnelDestination);
        tunnelDestination.setBounds(200, 100, 340, 27);

        tunnelProfile.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Interactive connection (Instant messaging)", "Bulk connection (Downloads, websites...)" }));
        tunnelProfile.setName("tunnelProfile"); // NOI18N
        getContentPane().add(tunnelProfile);
        tunnelProfile.setBounds(200, 130, 340, 27);

        tunnelDepth.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0 hop tunnel (no anonymity, low latency)", "1 hop tunnel (low anonymity, low latency)", "2 hop tunnel (medium anonymity, medium latency)", "3 hop tunnel (high anonymity, high latency)" }));
        tunnelDepth.setName("tunnelDepth"); // NOI18N
        getContentPane().add(tunnelDepth);
        tunnelDepth.setBounds(200, 280, 350, 27);

        depthVariance.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0 hop variance (no random, good performance)", "+ 0-1 hop variance (slightly random, lower performance)", "+ 0-2 hop variance (very random, lower performance)", "+/- 0-1 hop variance (slightly random, standard performance)", "+/- 0-2 hop variance (not recommended)" }));
        depthVariance.setName("depthVariance"); // NOI18N
        getContentPane().add(depthVariance);
        depthVariance.setBounds(200, 310, 350, 27);

        tunnelCount.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1 tunnel (low bandwidth usage, low reliability)", "2 tunnels (standard bandwidth usage, standard reliability)", "3 tunnels (high bandwidth usage, high reliability)" }));
        tunnelCount.setName("tunnelCount"); // NOI18N
        getContentPane().add(tunnelCount);
        tunnelCount.setBounds(200, 340, 350, 27);

        backupTunnelCount.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0 backup tunnels (no redundancy, no resource usage)", "1 backup tunnel (low redundancy, low resource usage)", "2 backup tunnels (medium redundancy, medium resource usage)", "3 backup tunnels (high redundancy, high resource usage)" }));
        backupTunnelCount.setName("backupTunnelCount"); // NOI18N
        getContentPane().add(backupTunnelCount);
        backupTunnelCount.setBounds(200, 370, 350, 27);

        changeTunnelState.setText(resourceMap.getString("changeTunnelState.text")); // NOI18N
        changeTunnelState.setName("changeTunnelState"); // NOI18N
        getContentPane().add(changeTunnelState);
        changeTunnelState.setBounds(160, 520, 150, 29);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
        EditBean bean = new EditBean();
        if(!bean.isClient(tunnelNumber)) {
            al.actionPerformed(evt);
            this.dispose();
        }
        else {
            bean.setTunnel("" + tunnelNumber);
            bean.setName(tunnelName.getText());
            bean.setPort(tunnelPort.getText());
            bean.setTargetDestination(tunnelDestination.getText());
            if(!bean.getTunnelType(tunnelNumber).equals(TYPE_STREAMR_CLIENT)) {
                if(tunnelProfile.getSelectedIndex() == TUNNEL_INTERACTIVE) {
                    bean.setProfile("interactive");
                }
                else {
                    bean.setProfile("bulk");
                }

                if(delayConnect.isSelected()) {
                    bean.setConnectDelay("true");
                }
                else {
                    bean.setConnectDelay("false");
                }

                if(sharedClient.isSelected()) {
                    bean.setShared(true);
                }
                else {
                    bean.setShared(false);
                }

                if(autoStart.isSelected()) {
                    bean.setStartOnLoad("true");
                }
                else {
                }
            }
            bean.setTunnelDepth("" + tunnelDepth.getSelectedIndex());

            int variance = depthVariance.getSelectedIndex();
            if(variance >= 0 && variance <= 2) {
                bean.setTunnelVariance("" + variance);
            }
            else if(variance == 3) {
                bean.setTunnelVariance("-1");
            }
            else if(variance == 4) {
                bean.setTunnelVariance("-2");
            }

            bean.setTunnelQuantity("" + tunnelCount.getSelectedIndex() + 1);

            bean.setTunnelBackupQuantity("" + backupTunnelCount.getSelectedIndex());

            if(!bean.getTunnelType(tunnelNumber).equals(TYPE_STREAMR_CLIENT)) {
                if(reduceIdle.isSelected()) {
                    bean.setReduce("true");
                }
                else {
                }

                if(closeIdle.isSelected()) {
                    bean.setClose("true");
                }
                else {
                }

                if(delayIdle.isSelected()) {
                    bean.setDelayOpen("true");
                }
            }
            
        }
        
        al.actionPerformed(evt);
        this.dispose();
}//GEN-LAST:event_saveActionPerformed

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        al.actionPerformed(evt);
        this.dispose();
    }//GEN-LAST:event_cancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox autoStart;
    private javax.swing.JComboBox backupTunnelCount;
    private javax.swing.JLabel backupTunnelCountLabel;
    private javax.swing.JButton cancel;
    private javax.swing.JButton changeTunnelState;
    private javax.swing.JCheckBox closeIdle;
    private javax.swing.JCheckBox delayConnect;
    private javax.swing.JCheckBox delayIdle;
    private javax.swing.JComboBox depthVariance;
    private javax.swing.JLabel depthVarianceLabel;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JCheckBox reduceIdle;
    private javax.swing.JButton save;
    private javax.swing.JCheckBox sharedClient;
    private javax.swing.JComboBox tunnelCount;
    private javax.swing.JLabel tunnelCountLabel;
    private javax.swing.JComboBox tunnelDepth;
    private javax.swing.JLabel tunnelDepthLabel;
    private javax.swing.JTextField tunnelDestination;
    private javax.swing.JLabel tunnelDestinationLabel;
    private javax.swing.JTextField tunnelName;
    private javax.swing.JLabel tunnelNameLabel;
    private javax.swing.JTextField tunnelPort;
    private javax.swing.JLabel tunnelPortLabel;
    private javax.swing.JComboBox tunnelProfile;
    private javax.swing.JLabel tunnelProfileLabel;
    private javax.swing.JLabel tunnelType;
    private javax.swing.JLabel tunnelTypeLabel;
    // End of variables declaration//GEN-END:variables
    private int tunnelNumber;
    private ActionListener al;
    private static final int TUNNEL_INTERACTIVE = 0;
    private static final int TUNNEL_BULK = 1;
    private static final String TYPE_STREAMR_CLIENT = "Streamr client";
}
