/***************************************************************************
 *	ATM Network Simulator ACS. FIC. UDC. 2012/2013									*
 *	Copyright (C) 2013 by Pablo Castro and Marcos Chavarria 						*
 * <pablo.castro1@udc.es>, <marcos.chavarria@udc.es> 								*
 * 																								*
 * This program is free software; you can redistribute it and/or modify 	*
 * it under the terms of the GNU General Public License as published by 	*
 * the Free Software Foundation; either version 2 of the License, or 		*
 * (at your option) any later version. 												*
 ***************************************************************************/
package practicaacs.cajeros.iu;

import practicaacs.cajeros.Cajero;
import practicaacs.cajeros.Envio;


public class PantallaInicialCajero_IU extends javax.swing.JFrame {

    private Cajero cajero;
    
	/**
     * Creates new form Ventana_principal
     */
    public PantallaInicialCajero_IU(Cajero caj) {
    	this.cajero = caj;
    	initComponents();
        inicializa_visibilidades();
        this.setLocationRelativeTo(null);
    }

    /**
     * Funcion que inicializa las visibilidades y los campos de texto
     */
    private void inicializa_visibilidades(){
        this.TarjetaTextField.setText("banco001001");
        this.CuentaTextFIeld.setText("1");
        this.IncorrectosLabel.setVisible(false);
    }
    
    /**
     * 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() {

        jLabel1 = new javax.swing.JLabel();
        TarjetaTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        CuentaTextFIeld = new javax.swing.JTextField();
        CancelarButton = new javax.swing.JButton();
        ContinuarButton = new javax.swing.JButton();
        IncorrectosLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Cajero");

        jLabel1.setText("Introdúzca su número de tarjeta");

        TarjetaTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TarjetaTextFieldActionPerformed(evt);
            }
        });

        jLabel2.setText("Introdúzca su número de cuenta");

        CuentaTextFIeld.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CuentaTextFIeldActionPerformed(evt);
            }
        });

        CancelarButton.setText("Cancelar");
        CancelarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelarButtonActionPerformed(evt);
            }
        });

        ContinuarButton.setText("Continuar");
        ContinuarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ContinuarButtonActionPerformed(evt);
            }
        });

        IncorrectosLabel.setText("Escriba el número de cuenta y de tarjeta...");

        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(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)
                    .addComponent(CuentaTextFIeld, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(TarjetaTextField, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(CancelarButton)
                                .addGap(18, 18, 18)
                                .addComponent(ContinuarButton))
                            .addComponent(IncorrectosLabel, javax.swing.GroupLayout.Alignment.TRAILING))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(TarjetaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(CuentaTextFIeld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addComponent(IncorrectosLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CancelarButton)
                    .addComponent(ContinuarButton))
                .addContainerGap())
        );

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

    private void TarjetaTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TarjetaTextFieldActionPerformed
    }//GEN-LAST:event_TarjetaTextFieldActionPerformed

    private void CancelarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelarButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_CancelarButtonActionPerformed

    private void CuentaTextFIeldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CuentaTextFIeldActionPerformed
    }//GEN-LAST:event_CuentaTextFIeldActionPerformed

    private void ContinuarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ContinuarButtonActionPerformed
        
    	try{
	    	String tarjeta = this.TarjetaTextField.getText();
	        String cuenta = this.CuentaTextFIeld.getText();
	        inicializa_visibilidades();

	        //Comprueba si TARJETA esta bien escrito
	        if(tarjeta.equals("") | tarjeta.length() < 4 | tarjeta.length() > 11){
	            IncorrectosLabel.setText("Número de tarjeta incorrecto...");
	            throw new NumberFormatException();
	        }
	        //Comprueba si CUENTA esta bien escrito
	        if(cuenta.equals("") | cuenta.length()!=1){
	            IncorrectosLabel.setText("Número de cuenta incorrecto...");
	            throw new NumberFormatException();
	        }
	        
	        int num_cuenta = Integer.parseInt(cuenta);
	        
	        //Crea el envio y se lo pasa a la siguiente pantalla
	        Envio envio = new Envio(tarjeta,num_cuenta);
	        this.setVisible(false);
	        new SeleccionAccion_IU(this,this.cajero,envio).setVisible(true);
    	}
    	catch (NumberFormatException nfe){
    		//Si hay errores, mostramos el label correspondiente.
            this.IncorrectosLabel.setVisible(true);
            return;
    	}
    }//GEN-LAST:event_ContinuarButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PantallaInicialCajero_IU.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PantallaInicialCajero_IU.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PantallaInicialCajero_IU.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PantallaInicialCajero_IU.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PantallaInicialCajero_IU(null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelarButton;
    private javax.swing.JButton ContinuarButton;
    private javax.swing.JTextField CuentaTextFIeld;
    private javax.swing.JLabel IncorrectosLabel;
    private javax.swing.JTextField TarjetaTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables
}
