/**
 * GUI for the Hunt the Wumpus game.
 *   @author Dave Reed
 *   @version 4/4/17
 */
public class WumpusGUI extends javax.swing.JFrame {
    private final static String fileName = "caves.txt";
    private CaveMaze maze;
    
    /**
     * Creates new form WumpusGUI
     */
    public WumpusGUI() {
        initComponents();
        this.startGame();
    }

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

        jScrollPane1 = new javax.swing.JScrollPane();
        outputArea = new javax.swing.JTextArea();
        titleLabel = new javax.swing.JLabel();
        moveLabel = new javax.swing.JLabel();
        move1Button = new javax.swing.JButton();
        move2Button = new javax.swing.JButton();
        move3Button = new javax.swing.JButton();
        tossLabel = new javax.swing.JLabel();
        toss1Button = new javax.swing.JButton();
        toss2Button = new javax.swing.JButton();
        toss3Button = new javax.swing.JButton();
        repeatButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        outputArea.setColumns(20);
        outputArea.setRows(5);
        jScrollPane1.setViewportView(outputArea);

        titleLabel.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        titleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        titleLabel.setText("Hunt the Wumpus");

        moveLabel.setText("move");

        move1Button.setText("1");
        move1Button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                move1ButtonMouseClicked(evt);
            }
        });

        move2Button.setText("2");
        move2Button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                move2ButtonMouseClicked(evt);
            }
        });

        move3Button.setText("3");
        move3Button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                move3ButtonMouseClicked(evt);
            }
        });

        tossLabel.setText("toss");

        toss1Button.setText("1");
        toss1Button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                toss1ButtonMouseClicked(evt);
            }
        });

        toss2Button.setText("2");
        toss2Button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                toss2ButtonMouseClicked(evt);
            }
        });

        toss3Button.setText("3");
        toss3Button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                toss3ButtonMouseClicked(evt);
            }
        });

        repeatButton.setText("Play Again");
        repeatButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                repeatButtonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(titleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(15, 15, 15)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(moveLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(move1Button)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(move2Button)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(move3Button))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(tossLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(toss1Button)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(toss2Button)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(toss3Button)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(repeatButton))))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 451, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(titleLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(moveLabel)
                            .addComponent(move1Button)
                            .addComponent(move2Button)
                            .addComponent(move3Button))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tossLabel)
                            .addComponent(toss1Button)
                            .addComponent(toss2Button)
                            .addComponent(toss3Button))
                        .addGap(26, 26, 26))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(repeatButton)
                        .addContainerGap())))
        );

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

    private void move1ButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_move1ButtonMouseClicked
        if (this.maze.stillAble() && this.maze.stillWumpi()) {
            this.outputArea.setText(this.maze.move(1));
            this.update();
        }      
    }//GEN-LAST:event_move1ButtonMouseClicked

    private void move2ButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_move2ButtonMouseClicked
        if (this.maze.stillAble() && this.maze.stillWumpi()) {
            this.outputArea.setText(this.maze.move(2));
            this.update();
        }
    }//GEN-LAST:event_move2ButtonMouseClicked

    private void move3ButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_move3ButtonMouseClicked
        if (this.maze.stillAble() && this.maze.stillWumpi()) {
            this.outputArea.setText(this.maze.move(3));
            this.update();
        }
    }//GEN-LAST:event_move3ButtonMouseClicked

    private void toss1ButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_toss1ButtonMouseClicked
        if (this.maze.stillAble() && this.maze.stillWumpi()) {
            this.outputArea.setText(this.maze.toss(1));
            this.update();
        }
    }//GEN-LAST:event_toss1ButtonMouseClicked

    private void toss2ButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_toss2ButtonMouseClicked
        if (this.maze.stillAble() && this.maze.stillWumpi()) {
            this.outputArea.setText(this.maze.toss(2));
            this.update();
        }
    }//GEN-LAST:event_toss2ButtonMouseClicked

    private void toss3ButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_toss3ButtonMouseClicked
        if (this.maze.stillAble() && this.maze.stillWumpi()) {
            this.outputArea.setText(this.maze.toss(3));
            this.update();
        }
    }//GEN-LAST:event_toss3ButtonMouseClicked

    private void repeatButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_repeatButtonMouseClicked
    this.startGame();
    }//GEN-LAST:event_repeatButtonMouseClicked

    private void startGame() {
        try {
            this.maze = new CaveMaze(WumpusGUI.fileName);
            this.outputArea.setText("HUNT THE WUMPUS:  "
                    + "Your mission is to explore the maze of caves\n"
                    + "and capture all of the wumpi (without getting yourself mauled).\n"
                    + "To move to an adjacent cave or toss a stun grenade, click on \n"
                    + "the button corresponding to that tunnel number.");
            this.update();
        } catch (java.io.FileNotFoundException e) {
            this.outputArea.setText("Unable to find " + WumpusGUI.fileName + " data file.");
        }
    }

    private void update() {
        if (this.maze.stillAble() && this.maze.stillWumpi()) {
            this.outputArea.append("\n\n" + this.maze.showLocation());
        } else {
            this.outputArea.append("\n\nGAME OVER");
        }
    }
    /**
     * @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(WumpusGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(WumpusGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(WumpusGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(WumpusGUI.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 WumpusGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton move1Button;
    private javax.swing.JButton move2Button;
    private javax.swing.JButton move3Button;
    private javax.swing.JLabel moveLabel;
    private javax.swing.JTextArea outputArea;
    private javax.swing.JButton repeatButton;
    private javax.swing.JLabel titleLabel;
    private javax.swing.JButton toss1Button;
    private javax.swing.JButton toss2Button;
    private javax.swing.JButton toss3Button;
    private javax.swing.JLabel tossLabel;
    // End of variables declaration//GEN-END:variables
}
