/*
 * ProfileSelector3.java
 *
 * Created on 3 april 2009, 15:17
 */

package net.i2p.desktopgui.gui;

import java.awt.Dimension;
import java.awt.Point;
import java.util.Properties;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import net.i2p.desktopgui.persistence.PropertyManager;
import net.i2p.desktopgui.router.configuration.SpeedHandler;
import net.i2p.desktopgui.router.configuration.SpeedHelper;

/**
 *
 * @author  mathias
 */
public class SpeedSelector3 extends javax.swing.JFrame {
    Properties props;

    /** Creates new form ProfileSelector3 */
    public SpeedSelector3(Point point, Dimension dimension) {
        this.props = PropertyManager.getProps();
        initComponents();
        this.setLocation(point);
        this.setSize(dimension);
        initSpeeds();
        initUsage();
        this.setVisible(true);
        this.requestFocus();
    }

    /** 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() {

        finishButton = new javax.swing.JButton();
        previousButton = new javax.swing.JButton();
        settingsInfo = new javax.swing.JLabel();
        uploadLabel = new javax.swing.JLabel();
        downloadLabel = new javax.swing.JLabel();
        uploadBurstLabel = new javax.swing.JLabel();
        downloadBurstLabel = new javax.swing.JLabel();
        uploadUsageLabel = new javax.swing.JLabel();
        downloadUsageLabel = new javax.swing.JLabel();
        uploadField = new javax.swing.JTextField();
        uploadBurstField = new javax.swing.JTextField();
        downloadField = new javax.swing.JTextField();
        downloadBurstField = new javax.swing.JTextField();
        kbpsBurstDownload = new javax.swing.JComboBox();
        kbpsUpload = new javax.swing.JComboBox();
        kbpsBurstUpload = new javax.swing.JComboBox();
        kbpsDownload = new javax.swing.JComboBox();
        uploadGB = new javax.swing.JLabel();
        uploadMonth = new javax.swing.JTextField();
        downloadMonth = new javax.swing.JTextField();
        downloadGB = new javax.swing.JLabel();
        explanation = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(net.i2p.desktopgui.desktopgui.Main.class).getContext().getResourceMap(SpeedSelector3.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setMinimumSize(new java.awt.Dimension(670, 330));
        setName("Form"); // NOI18N
        setResizable(false);
        getContentPane().setLayout(null);

        finishButton.setText(resourceMap.getString("finishButton.text")); // NOI18N
        finishButton.setName("finishButton"); // NOI18N
        finishButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                finishButtonMouseClicked(evt);
            }
        });
        getContentPane().add(finishButton);
        finishButton.setBounds(440, 250, 90, 29);

        previousButton.setText(resourceMap.getString("previousButton.text")); // NOI18N
        previousButton.setName("previousButton"); // NOI18N
        previousButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                previousButtonMouseClicked(evt);
            }
        });
        getContentPane().add(previousButton);
        previousButton.setBounds(336, 250, 90, 29);

        settingsInfo.setText(resourceMap.getString("settingsInfo.text")); // NOI18N
        settingsInfo.setName("settingsInfo"); // NOI18N
        getContentPane().add(settingsInfo);
        settingsInfo.setBounds(20, 30, 532, 17);

        uploadLabel.setText(resourceMap.getString("uploadLabel.text")); // NOI18N
        uploadLabel.setName("uploadLabel"); // NOI18N
        getContentPane().add(uploadLabel);
        uploadLabel.setBounds(20, 70, 140, 30);

        downloadLabel.setText(resourceMap.getString("downloadLabel.text")); // NOI18N
        downloadLabel.setName("downloadLabel"); // NOI18N
        getContentPane().add(downloadLabel);
        downloadLabel.setBounds(340, 70, 160, 30);

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

        downloadBurstLabel.setText(resourceMap.getString("downloadBurstLabel.text")); // NOI18N
        downloadBurstLabel.setName("downloadBurstLabel"); // NOI18N
        getContentPane().add(downloadBurstLabel);
        downloadBurstLabel.setBounds(340, 110, 160, 30);

        uploadUsageLabel.setText(resourceMap.getString("uploadUsageLabel.text")); // NOI18N
        uploadUsageLabel.setName("uploadUsageLabel"); // NOI18N
        getContentPane().add(uploadUsageLabel);
        uploadUsageLabel.setBounds(20, 150, 140, 30);

        downloadUsageLabel.setText(resourceMap.getString("downloadUsageLabel.text")); // NOI18N
        downloadUsageLabel.setName("downloadUsageLabel"); // NOI18N
        getContentPane().add(downloadUsageLabel);
        downloadUsageLabel.setBounds(340, 150, 160, 30);

        uploadField.setText(resourceMap.getString("uploadField.text")); // NOI18N
        uploadField.setMinimumSize(new java.awt.Dimension(77, 27));
        uploadField.setName("uploadField"); // NOI18N
        uploadField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                speedFieldKeyReleased(evt);
            }
        });
        getContentPane().add(uploadField);
        uploadField.setBounds(160, 70, 77, 27);

        uploadBurstField.setText(resourceMap.getString("uploadBurstField.text")); // NOI18N
        uploadBurstField.setMinimumSize(new java.awt.Dimension(77, 27));
        uploadBurstField.setName("uploadBurstField"); // NOI18N
        getContentPane().add(uploadBurstField);
        uploadBurstField.setBounds(160, 110, 77, 27);

        downloadField.setText(resourceMap.getString("downloadField.text")); // NOI18N
        downloadField.setMinimumSize(new java.awt.Dimension(77, 27));
        downloadField.setName("downloadField"); // NOI18N
        downloadField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                speedFieldKeyReleased(evt);
            }
        });
        getContentPane().add(downloadField);
        downloadField.setBounds(500, 70, 77, 27);

        downloadBurstField.setText(resourceMap.getString("downloadBurstField.text")); // NOI18N
        downloadBurstField.setMinimumSize(new java.awt.Dimension(77, 27));
        downloadBurstField.setName("downloadBurstField"); // NOI18N
        getContentPane().add(downloadBurstField);
        downloadBurstField.setBounds(500, 110, 77, 27);

        kbpsBurstDownload.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "kbps", "kBps" }));
        kbpsBurstDownload.setName("kbpsBurstDownload"); // NOI18N
        kbpsBurstDownload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kbpsBurstDownloadActionPerformed(evt);
            }
        });
        getContentPane().add(kbpsBurstDownload);
        kbpsBurstDownload.setBounds(580, 110, 68, 27);

        kbpsUpload.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "kbps", "kBps" }));
        kbpsUpload.setName("kbpsUpload"); // NOI18N
        kbpsUpload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kbpsUploadActionPerformed(evt);
            }
        });
        getContentPane().add(kbpsUpload);
        kbpsUpload.setBounds(240, 70, 68, 27);

        kbpsBurstUpload.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "kbps", "kBps" }));
        kbpsBurstUpload.setName("kbpsBurstUpload"); // NOI18N
        kbpsBurstUpload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kbpsBurstUploadActionPerformed(evt);
            }
        });
        getContentPane().add(kbpsBurstUpload);
        kbpsBurstUpload.setBounds(240, 110, 68, 27);

        kbpsDownload.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "kbps", "kBps" }));
        kbpsDownload.setName("kbpsDownload"); // NOI18N
        kbpsDownload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kbpsDownloadActionPerformed(evt);
            }
        });
        getContentPane().add(kbpsDownload);
        kbpsDownload.setBounds(580, 70, 68, 27);

        uploadGB.setText(resourceMap.getString("uploadUsageLabel.text")); // NOI18N
        uploadGB.setName("uploadUsageLabel"); // NOI18N
        getContentPane().add(uploadGB);
        uploadGB.setBounds(240, 150, 45, 30);

        uploadMonth.setText(resourceMap.getString("uploadMonth.text")); // NOI18N
        uploadMonth.setName("uploadMonth"); // NOI18N
        uploadMonth.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                monthKeyReleased(evt);
            }
        });
        getContentPane().add(uploadMonth);
        uploadMonth.setBounds(160, 150, 77, 27);

        downloadMonth.setText(resourceMap.getString("downloadMonth.text")); // NOI18N
        downloadMonth.setName("downloadMonth"); // NOI18N
        downloadMonth.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                monthKeyReleased(evt);
            }
        });
        getContentPane().add(downloadMonth);
        downloadMonth.setBounds(500, 150, 77, 27);

        downloadGB.setText(resourceMap.getString("downloadUsageLabel.text")); // NOI18N
        downloadGB.setName("downloadUsageLabel"); // NOI18N
        getContentPane().add(downloadGB);
        downloadGB.setBounds(580, 150, 40, 30);

        explanation.setText(resourceMap.getString("explanation.text")); // NOI18N
        explanation.setName("explanation"); // NOI18N
        getContentPane().add(explanation);
        explanation.setBounds(20, 180, 600, 70);

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

private void previousButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_previousButtonMouseClicked
    saveSpeeds();
    PropertyManager.saveProps(props);
    new SpeedSelector2(this.getLocationOnScreen());
    this.dispose();
}//GEN-LAST:event_previousButtonMouseClicked

private void finishButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_finishButtonMouseClicked
    saveSpeeds();
    PropertyManager.saveProps(props);
    
    int maxDownload = Integer.parseInt(props.getProperty(SpeedSelectorConstants.MAXDOWNLOAD));
    int maxUpload = Integer.parseInt(props.getProperty(SpeedSelectorConstants.MAXUPLOAD));
    int maxUploadBurst = Integer.parseInt(props.getProperty(SpeedSelectorConstants.MAXUPLOADBURST));
    int maxDownloadBurst = Integer.parseInt(props.getProperty(SpeedSelectorConstants.MAXDOWNLOADBURST));
    
     //Working in kB, not kb!
    SpeedHandler.setInboundBandwidth(maxDownload/8);
    SpeedHandler.setOutboundBandwidth(maxUpload/8);
    SpeedHandler.setInboundBurstBandwidth(maxDownloadBurst/8);
    SpeedHandler.setOutboundBurstBandwidth(maxUploadBurst/8);
    
    this.dispose();
}//GEN-LAST:event_finishButtonMouseClicked

private void speedFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_speedFieldKeyReleased
    try {
        String upload = "";
        if(kbpsUpload.getSelectedIndex() == KILOBIT)
            upload = uploadField.getText();
        else
            upload = "" + Integer.parseInt(uploadField.getText())*8;
        String download = "";
        if(kbpsDownload.getSelectedIndex() == KILOBIT)
            download = downloadField.getText();
        else
            download = "" + Integer.parseInt(downloadField.getText())*8;
        initUsage(upload, download);
    }
    catch(NumberFormatException e) {
        return;
    }
}//GEN-LAST:event_speedFieldKeyReleased

private void kbpsUploadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kbpsUploadActionPerformed
    kbpsSwitchPerformed(kbpsUpload, uploadField);
}//GEN-LAST:event_kbpsUploadActionPerformed

private void kbpsBurstUploadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kbpsBurstUploadActionPerformed
    kbpsSwitchPerformed(kbpsBurstUpload, uploadBurstField);
}//GEN-LAST:event_kbpsBurstUploadActionPerformed

private void kbpsDownloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kbpsDownloadActionPerformed
    kbpsSwitchPerformed(kbpsDownload, downloadField);
}//GEN-LAST:event_kbpsDownloadActionPerformed

private void kbpsBurstDownloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kbpsBurstDownloadActionPerformed
    kbpsSwitchPerformed(kbpsBurstDownload, downloadBurstField);
}//GEN-LAST:event_kbpsBurstDownloadActionPerformed

private void monthKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_monthKeyReleased
    try {
        int uploadMonthValue = Integer.parseInt(uploadMonth.getText());
        int downloadMonthValue = Integer.parseInt(downloadMonth.getText());
        
        String upload = "";
        String burstUpload = "";
        String download = "";
        String burstDownload = "";
        
        if(kbpsUpload.getSelectedIndex() == KILOBIT)
            upload = "" + SpeedHelper.calculateSpeed(uploadMonthValue)*8; //kbit
        else
            upload = "" + SpeedHelper.calculateSpeed(uploadMonthValue); //kbyte
        
        if(kbpsBurstUpload.getSelectedIndex() == KILOBIT)
            burstUpload = "" + SpeedHelper.calculateSpeed(uploadMonthValue)*8; //kbit
        else
            burstUpload = "" + SpeedHelper.calculateSpeed(uploadMonthValue); //kbyte
        
        if(kbpsDownload.getSelectedIndex() == KILOBIT)
            download = "" + SpeedHelper.calculateSpeed(downloadMonthValue)*8; //kbit
        else
            download = "" + SpeedHelper.calculateSpeed(downloadMonthValue); //kbyte
        
        if(kbpsBurstDownload.getSelectedIndex() == KILOBIT)
            burstDownload = "" + SpeedHelper.calculateSpeed(downloadMonthValue)*8; //kbit
        else
            burstDownload = "" + SpeedHelper.calculateSpeed(downloadMonthValue); //kbyte
        
        initSpeeds(upload, burstUpload, download, burstDownload);
    }
    catch(NumberFormatException e) {
        e.printStackTrace();
        return;
    }
}//GEN-LAST:event_monthKeyReleased

private void kbpsSwitchPerformed(JComboBox kbps, JTextField speed) {
    int index = kbps.getSelectedIndex();
    int previous = Integer.parseInt(speed.getText());
    if(index == KILOBIT) {
        speed.setText("" + previous*8);
    }
    else {
        speed.setText("" + previous/8);
    }
}

    protected void initSpeeds() {
        String up = "" + SpeedHelper.calculateSpeed(
                props.getProperty(SpeedSelectorConstants.MAXUPLOADCAPABLE), props.getProperty(SpeedSelectorConstants.USERTYPE));
        String upBurst = "" + SpeedHelper.calculateSpeed(
                props.getProperty(SpeedSelectorConstants.MAXUPLOADCAPABLE), props.getProperty(SpeedSelectorConstants.USERTYPE));
        String down = "" + SpeedHelper.calculateSpeed(
                props.getProperty(SpeedSelectorConstants.MAXDOWNLOADCAPABLE), props.getProperty(SpeedSelectorConstants.USERTYPE));
        String downBurst = "" + SpeedHelper.calculateSpeed(
                props.getProperty(SpeedSelectorConstants.MAXDOWNLOADCAPABLE), props.getProperty(SpeedSelectorConstants.USERTYPE));
        String userType = props.getProperty(SpeedSelectorConstants.USERTYPE);
        
        initSpeeds(up, upBurst, down, downBurst);
    }
    
    protected void initSpeeds(String up, String upBurst, String down, String downBurst) {
        uploadField.setText(up);
        uploadBurstField.setText(upBurst);
        downloadField.setText(down);
        downloadBurstField.setText(downBurst);
    }
    
    protected void saveSpeeds() {
        if(kbpsUpload.getSelectedIndex() == KILOBIT)
            props.setProperty(SpeedSelectorConstants.MAXUPLOAD, uploadField.getText());
        else
            props.setProperty(SpeedSelectorConstants.MAXUPLOAD, "" + Integer.parseInt(uploadField.getText())*8);
        if(kbpsBurstUpload.getSelectedIndex() == KILOBIT)
            props.setProperty(SpeedSelectorConstants.MAXUPLOADBURST, uploadBurstField.getText());
        else
            props.setProperty(SpeedSelectorConstants.MAXUPLOADBURST, "" + Integer.parseInt(uploadBurstField.getText())*8);
        if(kbpsDownload.getSelectedIndex() == KILOBIT)
            props.setProperty(SpeedSelectorConstants.MAXDOWNLOAD, downloadField.getText());
        else
            props.setProperty(SpeedSelectorConstants.MAXDOWNLOAD, "" + Integer.parseInt(downloadField.getText())*8);
        if(kbpsBurstDownload.getSelectedIndex() == KILOBIT)
            props.setProperty(SpeedSelectorConstants.MAXDOWNLOADBURST, downloadBurstField.getText());
        else
            props.setProperty(SpeedSelectorConstants.MAXDOWNLOADBURST, "" + Integer.parseInt(downloadBurstField.getText())*8);
    }
    
    protected void initUsage(String upload, String download) {
        uploadMonth.setText("" + SpeedHelper.calculateMonthlyUsage(Integer.parseInt(upload)/8));
        downloadMonth.setText("" + SpeedHelper.calculateMonthlyUsage(Integer.parseInt(download)/8));
    }
    
    protected void initUsage() {
        String upload = "";
        if(kbpsUpload.getSelectedIndex() == KILOBIT)
            upload = uploadField.getText();
        else
            upload = "" + Integer.parseInt(uploadField.getText())/8;
        String download = "";
        if(kbpsDownload.getSelectedIndex() == KILOBIT)
            download = downloadField.getText();
        else
            download = "" + Integer.parseInt(downloadField.getText())/8;
        initUsage(upload, download);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField downloadBurstField;
    private javax.swing.JLabel downloadBurstLabel;
    private javax.swing.JTextField downloadField;
    private javax.swing.JLabel downloadGB;
    private javax.swing.JLabel downloadLabel;
    private javax.swing.JTextField downloadMonth;
    private javax.swing.JLabel downloadUsageLabel;
    private javax.swing.JLabel explanation;
    private javax.swing.JButton finishButton;
    private javax.swing.JComboBox kbpsBurstDownload;
    private javax.swing.JComboBox kbpsBurstUpload;
    private javax.swing.JComboBox kbpsDownload;
    private javax.swing.JComboBox kbpsUpload;
    private javax.swing.JButton previousButton;
    private javax.swing.JLabel settingsInfo;
    private javax.swing.JTextField uploadBurstField;
    private javax.swing.JLabel uploadBurstLabel;
    private javax.swing.JTextField uploadField;
    private javax.swing.JLabel uploadGB;
    private javax.swing.JLabel uploadLabel;
    private javax.swing.JTextField uploadMonth;
    private javax.swing.JLabel uploadUsageLabel;
    // End of variables declaration//GEN-END:variables

    private static final int KILOBIT = 0;
    private static final int KILOBYTE = 1;
}
