function Dodge() {
  this.enemies;
  this.player;
  this.gameOverText;
  this.timeText;
  this.timeToNextSpawn = 4000;
  this.totalEnemies = 1;
  this.timeSinceLastSpawn = 0;
  this.spawnRampFactor = 1.05;
  this.howLongYouLived = 0;
  this.deadSound;
  this.surviveSound;
}

Dodge.prototype = {
  preload: function() {
    this.load.image("paper", "assets/Dodge/paper_clean.png");
    this.load.image("pushpin", "assets/Dodge/pushpin_clean.png");
    this.load.image("stapler", "assets/Dodge/stapler_clean.png");
    this.load.image("paper_hit", "assets/Dodge/paper_hit.png");
    this.load.image("pushpin_hit", "assets/Dodge/pushpin_hit.png");
    this.load.image("stapler_hit", "assets/Dodge/stapler_hit.png");
  },
  create: function() {

    //background for game
    this.add.sprite(0,0, "bg");

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

    this.enemies = this.add.group();
    this.enemies.enableBody = true;
    this.enemies.physicsBodyType = Phaser.Physics.ARCADE;
    this.enemies.setAll("anchor.x", .5);
    this.enemies.setAll("anchor.y", .5);
    this.enemies.createMultiple(1, "paper");
    this.enemies.createMultiple(1, "pushpin");
    this.enemies.createMultiple(1, "stapler");
    this.enemies.createMultiple(1, "paper");
    this.enemies.createMultiple(1, "pushpin");
    this.enemies.createMultiple(1, "stapler");
    this.enemies.createMultiple(1, "paper");
    this.enemies.createMultiple(1, "pushpin");
    this.enemies.createMultiple(1, "stapler");
    this.enemies.createMultiple(1, "paper");
    this.enemies.createMultiple(1, "pushpin");
    this.enemies.createMultiple(1, "stapler");
    this.enemies.createMultiple(1, "paper");
    this.enemies.createMultiple(1, "pushpin");
    this.enemies.createMultiple(1, "stapler");
    this.enemies.createMultiple(1, "paper");
    this.enemies.createMultiple(1, "pushpin");
    this.enemies.createMultiple(1, "stapler");

    this.player = this.add.sprite(400, 400, "cursor");
    this.player.anchor.setTo(.5, .5);
    this.player.health = 1;
    this.physics.enable(this.player, Phaser.Physics.ARCADE);
    this.player.body.setSize(1, 1);

    this.ripped = this.add.sprite(this.game.world.centerX, this.game.world.centerY, "ripped_tall");
    this.ripped.anchor.setTo(.5, .5);
    this.ripped.visible = false;

    this.gameOverText = this.add.text(this.game.world.centerX, this.game.world.centerY, " ", { font: "50px LightUpTheWorld", fill: "#222" });
    this.gameOverText.anchor.setTo(.5, .5);
    this.gameOverText.align = "center";
    this.gameOverText.visible = false;

    this.timeText = this.add.text(0, 0, " 0", { font: "50px LightUpTheWorld", fill: "#222" });

    this.deadSound = this.add.sound("badSound");
    this.surviveSound = this.add.sound("winSound");

    this.addEnemy();

    this.timeToNextSpawn = 4000;
    this.totalEnemies = 1;
    this.timeSinceLastSpawn = 0;
    this.spawnRampFactor = 1.05;
    this.howLongYouLived = 0;
    this.processed = false;
  },
  update: function() {
    this.timeSinceLastSpawn += this.time.elapsed;
    if(!this.player.alive) {
      if(!this.processed) { // Have we already processed the point gain?
        this.processed = true;
        if(this.howLongYouLived / 1000 > 15) { // 15 Seconds or higher gets you a point
          FingerPuppets.score++;
          this.surviveSound.play();
        } else {
          this.deadSound.play();
        }

        this.timeSinceLastSpawn = 0;
      }


      var score = Math.round(this.howLongYouLived / 1000);
      this.gameOverText.text = "You died!\nYou lasted " + score + (score !== 1 ? " seconds": " second") + "\n\nTouch to continue";
      this.gameOverText.visible = true;
      this.ripped.visible = true;
      if(this.input.activePointer.justPressed() && this.timeSinceLastSpawn > 2000) { // Use timeSinceLastTimeSpawn to keep us from clicking for 2 secs
        this.game.state.start("hub");
      }

      return;
    }

    this.howLongYouLived += this.time.elapsed;

    this.timeText.text = " " + Math.round(this.howLongYouLived / 1000);

    if(this.shouldAddEnemy()) {
      this.totalEnemies++;
      this.addEnemy();
      this.timeSinceLastSpawn = 0;
      this.timeToNextSpawn = this.timeToNextSpawn * this.spawnRampFactor; // The next one should spawn a bit faster
    }

    this.player.x = this.input.activePointer.position.x;
    this.player.y = this.input.activePointer.position.y;
    
    this.enemies.forEachAlive(function(e) {
      if(!e.inWorld) {
        e.kill();
      }
    }, this);
    this.physics.arcade.overlap(this.enemies, this.player, this.onPlayerCollision, null, this);
  },

  shouldAddEnemy: function() {
    return this.timeSinceLastSpawn >= this.timeToNextSpawn;
  }, 
  addEnemy: function() {
    if(this.enemies.countLiving() >= this.totalEnemies) return;

    var newEnemy = this.enemies.getFirstExists(false);
    if(!newEnemy) return;
    newEnemy.events.onKilled.add(function() {
      this.addEnemy();
    }, this);

    var randX = this.rnd.integerInRange(0, this.game.width - 1);
    var randSpeed = this.rnd.integerInRange(100, 200);
    var randRot = this.rnd.integerInRange(4, 150);

    newEnemy.reset(randX, 0);
    newEnemy.loadTexture(newEnemy.key.replace("_hit", ""));
    newEnemy.alreadyHit = false;
    newEnemy.anchor.setTo(.5, .5);
    newEnemy.body.angularVelocity = randRot;
    this.physics.arcade.moveToObject(newEnemy, this.player, randSpeed);
    newEnemy.body.velocity.x += this.rnd.integerInRange(-100, 100);
  },
  onPlayerCollision: function(player, enemy) {
    if(!enemy.alreadyHit) {
      player.damage(1);
      enemy.alreadyHit = true;
      enemy.loadTexture(enemy.key + "_hit");


      
    }
  }
}

module.exports = Dodge;