/*
 * Copyright 2014, Gregg Tavares.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Gregg Tavares. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
"use strict";

define([
    'hft/misc/misc',
    'hft/misc/strings',
    '../bower_components/hft-utils/dist/2d',
    '../bower_components/hft-utils/dist/imageutils',
  ], function(
    Misc,
    Strings,
    M2D,
    ImageUtils) {

  var availableColors = [];
  var nameFontOptions = {
    font: "20px sans-serif",
    yOffset: 18,
    height: 20,
    fillStyle: "black",
  };

  /**
   * Player represnt a player in the game.
   * @constructor
   */
  var Player = (function() {
    return function(services, width, height, direction, name, netPlayer) {
      this.services = services;
      this.renderer = services.renderer;
      this.netPlayer = netPlayer;
      this.velocity = [0, 0];
      this.acceleration = [0, 0];
      if (availableColors.length == 0) {
        var colors = services.colors;
        for (var ii = 0; ii < colors.length; ++ii) {
          availableColors.push(colors[ii]);
        }
      }
      var colorNdx = Math.floor(Math.random() * availableColors.length);
      this.color = availableColors[colorNdx];
window.p = this;
      netPlayer.sendCmd('setColor', this.color);
      availableColors.splice(colorNdx, 1);
      this.color.id;
      this.animTimer = 0;
      this.width = width;
      this.height = height;
      this.canJump = false;
      this.checkWallOffset = [
        -this.width / 2,
        this.width / 2 - 1,
      ];
      this.timeAccumulator = 0;

      this.scoreLine = this.services.scoreManager.createScoreLine(this, this.color);
      this.scoreLine.ctx.drawImage(
        this.services.images.idle.imgColors[this.color.id][0], 0, 0);

      netPlayer.addEventListener('disconnect', Player.prototype.handleDisconnect.bind(this));
      netPlayer.addEventListener('setName', Player.prototype.handleNameMsg.bind(this));
      netPlayer.addEventListener('busy', Player.prototype.handleBusyMsg.bind(this));
      netPlayer.addEventListener('word choice', Player.prototype.handleWordChoiceMsg.bind(this));

      this.setName(name);
      this.direction = 0;         // direction player is pushing (-1, 0, 1)
      this.facing = direction;    // direction player is facing (-1, 1)
      this.score = 0;
      this.addPoints(0);

      this.reset();
      this.checkBounds();
    };
  }());

  Player.prototype.setName = function(name) {
    if (name != this.playerName) {
      this.playerName = name;
      this.scoreLine.setName(":" + name);
    }
  };

  Player.prototype.reset = function() {
    var levelManager = this.services.levelManager;
    var level = levelManager.getLevel();
    var position = levelManager.getRandomOpenPosition();
    this.position = [position.x, position.y];
    this.lastPosition = [this.position[0], this.position[1]];
  };

  Player.prototype.addPoints = function(points) {
    this.score += points;
    this.scoreLine.setMsg(Strings.padLeft(this.score, 3, "0"));
  };

  Player.prototype.removeFromGame = function() {
    this.services.playerManager.removePlayer(this);
    this.services.scoreManager.deleteScoreLine(this.scoreLine);
    availableColors.push(this.color);
  };

  Player.prototype.handleDisconnect = function() {
    this.removeFromGame();
  };

  Player.prototype.handleBusyMsg = function(msg) {
    // We ignore this message
  };
    
  Player.prototype.handleWordChoiceMsg = function(msg) {
    
    if ( this.services.wordManager.checkWord( msg.word ) )
    {
        this.addPoints( 1 );
        this.services.wordManager.randomizeWord();
    }
  };

  Player.prototype.handleNameMsg = function(msg) {
    if (!msg.name) {
      this.sendCmd('setName', {
        name: this.playerName
      });
    } else {
      this.setName(msg.name.replace(/[<>]/g, ''));
    }
  };

  Player.prototype.sendCmd = function(cmd, data) {
    this.netPlayer.sendCmd(cmd, data);
  };

  return Player;
});


@@ -2229,72 +2229,8 @@
 er;%0A
-      this.velocity = %5B0, 0%5D;%0A      this.acceleration = %5B0, 0%5D;%0A
     
@@ -2262,24 +2262,24 @@
 gth == 0) %7B%0A
+
         var 
@@ -2671,241 +2671,8 @@
 .id;
-%0A      this.animTimer = 0;%0A      this.width = width;%0A      this.height = height;%0A      this.canJump = false;%0A      this.checkWallOffset = %5B%0A        -this.width / 2,%0A        this.width / 2 - 1,%0A      %5D;%0A      this.timeAccumulator = 0;
 %0A%0A  
@@ -3257,156 +3257,8 @@
 e);%0A
-      this.direction = 0;         // direction player is pushing (-1, 0, 1)%0A      this.facing = direction;    // direction player is facing (-1, 1)%0A
     
@@ -3297,24 +3297,24 @@
 Points(0);%0A%0A
+
       this.r
@@ -3325,34 +3325,8 @@
 ();%0A
-      this.checkBounds();%0A
     

Remove more platforming from player.