  ig.module(
	'game.main'
)
.requires(
  //'impact.debug.menu',
	'impact.game',
	'impact.font',

	'game.entities.ball',
	'game.entities.paddle-enemy',
	'game.entities.paddle-player',

	'game.levels.level1'
)
.defines(function(){
  RunningGame = ig.Game.extend({
    font: new ig.Font( 'media/04b03.font.png' ),
    pauseDialog: new ig.Image('media/pause_dialog.png'),
    showPause: false,
    clearColor: null,

    init: function() {
      ig.input.unbindAll();
      ig.global.score = {'ai': 1, 'human': 1};

      // Menu navigation
      ig.input.bind(ig.KEY.ESC, 'escape');
      ig.input.bind(ig.KEY.ENTER, 'enter');

      // Game navigation
      ig.input.bind(ig.KEY.UP_ARROW, 'up');
      ig.input.bind(ig.KEY.DOWN_ARROW, 'down');
      ig.input.bind(ig.KEY.T, 'force-top');
      ig.input.bind(ig.KEY.B, 'force-bottom');

      // vim goodness
      ig.input.bind(ig.KEY.K, 'up');
      ig.input.bind(ig.KEY.J, 'down');

      // Gamer style
      ig.input.bind(ig.KEY.W, 'up');
      ig.input.bind(ig.KEY.S, 'down');

      // Load the level
      this.loadLevel(LevelLevel1);
    },

    update: function() {
      // User is in the pause menu
      if (this.showPause) {
        if (ig.input.pressed('escape')) {
          ig.system.setGame(StartScreen);
          return;
        }

        if (ig.input.pressed('enter')) {
          this.showPause = false;
          return;
        }
      }

      // One player has won the game
      if (ig.global.score.ai == 0 || ig.global.score.human == 0) {
        ig.system.setGame(ScoreboardScreen);
      }

      // The User want back to the main menu inside a running game
      if (ig.input.pressed('escape')) {
        this.showPause = true;
      }

      // The game is running
      if (!this.showPause) this.parent();
    },

    draw: function() {
      ig.system.context.clearRect( 0 ,0, ig.system.realWidth, ig.system.realHeight );
		  this.parent(); // Draw entries and background
      this.font.draw(ig.global.score.ai, 270, 25, ig.Font.ALIGN.CENTER);
      this.font.draw(ig.global.score.human, 350, 25, ig.Font.ALIGN.CENTER);

      if (this.showPause) {
        this.pauseDialog.draw(150, 100);
      }
	  }
  });

  ScoreboardScreen = ig.Game.extend({
    scoreoid: {
      api_key: 'd1011f8f6776a10f7a5d87eaa86c43c8d2ffb9dc',
      game_id: '642fc6c1e8',
      response: 'json'
    },
    font: new ig.Font( 'media/04b03.font.png' ),
    background: new ig.Image('media/stats_screen.png'),

    init: function() {
      ig.input.bind(ig.KEY.ENTER, 'main-menu');
    },
    update: function() {
      if (ig.input.pressed('main-menu')) {
        ig.system.setGame(StartScreen);
      }
      this.parent();
    },

    loadScores: function() {
      $.post('https://www.scoreoid.com/api/getScores', this.scoreoid, function(data) {
        console.log(data);
      });
    },

    saveScore: function(username, score) {
    },

    draw: function() {
      this.parent();
      this.background.draw(0, 0);
      this.font.draw('You ' + (ig.global.score.ai == 0 ? 'won!' : 'loose!'), 200, 50, ig.Font.ALIGN.CENTER);
      this.font.draw('Press [ENTER] to move to the main menu', 200, 100, ig.Font.ALIGN.CENTER);
    }
  });

  StartScreen = ig.Game.extend({
    background: new ig.Image('media/main_screen.png'),
    clearColor: null,

    init: function() {
      ig.input.bind(ig.KEY.ENTER, 'start-game');
    },

    update: function() {
      if (ig.input.pressed('start-game')) {
        ig.system.setGame(RunningGame);
      }
      this.parent();
    },

    draw: function() {
      ig.system.context.clearRect( 0 ,0, ig.system.realWidth, ig.system.realHeight );
      this.parent();
      this.background.draw(0, 0);
    }
  });

  // Start the Game with 60fps, a resolution of 640x400 (16:10), scaled
  // up by a factor of 2
  ig.main( '#canvas', StartScreen, 60, 624, 384, 1 );
});

