package Globales;
import Personajes.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import villa1.*;

/*
 * Menu.java
 *
 * Created on 27-ago-2009, 16:50:11
 */

/**
 *
 * @author Xabier
 */
public class Cargar extends Inicio {
    /** Creates new form Menu */
    public Cargar() {
        pers = new Personaje();
        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() {

        jPanel1 = new javax.swing.JPanel();
        btnCargar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        txtResultado = new javax.swing.JTextField();
        btnContinuar = new javax.swing.JButton();
        jCheckBox1 = new javax.swing.JCheckBox();
        labelCargar = new javax.swing.JLabel();
        labelMote = new javax.swing.JLabel();
        txtMote = new javax.swing.JTextField();
        btnVolver = new javax.swing.JButton();
        labelPass = new javax.swing.JLabel();
        txtPass = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("RpG Java -> Cargar Personaje");

        btnCargar.setText("Cargar");
        btnCargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCargarActionPerformed(evt);
            }
        });

        btnSalir.setText("Salir del juego");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        txtResultado.setEditable(false);
        txtResultado.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        txtResultado.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

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

        jCheckBox1.setEnabled(false);
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        labelCargar.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        labelCargar.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelCargar.setText(". Cargar .");

        labelMote.setText("Mote Personaje:");

        txtMote.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtMote.setText("Personaje");
        txtMote.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtMoteMouseClicked(evt);
            }
        });

        btnVolver.setText("Volver");
        btnVolver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVolverActionPerformed(evt);
            }
        });

        labelPass.setText("Contraseña:");

        txtPass.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtPass.setText("1234");
        txtPass.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtPassMouseClicked(evt);
            }
        });
        txtPass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPassActionPerformed(evt);
            }
        });
        txtPass.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPassKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(87, 87, 87)
                .addComponent(jCheckBox1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnContinuar)
                .addContainerGap(89, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelCargar, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(labelPass)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtResultado, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnVolver)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                                .addComponent(btnSalir))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(labelMote)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtPass, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                                    .addComponent(txtMote, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE))))
                        .addGap(18, 18, 18))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(111, Short.MAX_VALUE)
                .addComponent(btnCargar)
                .addGap(106, 106, 106))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(labelCargar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelMote)
                    .addComponent(txtMote, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPass)
                    .addComponent(txtPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnCargar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtResultado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jCheckBox1)
                    .addComponent(btnContinuar))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalir)
                    .addComponent(btnVolver))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnCargar.getAccessibleContext().setAccessibleName("btnCargar");
        btnSalir.getAccessibleContext().setAccessibleName("btnSalir");
        btnContinuar.getAccessibleContext().setAccessibleName("btnContinuar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

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

    private void btnCargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCargarActionPerformed
                pers = new Personaje();
                FileInputStream fos;
                DataInputStream dos;
                FileInputStream fos2;
                DataInputStream dos2;
        try{
                pers.mote = txtMote.getText();
                File in2 = new File(pers.mote+"_pass.txt");
                fos2 = new FileInputStream(in2);
                dos2 = new DataInputStream(fos2);
    		pers.pass = dos2.readInt();
                String pass = txtPass.getText();
                pass.replaceAll(" ", "");
                int pass_ = Integer.parseInt(pass);
        if (pers.pass == pass_){
                pers.mote = txtMote.getText();
    		File in = new File(pers.mote+"_pass.txt");
                fos = new FileInputStream(in);
                dos = new DataInputStream(fos);
    		//Va leyendo linias y pasando los valores a las variables del person
                pers.hp = dos.readInt();
    		pers.hp_max = dos.readInt();
    		pers.pp = dos.readInt();
    		pers.pp_max = dos.readInt();
    		pers.nivel = dos.readInt();
    		pers.exp = dos.readInt();
    		pers.dinero = dos.readInt();
    		pers.atc = dos.readInt();
    		pers.def = dos.readInt();
    		pers.suerte = dos.readInt();
                pers.tipo = dos.readInt();
                pers.atc1 = dos.readInt();
                pers.atc2 = dos.readInt();
    		pers.agilidad = dos.readInt();
                pers.posicion = dos.readInt();
    		pers.contObj1 = dos.readInt();
    		pers.contObj2 = dos.readInt();
    		pers.contObj3 = dos.readInt();
    		pers.cC1 = dos.readInt();
    		pers.cC2 = dos.readInt();
    		pers.cC3 = dos.readInt();
    		pers.cE1 = dos.readInt();
    		pers.cE2 = dos.readInt();
    		pers.cE3 = dos.readInt();
                pers.bisera = dos.readInt();
                pers.gorra = dos.readInt();
                pers.casco = dos.readInt();
                pers.espada = dos.readInt();
                pers.dobles = dos.readInt();
                pers.hacha = dos.readInt();
                pers.mis1_vill1_fin = dos.readInt();
                pers.mis2_vill1_fin = dos.readInt();
                switch (pers.tipo){
                     case 1: Elfo elf = new Elfo(); elf.AsignarAtac(pers); break;
                     case 2: Mago mag = new Mago(); mag.AsignarAtac(pers); break;
                     case 3: Ladron lad = new Ladron(); lad.AsignarAtac(pers); break;
                     case 4: Orco orc = new Orco(); orc.AsignarAtac(pers); break;
                     case 5: Soldado sold = new Soldado(); sold.AsignarAtac(pers); break;
                     default: break;
                     }
                   //Confirma que exista y se haya cargado el personaje
    		if (pers.atc<=0 || pers.hp_max <=0 || pers.def<=0 || pers.pp_max <= 0){
                      txtResultado.setText("Error al cargar el personaje.");
    	 	}
    		else {
    		txtResultado.setText("Personaje cargado con exito.");
                jCheckBox1.setSelected(rootPaneCheckingEnabled);
    		}
                dos.close();
    		dos2.close();
        }
        else {
           txtResultado.setText("Error. Contraseña incorrecta.");
        }
    	}catch (Exception es){
    		txtResultado.setText(es.getMessage());
    		}
        
    }//GEN-LAST:event_btnCargarActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        System.exit(0);     // TODO add your handling code here:
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnContinuarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnContinuarActionPerformed
        if ((jCheckBox1.isSelected() == true)){
           if (pers.posicion == 1){this.hide(); new villa1.NMenu().setVisible(true);}
           if (pers.posicion == 2){this.hide(); new villa2.NMenu().setVisible(true);}
           else {txtResultado.setText("Error de villa.");}
        }
        else{
        txtResultado.setText("Carga antes de continuar o inicia una nueva partida");
        }// TODO add your handling code here:
    }//GEN-LAST:event_btnContinuarActionPerformed

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

    private void txtMoteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtMoteMouseClicked
 txtMote.setText(null);        // TODO add your handling code here:
    }//GEN-LAST:event_txtMoteMouseClicked

    private void btnVolverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVolverActionPerformed
