/Users/petercappello/NetBeansProjects/56-2014/56-2014-4-AdaptableZapem/src/ControlPanel.java
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;

/**
 * The controller for the Zap 'em game.
 * @author Pete Cappello
 */
public class ControlPanel extends JPanel implements ActionListener
{
    private static int PAUSE = 200;         // milliseconds
    private static int PAUSE_DELTA = 50;    // milliseconds
    private static int WINNER_TIME = 10000; // 10 seconds
    
    private final View view;
    private final Game game;
    
    private final JButton newGameButton = new JButton( "New Game" );
    private final JLabel  gameDurationLabel = new JLabel( "Game Duration (s):" );
    private final JTextField gameDurationTextField = new JTextField();
    private final JLabel  gameLevelLabel = new JLabel( "  Game Level:" );
    private final JTextField gameLevelLabelTextField = new JTextField();
    
    private Timer animationTimer;
    private long gameStartTime;
    private int  level = PAUSE / PAUSE_DELTA;
    
    ControlPanel( View view, Game game ) 
    {
        this.view = view;
        this.game = game;
     
        setLayout( new GridLayout( 1, 5 ) );
        add( gameLevelLabel );
        add( gameLevelLabelTextField );
        add( newGameButton );
        add( gameDurationLabel );
        add( gameDurationTextField );

        animationTimer = new Timer( PAUSE, this );
        gameDurationTextField.setEditable( false );
        gameLevelLabelTextField.setEditable( false );
        gameLevelLabelTextField.setText( "" + level );
        
        addEventListeners();
    }

    private void addEventListeners() 
    {
        //------------------------------------------
        // contoller TEMPLATE CODE for each action
        //------------------------------------------
        // If you are running Java 8, use lambda expressions
//        newGameButton.addActionListener( 
//            ( ActionEvent actionEvent ) -> { newGameButtonActionPerformed( actionEvent ); } 
//        );
        
        // If you are not running Java 8, uncomment the code below   
        newGameButton.addActionListener( new ActionListener() 
        {
            @Override
            public void actionPerformed( ActionEvent actionEvent ) 
            {
                newGameButtonActionPerformed( actionEvent );
            }
        } );
        
        view.addMouseListener( new MouseAdapter() 
        {
            /**
            * Process mouse clicked event
            * @param event contains the x & y coordinates of the mouse click 
            */
           @Override
           public void mousePressed( MouseEvent event ) 
           {
               if ( game.isOver() )
               {
                   return; // ignore mouse clicks between games
               }
               animationTimer.stop();
               game.processClick( event.getX(), event.getY() );      
               animationTimer.start();
               view.repaint();
           }
        } );
    }

    // _____________________________
    //  controller for each action
    // _____________________________
    private void newGameButtonActionPerformed( ActionEvent actionEvent ) 
    {
        gameDurationTextField.setText("");
        gameStartTime = System.currentTimeMillis();
        animationTimer.restart();
        game.start();
    }
    /**
     * Implementation of ActionListener of Timer
     * @param e unused
     */
    @Override
    public void actionPerformed( ActionEvent e) 
    {
        game.draw();
        view.repaint();
        
        long gameDuration = System.currentTimeMillis() - gameStartTime;
        long seconds  = gameDuration / 1000;
        long fraction = gameDuration % 1000;
        gameDurationTextField.setText( seconds + "." + fraction );
        gameDurationTextField.setText( seconds + "." + fraction );
        if ( game.isOver() ) 
        {
            animationTimer.stop();
            adjustPauseTime( gameDuration );
            animationTimer = new Timer( PAUSE, this );
            gameLevelLabelTextField.setText( "" + level );
        }
}
    private void adjustPauseTime( long gameDuration )
    {
        if ( gameDuration > WINNER_TIME )
        {
            level--;
            JOptionPane.showMessageDialog( null, "PlayerTooFrigginSlowException: Perhaps more coffee? Down to level " + level, "Game Kibitzer", JOptionPane.ERROR_MESSAGE);
            PAUSE += PAUSE_DELTA;
        }
        else
        {
            level++;
            JOptionPane.showMessageDialog( null, "I am in the presence of greatness. Onward & upward to level " + level, "Game Kibitzer", JOptionPane.INFORMATION_MESSAGE);
            PAUSE -= PAUSE_DELTA;
        }
        PAUSE = PAUSE > 0 ? PAUSE : PAUSE_DELTA;
    }
}