/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DatiClienteV.java
 *
 * Created on 9-lug-2011, 22.36.48
 */

package app.gui;

import archive.data.Carta;
import archive.data.Cliente;
import archive.data.Volo;
import archive.interfacce.RichiestaPrenotazioneVolo;
import archive.system.AgenziaSystem;
import archive.system.PRequestVoloManager;
import javax.swing.JOptionPane;

public class DatiClienteV extends javax.swing.JFrame {

    /** Creates new form DatiClienteV */
    public DatiClienteV() {
        initComponents();
    }

    /** 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() {
        java.awt.GridBagConstraints gridBagConstraints;

        jTFCognome = new javax.swing.JTextField();
        jTFNome = new javax.swing.JTextField();
        jTFTel = new javax.swing.JTextField();
        jTFTCarta = new javax.swing.JTextField();
        jTFNCarta = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jBPrenota = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jTFCognome.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTFCognomeKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 95;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(40, 4, 0, 0);
        getContentPane().add(jTFCognome, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 95;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(18, 4, 0, 0);
        getContentPane().add(jTFNome, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 95;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(18, 4, 0, 0);
        getContentPane().add(jTFTel, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 95;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(11, 4, 0, 0);
        getContentPane().add(jTFTCarta, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 95;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(11, 4, 0, 0);
        getContentPane().add(jTFNCarta, gridBagConstraints);

        jLabel1.setText("Cognome Cliente");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(43, 91, 0, 0);
        getContentPane().add(jLabel1, gridBagConstraints);

        jLabel2.setText("Nome Cliente");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(21, 105, 0, 0);
        getContentPane().add(jLabel2, gridBagConstraints);

        jLabel3.setText("Numero di Telefono");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(21, 81, 0, 0);
        getContentPane().add(jLabel3, gridBagConstraints);

        jLabel4.setText("Tipo Carta di Credito");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(14, 75, 0, 0);
        getContentPane().add(jLabel4, gridBagConstraints);

        jLabel5.setText("Numero Carta di Credito");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(14, 52, 0, 0);
        getContentPane().add(jLabel5, gridBagConstraints);

        jBPrenota.setText("Prenota");
        jBPrenota.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBPrenotaActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(32, 68, 47, 87);
        getContentPane().add(jBPrenota, gridBagConstraints);

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

    private void jTFCognomeKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTFCognomeKeyReleased
        Cliente cliente = AgenziaSystem.archivio.getCliente(jTFCognome.getText());
        if(cliente!=null){
            jTFNome.setText(cliente.getNome());
            jTFTel.setText(cliente.getTelefono());
            jTFTCarta.setText(cliente.getCarta().getTipo());
            jTFNCarta.setText(cliente.getCarta().getNumero());

        }
}//GEN-LAST:event_jTFCognomeKeyReleased

    private void jBPrenotaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBPrenotaActionPerformed
        RichiestaPrenotazioneVolo prenV=new PRequestVoloManager();
        String cognome=jTFCognome.getText();
        String nome=jTFNome.getText();
        String telefono=jTFTel.getText();
        String carta=jTFTCarta.getText()+jTFNCarta.getText();
        Volo volo=prenV.isAvaible(RPrenotazioneV.jTFLPartenza.getText(),RPrenotazioneV.jTFLArrivo.getText(), RPrenotazioneV.jTFOrario.getText());
        try{
        if(prenV.doPrenotation(volo, cognome, nome,telefono, new Carta(carta))){
            JOptionPane.showMessageDialog(rootPane, "Prenotazione effetuata");
        }
        }catch(IllegalArgumentException e){
            JOptionPane.showMessageDialog(rootPane, "Carta non Valida","",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jBPrenotaActionPerformed

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBPrenota;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField jTFCognome;
    private javax.swing.JTextField jTFNCarta;
    private javax.swing.JTextField jTFNome;
    private javax.swing.JTextField jTFTCarta;
    private javax.swing.JTextField jTFTel;
    // End of variables declaration//GEN-END:variables

}