this.hide();
new Menu().setVisible(true);// TODO add your handling code here:
    }//GEN-LAST:event_btnVolverActionPerformed

    private void txtPassKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPassKeyPressed
txtPass.addKeyListener(new KeyAdapter()
{ @Override
public void keyTyped(KeyEvent e)
{
char caracter = e.getKeyChar();
if(((caracter < '0') || (caracter > '9')) && (caracter != KeyEvent.VK_BACK_SPACE))
e.consume();
}
});         // TODO add your handling code here:
    }//GEN-LAST:event_txtPassKeyPressed

    private void txtPassMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtPassMouseClicked
txtPass.addKeyListener(new KeyAdapter()
{@Override
public void keyTyped(KeyEvent e)
{
char caracter = e.getKeyChar();
if(((caracter < '0') || (caracter > '9')) && (caracter != KeyEvent.VK_BACK_SPACE))
e.consume();
}
});         // TODO add your handling code here:
    }//GEN-LAST:event_txtPassMouseClicked

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


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCargar;
    private javax.swing.JButton btnContinuar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JButton btnVolver;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel labelCargar;
    private javax.swing.JLabel labelMote;
    private javax.swing.JLabel labelPass;
    private javax.swing.JTextField txtMote;
    private javax.swing.JTextField txtPass;
    private javax.swing.JTextField txtResultado;
    // End of variables declaration//GEN-END:variables

}
