var GameState = function(game){};

GameState.prototype.preload = function() {
};

GameState.prototype.create = function() {
  this.game.stage.backgroundColor = 0xBF844F;

  this.game.physics.startSystem(Phaser.Physics.ARCADE);

  this.sounds = {
    //tada: this.game.add.audio("tada")
  };

  this.groups = {
    bg: this.game.add.group(),
    enemies: this.game.add.group(),
    players: this.game.add.group(),
    enemyBullets: this.game.add.group(),
    playerBullets: this.game.add.group(),
    enemyHits: this.game.add.group(),
    playerHits: this.game.add.group(),
    lasso: this.game.add.group(),
    dialogs: this.game.add.group(),
    spawnerIcons: this.game.add.group(),
    lifeCounters: this.game.add.group()
  };

  // Life counters
  for (var i = 2; i >= 0; i--) {
    this.groups.lifeCounters.add(
      this.game.make.sprite(
        10 + 16*i, SCREEN_HEIGHT - 10 - 16, 'health'
      )
    );
  }

  // Spawn counters
  this.spawner = new Spawner(
    this.game, this.groups.spawnerIcons
  );

  this.spawnPlayer();

  // Add some enemies
  new Enemy(
    this.game,
    this.groups.enemies, this.groups.enemyBullets,
    this.groups.enemyHits,
    this.groups.players,
    80, 60, EnemyTypes.outlaw, true);
  new Enemy(
    this.game,
    this.groups.enemies, this.groups.enemyBullets,
    this.groups.enemyHits,
    this.groups.players,
    90, 50, EnemyTypes.outlaw, true);
  new Enemy(
    this.game,
    this.groups.enemies, this.groups.enemyBullets,
    this.groups.enemyHits,
    this.groups.players,
    200, 90, EnemyTypes.outlaw, true);

  this.dialog = new Dialog(this.game, this.groups.dialogs,0, 0);
  // Hide dialog initially
  this.groups.dialogs.alpha = 0;

  this.keys = this.game.input.keyboard.addKeys({
    up: Phaser.Keyboard.UP,
    down: Phaser.Keyboard.DOWN,
    left: Phaser.Keyboard.LEFT,
    right: Phaser.Keyboard.RIGHT,
    fire: Phaser.Keyboard.Z,
    lasso: Phaser.Keyboard.X,
    spawn1: Phaser.Keyboard.ONE,
    spawn2: Phaser.Keyboard.TWO,
    spawn3: Phaser.Keyboard.THREE
  });
  this.keys.spawn1.onDown.add(function() {
    if (this.spawner.trySpawn('outlaw')) {
      this.spawnAlly('outlaw');
    }
  }, this);
  this.keys.spawn2.onDown.add(function() {
    if (this.spawner.trySpawn('cowboy')) {
      this.spawnAlly('cowboy');
    }
  }, this);
  this.keys.spawn2.onDown.add(function() {
    if (this.spawner.trySpawn('bandito')) {
      this.spawnAlly('bandito');
    }
  }, this);
};

GameState.prototype.spawnPlayer = function() {
  if (this.groups.lifeCounters.total === 0) {
    console.log('GAME OVER!');
    // TODO: game over screen, reset to title
  } else {
    this.player = new Player(
      this.game,
      this.groups.players, this.groups.playerBullets,
      this.groups.playerHits, this.groups.lasso,
      SCREEN_WIDTH / 2, SCREEN_HEIGHT - 32, []);
    this.playerRespawnCounter = 2000;
    this.groups.lifeCounters.getFirstExists().destroy();
  }
};

GameState.prototype.spawnAlly = function(key) {
  // Spawn ally near where player is
  var x = Phaser.Math.clamp(
    (Math.random() - 0.5) * 100 + this.player.x,
    0, SCREEN_WIDTH);
  new Enemy(
    this.game,
    this.groups.players, this.groups.playerBullets,
    this.groups.playerHits,
    this.groups.enemies,
    x, SCREEN_HEIGHT, EnemyTypes[key], false);
}

GameState.prototype.update = function() {
  // Move using arrow keys
  var dx = 0;
  var dy = 0;
  if (this.keys.left.isDown) {
    dx = -1;
  } else if (this.keys.right.isDown) {
    dx = 1;
  }
  if (this.keys.up.isDown) {
    dy = -1;
  } else if (this.keys.down.isDown) {
    dy = 1;
  }
  this.player.move(dx, dy);

  // firing
  if (this.keys.fire.isDown) {
    this.player.fire();
  }

  // Lassoing
  if (this.keys.lasso.isDown) {
    this.player.lasso();
  }

  // Depth sort
  this.groups.enemies.sort(
    'y', Phaser.Group.SORT_ASCENDING);
  this.groups.players.sort(
    'y', Phaser.Group.SORT_ASCENDING);

  // Player bullets to enemy
  this.game.physics.arcade.overlap(
    this.groups.playerHits, this.groups.enemies,
    function(hit, enemy) {
      // TODO: enemy kill effects
      hit.kill();
      enemy.kill();
    }
  );

  // Lasso
  this.game.physics.arcade.overlap(
    this.groups.lasso, this.groups.enemies,
    function(lasso, enemy) {
      enemy.capture();
      lasso.kill();
      this.spawner.add(enemy.key);
    }, null, this
  );

  // Enemy bullets to players
  this.game.physics.arcade.overlap(
    this.groups.enemyHits, this.groups.players,
    function(hit, player) {
      if (player.invincibilityCounter > 0) {
        // Can't kill when invincible
        return;
      }
      player.kill();
    }
  );

  // Player respawn
  if (!this.player.alive) {
    this.playerRespawnCounter -= this.game.time.elapsed;
    if (this.playerRespawnCounter <= 0) {
      this.spawnPlayer();
    }
  }

  // TODO: enemy spawning
};

@@ -1058,511 +1058,148 @@
 s%0A  
-new Enemy(%0A    this.game,%0A    this.groups.enemies, this.groups.enemyBullets,%0A    this.groups.enemyHits,%0A    this.groups.players,%0A    80, 60, EnemyTypes.outlaw, true);%0A  new Enemy(%0A    this.game,%0A    this.groups.enemies, this.groups.enemyBullets,%0A    this.groups.enemyHits,%0A    this.groups.players,%0A    90, 50, EnemyTypes.outlaw, true);%0A  new Enemy(%0A    this.game,%0A    this.groups.enemies, this.groups.enemyBullets,%0A    this.groups.enemyHits,%0A    this.groups.players,%0A    200, 90, EnemyTypes.outlaw, true
+this.spawnEnemy('outlaw');%0A  this.spawnEnemy('outlaw');%0A  this.spawnEnemy('outlaw');%0A  this.spawnEnemy('outlaw');%0A  this.spawnEnemy('outlaw'
 );%0A%0A
@@ -2927,16 +2927,293 @@
 alse);%0A%7D
+;%0A%0AGameState.prototype.spawnEnemy = function(key) %7B%0A  // Spawn enemy along top%0A  new Enemy(%0A    this.game,%0A    this.groups.enemies, this.groups.enemyBullets,%0A    this.groups.enemyHits,%0A    this.groups.players,%0A    Math.random() * SCREEN_WIDTH, 0,%0A    EnemyTypes%5Bkey%5D, true);%0A%7D;
 %0A%0AGameSt

Refactor spawn enemy