package fr.enssat.BoulderDash.controllers;

import fr.enssat.BoulderDash.models.LevelModel;
import fr.enssat.BoulderDash.helpers.AudioLoadHelper;
import fr.enssat.BoulderDash.views.MenuView;
import fr.enssat.BoulderDash.views.GameView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


/**
 * GameController
 * <p>
 * This system creates the view.
 * The game loop is also handled there.
 *
 * @author Colin Leverger <me@colinleverger.fr>
 * @since 2015-06-19
 */
public class GameController implements ActionListener {
  private LevelModel levelModel;
  private AudioLoadHelper audioLoadHelper;
  private boolean firstClickOnPause;
  private MenuView menuView;
  private GameView gameView;

  /**
   * Class constructor
   *
   * @param levelModel                      Level model
   * @param navigationBetweenViewController
   */
  public GameController(LevelModel levelModel, AudioLoadHelper audioLoadHelper, NavigationBetweenViewController navigationBetweenViewController) {
    this.firstClickOnPause = true;

    this.levelModel = levelModel;
    this.audioLoadHelper = audioLoadHelper;
    this.gameView = new GameView(this, levelModel);
    this.menuView = navigationBetweenViewController.getMenuView();

    this.getAudioLoadHelper().stopMusic();
    this.getAudioLoadHelper().playSound("new");
  }

  /**
   * Handles the 'action performed' event
   *
   * @param event Action event
   */
  public void actionPerformed(ActionEvent event) {
    switch (event.getActionCommand()) {
      case "pause":
        if (this.firstClickOnPause) {
          this.levelModel.setGamePaused(true);
        } else if (!this.firstClickOnPause) {
          this.levelModel.setGamePaused(false);
        }

        this.firstClickOnPause = !this.firstClickOnPause;
        this.gameView.getGameFieldView().grabFocus();
        break;

      case "restart":
        this.reset();
        this.getAudioLoadHelper().playSound("new");
        this.gameView.getGameFieldView().grabFocus();
        break;

      case "menu":
        this.menuView.setVisible(true);
        this.getAudioLoadHelper().startMusic("game");
        this.dispose();
        break;
    }
  }

  /**
   * Reset the game
   */
  private void reset() {
    String pickedLevel = levelModel.getLevelName();
    dispose();

    // Recreate game
    this.levelModel = new LevelModel(pickedLevel, audioLoadHelper);
    this.gameView = new GameView(this, levelModel);
    this.gameView.setVisible(true);
  }

  /**
   * Dispose the game
   */
  private void dispose() {
    this.gameView.dispose();
  }

  /**
   * Gets the audio load helper instance
   *
   * @return Audio load helper instance
   */
  public AudioLoadHelper getAudioLoadHelper() {
    return this.audioLoadHelper;
  }

  /**
   * Return the game view
   *
   * @return gameView
   */
  public GameView getGameView() {
    return gameView;
  }

  // dead code
  //
  //public void setGameView(GameView gameView) {
  //  this.gameView = gameView;
  //}
}