/* global Kiwi */
'use strict';

var HelixPiEditor = HelixPiEditor || {};

HelixPiEditor.Play = new Kiwi.State('Play');

HelixPiEditor.Play.create = function () {
  this.entity = new Kiwi.GameObjects.Sprite(this, this.textures.entity, 100, 100, true);
  this.addChild(this.entity);
  this.entity.input.enableDrag();

  this.game.input.keyboard.onKeyDown.add(
    this.onPress,
    this
  );

  this.frameText = new Kiwi.GameObjects.TextField(this, 'test', 10, 10, '#FFF');

  this.addChild(this.frameText);

  this.currentFrame = 0;

  this.positions = {};

  this.api = (function(entity) {
    return {
      move: function(coordinates) {
        entity.x += coordinates.x;
        entity.y += coordinates.y;
      }
    }
  });

  this.compiledApi = this.api(this.entity);
};

HelixPiEditor.Play.update = function () {
  Kiwi.State.prototype.update.call(this);

  this.frameText.text = ['Frame: ', this.currentFrame].join('');

  if (this.play) {
    var compiledApi = this.compiledApi;
    _.each(this.codeToPlay, function (instruction) {
      instruction(compiledApi);
    });
  }
};

HelixPiEditor.Play.createScenario = function () {
  var startPosition = this.positions[0];
  var expectedEndPosition = this.positions[1];

  return {
    startingPosition: function () {
      return JSON.parse(JSON.stringify(startPosition));
    },

    expectedEndPosition: expectedEndPosition,

    duration: 60,

    fitness: function (entity) {
      var distance = {
        x: Math.abs(expectedEndPosition.x - entity.x),
        y: Math.abs(expectedEndPosition.y - entity.y)
      };

      return 1000 - (distance.x + distance.y);
    }
  };
};

HelixPiEditor.Play.savePosition = function () {
  this.positions[this.currentFrame] = {
    x: this.entity.x,
    y: this.entity.y
  };
};

HelixPiEditor.Play.loadPosition = function () {
  if (!this.positions[this.currentFrame]) {
    return;
  };

  this.entity.x = this.positions[this.currentFrame].x;
  this.entity.y = this.positions[this.currentFrame].y;
};

HelixPiEditor.Play.onPress = function (keyCode) {
  if (keyCode === Kiwi.Input.Keycodes.RIGHT) {
    this.savePosition();
    this.currentFrame += 1;
    this.loadPosition();
  }

  if (keyCode === Kiwi.Input.Keycodes.LEFT) {
    this.savePosition();
    this.currentFrame -= 1;
    this.loadPosition();
  }

  if (keyCode === Kiwi.Input.Keycodes.R) {
    this.results = helixPi(this.createScenario(), this.api);
    this.currentFrame = 0;
    this.loadPosition();
    this.play = true;
    this.entity.x = 100;
    this.entity.y = 100;
    this.codeToPlay = this.results[0].entity.individual;
    console.log(this.results[0].fitness);
    this.renderCode(this.results[0].entity.individual);
  }
};

HelixPiEditor.Play.renderCode = function (lines) {
  console.log(lines.map(function(f) { return f.toSource(); }).join('\n'));
};

@@ -547,16 +547,42 @@
 me = 0;%0D
+%0A  this.highestFrame = 0;%0D
 %0A%0D%0A  thi
@@ -595,18 +595,18 @@
 tions = 
-%7B%7D
+%5B%5D
 ;%0D%0A%0D%0A  t
@@ -1266,27 +1266,24 @@
 expected
-End
 Position
  = this.
@@ -1266,32 +1266,33 @@
 expectedPosition
+s
  = this.position
@@ -1296,11 +1296,40 @@
 ions
-%5B1%5D
+.slice(1, this.highestFrame + 1)
 ;%0D%0A%0D
@@ -1457,27 +1457,24 @@
 expected
-End
 Position
 : expect
@@ -1465,16 +1465,17 @@
 Position
+s
 : expect
@@ -1480,40 +1480,143 @@
 cted
-EndPosition,%0D%0A%0D%0A    duration: 60
+Positions.map(function(expectedPosition, index) %7B%0D%0A      expectedPosition.frame = index * 60;%0D%0A%0D%0A      return expectedPosition;%0D%0A    %7D)
 ,%0D%0A%0D
@@ -1639,16 +1639,34 @@
 nction (
+expectedPosition, 
 entity) 
@@ -1712,35 +1712,32 @@
 ath.abs(expected
-End
 Position.x - ent
@@ -1777,11 +1777,8 @@
 cted
-End
 Posi
@@ -1921,24 +1921,123 @@
 ction () %7B%0D%0A
+  if (this.currentFrame %3E this.highestFrame) %7B%0D%0A    this.highestFrame = this.currentFrame;%0D%0A  %7D%0D%0A%0D%0A
   this.posit

Add multiple waypoint support