/*
 * jfrmInicio.java
 *
 * Created on 16 de enero de 2009, 21:47
 */



/**
 *
 * @author  Administrador
 */
public class jfrmInicio extends javax.swing.JFrame {

    /** Creates new form jfrmInicio */
    Juego j;
    String nombrePartida;
    String nombreJugador;
    String ejercito;
    String numJugadores=new String("0");
    int n;
    public jfrmInicio(Juego j) {
        initComponents();
        jComboBoxEjercitos.removeAllItems();
        jComboBoxEjercitos.addItem("Pijos");
        jComboBoxEjercitos.addItem("Canis");
        jComboBoxEjercitos.addItem("Frikis");
        jComboBoxEjercitos.addItem("Heviatas");
        jComboBoxEjercitos.addItem("Raperos");
        jComboBoxEjercitos.addItem("Jipis Guarros");
        jLabel1.setVisible(false);
        jLabelJugadores.setVisible(true);
        jLabelNumeroJugador.setVisible(false);
        jButtonAnadirJugador.setVisible(false);
        jButtonFinalizado.setVisible(false);
        jComboBoxEjercitos.setVisible(false);
        jLabelNombrePartida.setVisible(true);
        jTextFieldNombrePartida.setVisible(true);
        jTextFieldNombreJugador.setVisible(false);
        this.j=j;
        
    }

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

        jComboBox1 = new javax.swing.JComboBox();
        jLabelJugadores = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldNombreJugador = new javax.swing.JTextField();
        jComboBoxEjercitos = new javax.swing.JComboBox();
        jButtonAnadirJugador = new javax.swing.JButton();
        jButtonFinalizado = new javax.swing.JButton();
        jLabelNumeroJugador = new javax.swing.JLabel();
        jButtonAceptar = new javax.swing.JButton();
        jLabelNombrePartida = new javax.swing.JLabel();
        jTextFieldNombrePartida = new javax.swing.JTextField();
        jLabelInfo = new javax.swing.JLabel();
        jTextFieldJugadores = new javax.swing.JTextField();
        jButtonCancelar = new javax.swing.JButton();

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabelJugadores.setText("Jugadores:");

        jLabel1.setText("Jugador");

        jComboBoxEjercitos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jButtonAnadirJugador.setText("Añadir Jugador");
        jButtonAnadirJugador.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAnadirJugadorActionPerformed(evt);
            }
        });

        jButtonFinalizado.setText("Finalizado");
        jButtonFinalizado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonFinalizadoActionPerformed(evt);
            }
        });

        jButtonAceptar.setText("Aceptar");
        jButtonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAceptarActionPerformed(evt);
            }
        });

        jLabelNombrePartida.setText("Nombre de la partida: ");

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

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

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelNumeroJugador, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabelJugadores)
                    .addComponent(jLabelNombrePartida))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldNombreJugador, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldJugadores, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 137, Short.MAX_VALUE)
                                .addComponent(jComboBoxEjercitos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(37, 37, 37)
                                .addComponent(jButtonAceptar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButtonCancelar))))
                    .addComponent(jTextFieldNombrePartida, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE))
                .addGap(130, 130, 130))
            .addGroup(layout.createSequentialGroup()
                .addGap(79, 79, 79)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabelInfo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jButtonAnadirJugador)
                        .addGap(68, 68, 68)
                        .addComponent(jButtonFinalizado)))
                .addContainerGap(185, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelNombrePartida)
                    .addComponent(jTextFieldNombrePartida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelJugadores)
                    .addComponent(jTextFieldJugadores, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonAceptar)
                    .addComponent(jButtonCancelar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelNumeroJugador, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldNombreJugador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBoxEjercitos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jLabel1)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonAnadirJugador)
                    .addComponent(jButtonFinalizado))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabelInfo, javax.swing.GroupLayout.DEFAULT_SIZE, 15, Short.MAX_VALUE)
                .addContainerGap())
        );

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

