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

/*
 * GPrenotazioneP.java
 *
 * Created on 10-lug-2011, 11.27.27
 */

package app.gui;


import app.Main;
import archive.data.Cliente;
import archive.data.Prenotazioni;
import archive.interfacce.*;
import archive.system.*;
import archive.utility.FormattedSetPrenotationArray;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.swing.JOptionPane;

public class GPrenotazioneP extends javax.swing.JFrame {
  private GestionePrenotazione gpren=new PrenotationManager(AgenziaSystem.archivio.getPrenotazioni());
  public static String nvillaggio;
  public static int nweek;
  public static String cognome;
    /** Creates new form GPrenotazioneP */
    public GPrenotazioneP() {
        Main.gui.setVisible(false);
        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();
        jLabel1 = new javax.swing.JLabel();
        jBCancella = new javax.swing.JButton();
        jBModifica = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jLElenco = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jTFCognome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTFCognomeActionPerformed(evt);
            }
        });
        jTFCognome.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTFCognomeKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 67;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(85, 10, 0, 0);
        getContentPane().add(jTFCognome, gridBagConstraints);

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

        jBCancella.setText("Cancella Prenotazione");
        jBCancella.setEnabled(false);
        jBCancella.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCancellaActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(62, 43, 0, 0);
        getContentPane().add(jBCancella, gridBagConstraints);

        jBModifica.setText("Modifica Caratteristiche");
        jBModifica.setEnabled(false);
        jBModifica.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBModificaActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(42, 43, 0, 0);
        getContentPane().add(jBModifica, gridBagConstraints);

        jScrollPane1.setViewportView(jLElenco);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 208;
        gridBagConstraints.ipady = 182;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(85, 42, 28, 41);
        getContentPane().add(jScrollPane1, gridBagConstraints);

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

    private void jBCancellaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCancellaActionPerformed
        Cliente cliente= gpren.requestInformation(jTFCognome.getText());
        Set<Prenotazioni> set= gpren.showList(cliente);
        String[] s=new String[0];
        for(Prenotazioni x:set){
            int n=s.length;
            s=Arrays.copyOf(s, n+1);
            s[n]=x.toString();
           
        }
        s = FormattedSetPrenotationArray.formattedSetPrenotation(s);
         jLElenco.setListData(s);
        String nomevillaggio = JOptionPane.showInputDialog(rootPane, "Inserisci il nome del Villaggio");
        String nsett = JOptionPane.showInputDialog(rootPane, "Inserisci il numero della settimana");
        if(gpren.delPrenotation(AgenziaSystem.archivio.getOnePrenotazioni(nomevillaggio).getPrenotazione(jTFCognome.getText(), Integer.parseInt(nsett)))){
            JOptionPane.showMessageDialog(rootPane,"Cancellazione Effettuata");
        }

    }//GEN-LAST:event_jBCancellaActionPerformed

    private void jTFCognomeKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTFCognomeKeyReleased
    
    }//GEN-LAST:event_jTFCognomeKeyReleased

    private void jTFCognomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTFCognomeActionPerformed
        Cliente cliente = null;
        try{
            cliente=gpren.requestInformation(jTFCognome.getText());
        }catch( NoSuchElementException e){
             JOptionPane.showMessageDialog(rootPane, "Cliente non presente nell'archivio","",JOptionPane.ERROR_MESSAGE);
        }
       if(gpren.showList(cliente).isEmpty()){
            JOptionPane.showMessageDialog(rootPane, "Prenotazioni non presenti", "", JOptionPane.ERROR_MESSAGE);
        }else{
               cognome=jTFCognome.getText();
               jBCancella.setEnabled(true);
               jBModifica.setEnabled(true);
        }
    }//GEN-LAST:event_jTFCognomeActionPerformed

    private void jBModificaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBModificaActionPerformed
        Cliente cliente= gpren.requestInformation(jTFCognome.getText());
        Set<Prenotazioni> set= gpren.showList(cliente);
        String[] s=new String[0];
        for(Prenotazioni x:set){
            int n=s.length;
            s=Arrays.copyOf(s, n+1);
            s[n]=x.toString().replace(';', ' ');
        }
        s = FormattedSetPrenotationArray.formattedSetPrenotation(s);
         jLElenco.setListData(s);
       String nomevillaggio = JOptionPane.showInputDialog(rootPane, "Inserisci il nome del Villaggio");
       String nsett = JOptionPane.showInputDialog(rootPane, "Inserisci il numero della settimana");
       nvillaggio= nomevillaggio;
       nweek=Integer.parseInt(nsett);
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MPrenotazione().setVisible(true);
            }
        });
    }//GEN-LAST:event_jBModificaActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
Main.gui.setVisible(true);        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosing

 
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBCancella;
    private javax.swing.JButton jBModifica;
    private javax.swing.JList jLElenco;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTFCognome;
    // End of variables declaration//GEN-END:variables

}
