/*
 * ProfileSelector.java
 *
 * Created on 3 april 2009, 13:57
 */

package net.i2p.desktopgui.gui;

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.util.IntegerVerifier;

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

    /** Creates new form ProfileSelector */
    public SpeedSelector() {
        this.props = PropertyManager.getProps();
        initComponents();
        initComponentsCustom();
        initSpeeds(props);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        this.requestFocus();
    }
    
    public SpeedSelector(Point point) {
        this();
        this.setLocation(point);
    }
    
    public void initComponentsCustom() {
        ((JTextField)uploadChoice.getEditor().getEditorComponent()).setInputVerifier(new IntegerVerifier()); 
        ((JTextField)downloadChoice.getEditor().getEditorComponent()).setInputVerifier(new IntegerVerifier()); 
    }

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

        nextButton = new javax.swing.JButton();
        uploadLabel = new javax.swing.JLabel();
        downloadLabel = new javax.swing.JLabel();
        uploadChoice = new javax.swing.JComboBox();
        downloadChoice = new javax.swing.JComboBox();
        speedExplanation = new javax.swing.JLabel();
        uploadkbps = new javax.swing.JComboBox();
        downloadkbps = new javax.swing.JComboBox();

        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(SpeedSelector.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setMinimumSize(new java.awt.Dimension(610, 330));
        setName("Form"); // NOI18N
        setResizable(false);
        getContentPane().setLayout(null);

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

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

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

        uploadChoice.setEditable(true);
        uploadChoice.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "100", "200", "500", "1000", "2000", "4000", "8000", "10000", "20000", "50000", "100000" }));
        uploadChoice.setSelectedIndex(3);
        uploadChoice.setName("uploadChoice"); // NOI18N
        getContentPane().add(uploadChoice);
        uploadChoice.setBounds(300, 60, 154, 27);

        downloadChoice.setEditable(true);
        downloadChoice.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "100", "200", "500", "1000", "2000", "4000", "8000", "10000", "20000", "50000", "100000" }));
        downloadChoice.setSelectedIndex(3);
        downloadChoice.setName("downloadChoice"); // NOI18N
        getContentPane().add(downloadChoice);
        downloadChoice.setBounds(300, 110, 154, 27);

        speedExplanation.setText(resourceMap.getString("speedExplanation.text")); // NOI18N
        speedExplanation.setName("speedExplanation"); // NOI18N
        getContentPane().add(speedExplanation);
        speedExplanation.setBounds(20, 160, 570, 60);

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

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

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

private void nextButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nextButtonMouseClicked
    if(uploadkbps.getSelectedIndex() == KILOBIT)
        props.setProperty(SpeedSelectorConstants.MAXUPLOADCAPABLE, uploadChoice.getSelectedItem().toString());
    else
        props.setProperty(SpeedSelectorConstants.MAXUPLOADCAPABLE, "" + Integer.parseInt(uploadChoice.getSelectedItem().toString())*8);
    if(downloadkbps.getSelectedIndex() == KILOBIT)
        props.setProperty(SpeedSelectorConstants.MAXDOWNLOADCAPABLE, downloadChoice.getSelectedItem().toString());
    else
        props.setProperty(SpeedSelectorConstants.MAXDOWNLOADCAPABLE, "" + Integer.parseInt(downloadChoice.getSelectedItem().toString())*8);
    PropertyManager.saveProps(props);
    new SpeedSelector2(this.getLocationOnScreen());
    this.dispose();
}//GEN-LAST:event_nextButtonMouseClicked

private void uploadkbpsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_uploadkbpsActionPerformed
    kbpsSwitchPerformed(uploadkbps, uploadChoice);
}//GEN-LAST:event_uploadkbpsActionPerformed

private void downloadkbpsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downloadkbpsActionPerformed
    kbpsSwitchPerformed(downloadkbps, downloadChoice);
}//GEN-LAST:event_downloadkbpsActionPerformed

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

private void initSpeeds(Properties props) {
    String up = props.getProperty(SpeedSelectorConstants.MAXUPLOADCAPABLE);
    String down = props.getProperty(SpeedSelectorConstants.MAXDOWNLOADCAPABLE);
    
    if(up == null)
        props.setProperty(SpeedSelectorConstants.MAXUPLOADCAPABLE, "1000");
    if(down == null)
        props.setProperty(SpeedSelectorConstants.MAXDOWNLOADCAPABLE, "1000");
    
    uploadChoice.setSelectedItem(props.getProperty(SpeedSelectorConstants.MAXUPLOADCAPABLE));
    downloadChoice.setSelectedItem(props.getProperty(SpeedSelectorConstants.MAXDOWNLOADCAPABLE));
}


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox downloadChoice;
    private javax.swing.JLabel downloadLabel;
    private javax.swing.JComboBox downloadkbps;
    private javax.swing.JButton nextButton;
    private javax.swing.JLabel speedExplanation;
    private javax.swing.JComboBox uploadChoice;
    private javax.swing.JLabel uploadLabel;
    private javax.swing.JComboBox uploadkbps;
    // End of variables declaration//GEN-END:variables

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