public int getNumJugadoresIntroducidos(){
    return Integer.parseInt(numJugadores);
}    
public void crearPartida(){
    String s=jTextFieldNombrePartida.getText();
    
    if(s.isEmpty()){
        jLabelInfo.setText("Introduce un nombre para la partida");
    }
    else{
        numJugadores=(String) jTextFieldJugadores.getText();
        if(numJugadores.isEmpty() || Integer.parseInt(numJugadores)>6 || Integer.parseInt(numJugadores)<2){
            jLabelInfo.setText("Introduce un número de jugadores entre 2 y 6");
        }
        else{
            j.setNombrePartida(s);
            j.setNumJugadores(Integer.parseInt(numJugadores));
            j.setNumJugadoresActivos(Integer.parseInt(numJugadores));
            jTextFieldJugadores.setVisible(false);
            jButtonAceptar.setVisible(false);
            jLabel1.setVisible(true);
            jLabelJugadores.setVisible(false);
            jLabelNumeroJugador.setVisible(true);
            jButtonAnadirJugador.setVisible(true);
            jButtonCancelar.setVisible(false);
            jComboBoxEjercitos.setVisible(true);
            jLabelNombrePartida.setVisible(false);
            jTextFieldNombrePartida.setVisible(false);
            jTextFieldNombreJugador.setVisible(true);
            jLabelNumeroJugador.setText(Integer.toString(n+1));
           
        }
    }
    }    

public void anadirJugador(){
    String s=jTextFieldNombreJugador.getText();
    if(s.isEmpty()){
        jLabelInfo.setText("Introduce un nombre para el jugador");
    }
    else{
        jLabelInfo.setText("");
        ejercito=(String) jComboBoxEjercitos.getSelectedItem();
        jComboBoxEjercitos.removeItem(jComboBoxEjercitos.getSelectedItem());
        Jugador jug=new Jugador(s,ejercito,n);
        j.anadirJugador(jug);
        jLabelInfo.setText("Jugador añadido correctamente");
        n++;
        jLabelNumeroJugador.setText(Integer.toString(n+1));
        if(n==j.getNumJugadores()){
            jComboBoxEjercitos.setVisible(false);
            jButtonAnadirJugador.setVisible(false);
            jButtonFinalizado.setVisible(true);
            jButtonCancelar.setVisible(false);
            jTextFieldNombreJugador.setVisible(false);
            jLabelJugadores.setVisible(false);
            jLabelNumeroJugador.setVisible(false);
            jLabel1.setVisible(false);
            
            
        }
    }
    }

public void repartirTropasInicio(){
    int nTropasInicio=0;
        switch (j.getNumJugadores()){
            case 2: nTropasInicio=30;break;
            case 3: nTropasInicio=24;break;
            case 4: nTropasInicio=19;break;
            case 5: nTropasInicio=15;break;
            case 6: nTropasInicio=12;break;
        }
        for(int i=0;i<j.getNumJugadores();i++){
            j.getjugadores(i).setTropasPendUbicar(nTropasInicio);
        }
}
private void jButtonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAceptarActionPerformed
    crearPartida();
    
    // TODO add your handling code here:
}//GEN-LAST:event_jButtonAceptarActionPerformed

private void jTextFieldNombrePartidaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldNombrePartidaActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextFieldNombrePartidaActionPerformed

private void jButtonAnadirJugadorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAnadirJugadorActionPerformed
    anadirJugador();// TODO add your handling code here:
}//GEN-LAST:event_jButtonAnadirJugadorActionPerformed

private void jButtonFinalizadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFinalizadoActionPerformed
    setVisible(false);
    repartirTropasInicio();// TODO add your handling code here:
}//GEN-LAST:event_jButtonFinalizadoActionPerformed

private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
    jTextFieldNombrePartida.setText("");
    jTextFieldJugadores.setText("");
    setVisible(false);// TODO add your handling code here:
}//GEN-LAST:event_jButtonCancelarActionPerformed

    /**
    * @param args the command line arguments
    */
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAceptar;
    private javax.swing.JButton jButtonAnadirJugador;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonFinalizado;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBoxEjercitos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelInfo;
    private javax.swing.JLabel jLabelJugadores;
    private javax.swing.JLabel jLabelNombrePartida;
    private javax.swing.JLabel jLabelNumeroJugador;
    private javax.swing.JTextField jTextFieldJugadores;
    private javax.swing.JTextField jTextFieldNombreJugador;
    private javax.swing.JTextField jTextFieldNombrePartida;
    // End of variables declaration//GEN-END:variables

}
