/Users/petercappello/NetBeansProjects/56-2014/56-2014-3-Zapem/src/ControlPanel.java
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JLabel;
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, MouseListener
{
    private final static int PAUSE = 150; // milliseconds
    
    private final View view;
    private final Game game;
    
    private final JButton newGameButton = new JButton( "New Game" );
    private final JLabel  gameDurationLabel = new JLabel( "  Game duraion in seconds:" );
    private final JTextField gameDurationTextField = new JTextField();
    
    private final Timer animationTimer;
    private long gameStartTime;
    
    ControlPanel( View view, Game game ) 
    {
        this.view = view;
        this.game = game;
     
        setLayout( new GridLayout( 1, 3 ) );
        add( newGameButton );
        add( gameDurationLabel );
        add( gameDurationTextField );

        animationTimer = new Timer( PAUSE, this );
        gameDurationTextField.setEditable( false );
        addEventListener();
    }

    private void addEventListener() 
    {
        //------------------------------------------
        // 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 );
            }
        });
        
        // register this as the listener for mouse events in the View JPanel
        view.addMouseListener( this );
    }

    // _____________________________
    //  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();
        if ( game.isOver() ) 
        {
            animationTimer.stop();
            Long gameDuration = System.currentTimeMillis() - gameStartTime;
            Long seconds  = gameDuration / 1000;
            Long fraction = gameDuration % 1000;
            gameDurationTextField.setText( seconds + "." + fraction );
        }
}

    // Begin implementation of MouseListener
    /**
     * Process mouse clicked event
     * @param event contains the x & y coordinates of the mouse click
     */
    @Override
    public void mouseClicked( MouseEvent event ) 
    {
        if ( game.isOver() )
        {
            return; // ignore mouse clicks between games
        }
        animationTimer.stop();
        game.processClick( event.getX(), event.getY() );      
        animationTimer.start();
        view.repaint();
    }

    /**
     * Unimplemented.
     * @param e 
     */
    @Override
    public void mousePressed(MouseEvent e) {}

    /**
     * Unimplemented.
     * @param e 
     */
    @Override
    public void mouseReleased(MouseEvent e) {}

    /**
     * Unimplemented.
     * @param e 
     */
    @Override
    public void mouseEntered(MouseEvent e) {}
    
    /**
     * Unimplemented.
     * @param e 
     */
    @Override
    public void mouseExited(MouseEvent e) {}
    // End implementation of MouseListener
}