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

/*
 * WormsLike_Main.java
 *
 * Created on 26 juin 2011, 12:03:13
 */
package wormslike;

import java.io.File;
import java.util.HashMap;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import wormslike_game.Game_Frame;
import wormslike_mapeditor.MapEditor_Main;
import wormslike_menu.Menu_Main;
import wormslike_menu.VideoSettings_Menu;
import wormslike_sound.Game_Sound;

/**
 * CLASSE PRINCIPALE
 * @author wallouf
 */
public class WormsLike_Main extends javax.swing.JFrame {

    public static int THEME = 0;
    public static Game_Sound sound;

    /** Creates new form WormsLike_Main */
    //CREE LE NOM, LA TAILLE, LES OPTIONS, ET LE STYLE DE LA FENETRE
    public WormsLike_Main() {
        try {
            boolean find = false;
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Metal".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    find = true;
                    break;
                }
            }
            if (!find) {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            }
        } catch (Exception e) {
        }
        initComponents();
        this.setResizable(false);
        this.setTitle("World Like v1.0 (0.9.4)");
        this.setSize(1000, 650);
        this.setLocationRelativeTo(null);
        this.setContentPane(mM);
        sound = new Game_Sound();
        Game_Sound.playMenu();
        THEME = 0;
    }

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

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 959, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 642, Short.MAX_VALUE)
        );

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

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new WormsLike_Main().setVisible(true);
            }
        });
    }
    //PERMET DE GERER LE MENU ET LES DIFFERENTS SOUS MENUS
    public void changeView(String name) {
        //on reviens sur le menu
        if (name.equalsIgnoreCase("retour_video")) {
            mM.setSelection(0);
            this.setContentPane(mM);
            //on lance une partie deux joueurs humains et on cache le menu (plus de ressources)
        } else if (name.equalsIgnoreCase("two")) {
            int option = JOptionPane.showConfirmDialog(null, "Please select a map with .xml.gz extension", "Select your map", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (option == JOptionPane.OK_OPTION) {
                jfc.setCurrentDirectory(new File("./bin/maps/"));
                if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)//retourne une valeur si yes ou ok est choisit
                {
                    HashMap<Integer, Integer> mapPos;
                    if (jfc.getSelectedFile().getName().endsWith(".xml.gz")) {
                        mapPos = (HashMap<Integer, Integer>) MapEditor_Main.decodeFromFileGz(jfc.getSelectedFile().getPath());
                    } else {
                        System.out.println("Please select a good path.");
                        return;
                    }
                    if (gF != null && gF.isVisible()) {
                        gF.dispose();
                    }
                    Game_Sound.stopMenu();
                    gF = new Game_Frame(this, mapPos, false, false);
                    this.setVisible(false);
                }
            }
            //on lance une partie avec un joueur humain et une IA et on cache le menu
        } else if (name.equalsIgnoreCase("one")) {
            int option = JOptionPane.showConfirmDialog(null, "Please select a map with .xml.gz extension", "Select your map", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (option == JOptionPane.OK_OPTION) {
                jfc.setCurrentDirectory(new File("./bin/maps/"));
                if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)//retourne une valeur si yes ou ok est choisit
                {
                    HashMap<Integer, Integer> mapPos;
                    if (jfc.getSelectedFile().getName().endsWith(".xml.gz")) {
                        mapPos = (HashMap<Integer, Integer>) MapEditor_Main.decodeFromFileGz(jfc.getSelectedFile().getPath());
                    } else {
                        System.out.println("Please select a good path.");
                        return;
                    }
                    if (gF != null && gF.isVisible()) {
                        gF.dispose();
                    }
                    Game_Sound.stopMenu();
                    gF = new Game_Frame(this, mapPos, false, true);
                    this.setVisible(false);
                }
            }
            //on affiche le menu video
        } else if (name.equalsIgnoreCase("video settings")) {
            this.setContentPane(vSM);
            //on affiche le createur de map
        } else if (name.equalsIgnoreCase("map editor")) {
            if (mapEditor != null && mapEditor.isVisible()) {
                mapEditor.dispose();
            }
            mapEditor = new MapEditor_Main();
            mapEditor.setVisible(true);
            //on quitte le logiciel
        } else if (name.equalsIgnoreCase("exit")) {
            Game_Sound.stopMenu();
            System.exit(0);
        }
        this.validate();
        this.repaint();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    Menu_Main mM = new Menu_Main(this);
    VideoSettings_Menu vSM = new VideoSettings_Menu(this, THEME);
    Game_Frame gF;
    MapEditor_Main mapEditor;
    private JFileChooser jfc = new JFileChooser("");
}
