package com.fitbank.fitpatch;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import com.fitbank.util.SwingUtils;

/**
 * Diálogo de preferencias.
 *
 * @author FitBank CI
 */
public class PreferencesDialog extends javax.swing.JDialog {

    public PreferencesDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        SwingUtils.load(PatchHandler.class, controlSources, mantis,
                username, privateKey, passphrase);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        controlFileChooser = new JFileChooser();
        privateKeyFileChooser = new JFileChooser();
        javax.swing.JPanel preferencesPanel = new javax.swing.JPanel();
        javax.swing.JLabel mantisLabel = new javax.swing.JLabel();
        mantis = new javax.swing.JTextField();
        javax.swing.JLabel controlSourcesLabel = new javax.swing.JLabel();
        controlSources = new javax.swing.JTextField();
        javax.swing.JButton controlSourcesBrowse = new javax.swing.JButton();
        javax.swing.JLabel usernameLabel = new javax.swing.JLabel();
        username = new javax.swing.JTextField();
        javax.swing.JLabel passphraseLabel = new javax.swing.JLabel();
        passphrase = new javax.swing.JPasswordField();
        javax.swing.JLabel privateKeyLabel = new javax.swing.JLabel();
        privateKey = new javax.swing.JTextField();
        javax.swing.JButton privateKeyBrowse = new javax.swing.JButton();
        javax.swing.JLabel portLabel = new javax.swing.JLabel();
        javax.swing.JSpinner port = new javax.swing.JSpinner();
        javax.swing.JButton okButton = new javax.swing.JButton();

        controlFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        controlFileChooser.setName("controlFileChooser"); // NOI18N

        privateKeyFileChooser.setName("privateKeyFileChooser"); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N
        setResizable(false);

        preferencesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1), "Preferencias"));
        preferencesPanel.setName("preferencesPanel"); // NOI18N

        mantisLabel.setText("Base de datos Mantis:");
        mantisLabel.setName("mantisLabel"); // NOI18N

        mantis.setText("192.168.1.13:3306");
        mantis.setName("mantis"); // NOI18N

        controlSourcesLabel.setText("Fuentes control:");
        controlSourcesLabel.setName("controlSourcesLabel"); // NOI18N

        controlSources.setName("controlSources"); // NOI18N

        controlSourcesBrowse.setText("Examinar...");
        controlSourcesBrowse.setName("controlSourcesBrowse"); // NOI18N
        controlSourcesBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                controlSourcesBrowseActionPerformed();
            }
        });

        usernameLabel.setText("Usuario");
        usernameLabel.setName("usernameLabel"); // NOI18N

        username.setName("username"); // NOI18N

        passphraseLabel.setText("Contraseña de la llave:");
        passphraseLabel.setName("passphraseLabel"); // NOI18N

        passphrase.setName("passphrase"); // NOI18N

        privateKeyLabel.setText("Llave privada:");
        privateKeyLabel.setName("privateKeyLabel"); // NOI18N

        privateKey.setName("privateKey"); // NOI18N

        privateKeyBrowse.setText("Examinar...");
        privateKeyBrowse.setName("privateKeyBrowse"); // NOI18N
        privateKeyBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                privateKeyBrowseActionPerformed();
            }
        });

        portLabel.setText("Puerto:");
        portLabel.setName("portLabel"); // NOI18N

        port.setName("port"); // NOI18N
        port.setValue(22);

        javax.swing.GroupLayout preferencesPanelLayout = new javax.swing.GroupLayout(preferencesPanel);
        preferencesPanel.setLayout(preferencesPanelLayout);
        preferencesPanelLayout.setHorizontalGroup(
            preferencesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(preferencesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(preferencesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(passphraseLabel)
                    .addComponent(mantisLabel)
                    .addComponent(controlSourcesLabel)
                    .addComponent(usernameLabel)
                    .addComponent(privateKeyLabel)
                    .addComponent(portLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(preferencesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(preferencesPanelLayout.createSequentialGroup()
                        .addComponent(controlSources, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(controlSourcesBrowse))
                    .addComponent(mantis, javax.swing.GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE)
                    .addComponent(username, javax.swing.GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE)
                    .addGroup(preferencesPanelLayout.createSequentialGroup()
                        .addComponent(privateKey, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(privateKeyBrowse))
                    .addComponent(passphrase, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE)
                    .addComponent(port, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        preferencesPanelLayout.setVerticalGroup(
            preferencesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(preferencesPanelLayout.createSequentialGroup()
                .addGroup(preferencesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(controlSourcesLabel)
                    .addComponent(controlSources, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(controlSourcesBrowse))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(preferencesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(mantisLabel)
                    .addComponent(mantis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(preferencesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usernameLabel)
                    .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(preferencesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(privateKeyLabel)
                    .addComponent(privateKey, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(privateKeyBrowse))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(preferencesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passphraseLabel)
                    .addComponent(passphrase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(preferencesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(portLabel)
                    .addComponent(port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        okButton.setText("Guardar");
        okButton.setName("okButton"); // NOI18N
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed();
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(preferencesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(okButton)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(preferencesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(okButton)
                .addContainerGap())
        );

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

    private void okButtonActionPerformed() {//GEN-FIRST:event_okButtonActionPerformed
        SwingUtils.save(PatchHandler.class, controlSources, mantis,
                username, privateKey, passphrase);
        this.setVisible(false);
        JOptionPane.showMessageDialog(this,
                "Por favor reinicie fitpatch para aplicar los cambios");
    }//GEN-LAST:event_okButtonActionPerformed

    private void controlSourcesBrowseActionPerformed() {//GEN-FIRST:event_controlSourcesBrowseActionPerformed
        controlFileChooser.setSelectedFile(new File(controlSources.getText()));
        if (controlFileChooser.showOpenDialog(this)
                == JFileChooser.APPROVE_OPTION) {
            controlSources.setText(
                    controlFileChooser.getSelectedFile().getAbsolutePath());
        }
    }//GEN-LAST:event_controlSourcesBrowseActionPerformed

    private void privateKeyBrowseActionPerformed() {//GEN-FIRST:event_privateKeyBrowseActionPerformed
        privateKeyFileChooser.setSelectedFile(new File(privateKey.getText()));
        if (privateKeyFileChooser.showOpenDialog(this)
                == JFileChooser.APPROVE_OPTION) {
            privateKey.setText(
                    privateKeyFileChooser.getSelectedFile().getAbsolutePath());
        }
    }//GEN-LAST:event_privateKeyBrowseActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JFileChooser controlFileChooser;
    private javax.swing.JTextField controlSources;
    private javax.swing.JTextField mantis;
    private javax.swing.JPasswordField passphrase;
    private javax.swing.JTextField privateKey;
    private JFileChooser privateKeyFileChooser;
    private javax.swing.JTextField username;
    // End of variables declaration//GEN-END:variables

}
