/**
 * Created by apizzimenti on 5/19/16.
 */

"use strict";

/**
 * @author Anthony Pizzimenti
 *
 * @desc Provides an easy way to specify debugging within the Phaser game.
 *
 * @param game {object} Phaser game instance.
 *
 * @property game {game} Phaser game instance.
 * @property x {number} Middle of the Phaser game window.
 * @property y {number} Starting height to display debug information.
 * @property color {string} Hexadecimal color code.
 * @property on {boolean} Turns the debug information on and off.
 *
 * @class {object} Debug
 * @this Debug
 * @constructor
 */

function Debug (game) {

    this.game = game;
    this.x = this.game.width / 2;
    this.y = 20;
    this.color = "#FFF";
    this.on = true;
}

/**
 * @author Anthony Pizzimenti
 *
 * @desc Writes the current FPS to the game window.
 *
 * @this Debug
 */

Debug.prototype.fps = function () {
    if (this.on) {
        this.game.debug.text(this.game.time.fps, this.x, this.y, this.color);
    }
};

/**
 * @author Anthony Pizzimenti
 *
 * @desc Writes the current mouse position to the game window.
 *
 * @param mouse {Mouse} Mouse object.
 *
 * @this Debug
 *
 * @see mouseTrack
 */

Debug.prototype.mousePos = function (mouse) {
    if (this.on) {
        this.game.debug.text(`${mouse.tile.row}, ${mouse.tile.col}`, this.x, this.y, this.color);
    }
};

/**
 * @author Anthony Pizzimenti
 *
 * @desc Displays debug information and bodies for one or more sprites.
 *
 * @param sprites {Player | Animal | Item}
 *
 * @this Debug
 */

Debug.prototype.sprite = function (sprites) {

    if (this.on) {
        var debugSprite = (sprite) => {
            this.game.debug.text(`${sprite.sprite.row}, ${sprite.sprite.col}`, this.x, this.y, this.color);
            
            try {
                this.game.debug.body(sprite.sprite.tile.top, "#FF0000", false);
                this.game.debug.body(sprite.sprite, "#FFFFFF", false);
    
                for (var tile in sprite.sprite.tile) {
                    if (sprite.tile.hasOwnProperty(tile) && tile !== "top") {
                        this.game.debug.body(sprite.tile[tile], "#FFDD00", false);
                    }
                }
            } catch (e) {
                console.log("%cSprite has not loaded yet", "color: red");
            }
        };

        if (Array.isArray(sprites)) {

            for (var sprite of sprites) {
                debugSprite(sprite);
            }

        } else {
            debugSprite(sprites);
        }
    }
};

/**
 * @author Anthony Pizzimenti
 *
 * @desc Displays blocked tiles' debug bodies.
 *
 * @param tiles {sprite[]} An array of tile sprites.
 *
 * @this Debug
 */

Debug.prototype.tiles = function (tiles) {

    if (this.on) {
        for (var i = 0; i < tiles.length; i++) {
            for (var j = 0; j < tiles[0].length; j++) {
                var tile = tiles[i][j];

                if (tile.blocked) {
                    this.game.debug.body(tiles[i][j], "#FFDD00", false);
                }
            }
        }
    }
};

/**
 * @author Anthony Pizzimenti
 *
 * @desc Allows for future implementation of a _button on/off switch for displaying debug info.
 *
 * @this Debug
 *
 * @todo Implement buttons.
 */

Debug.prototype.switch = function () {
    this.on = !this.on;
};
@@ -719,19 +719,88 @@
 = true;%0A
+%0A    var graphics = game.add.graphics(0, 0),%0A        _this = this;%0A%0A
 %7D%0A%0A
+%0A
 /**%0A * @
@@ -935,24 +935,25 @@
 nction () %7B%0A
+%0A
     if (this
@@ -1275,24 +1275,25 @@
 n (mouse) %7B%0A
+%0A
     if (this
@@ -1337,21 +1337,16 @@
 $%7Bmouse.
-tile.
 row%7D, $%7B
@@ -1351,21 +1351,16 @@
 $%7Bmouse.
-tile.
 col%7D%60, t
@@ -1364,32 +1364,37 @@
 , this.x, this.y
+ + 20
 , this.color);%0A 
@@ -1702,104 +1702,128 @@
     
-this.game.debug.text(%60$%7Bsprite.sprite.row%7D, $%7Bsprite.sprite.col%7D%60, this.x, this.y, this.color
+%0A            try %7B%0A%0A                var s = sprite.sprite;%0A%0A                this.game.debug.body(s, %22#FFFFFF%22, false
 );%0A
+%0A
     
@@ -1830,35 +1830,38 @@
         
-%0A   
     
-     try %7B%0A
+if (s.tile) %7B%0A    
         
@@ -1890,28 +1890,16 @@
 g.body(s
-prite.sprite
 .tile.to
@@ -1940,68 +1940,45 @@
     
-this.game.debug.body(sprite.sprite, %22#FFFFFF%22, false);
+%7D%0A%0A                if (s.tile) %7B
 %0A    
-%0A
     
@@ -2007,28 +2007,16 @@
 ile in s
-prite.sprite
 .tile) %7B
@@ -2040,18 +2040,17 @@
     
+    
 if (s
-prite
 .til
@@ -2109,32 +2109,36 @@
                 
+    
 this.game.debug.
@@ -2143,21 +2143,16 @@
 g.body(s
-prite
 .tile%5Bti
@@ -2167,32 +2167,58 @@
 FDD00%22, false);%0A
+                        %7D%0A
                 
@@ -2295,55 +2295,47 @@
 ole.
-log(%22%25cSprite has not loaded yet%22, %22color: red%22
+warn(%60$%7Bsprite.type%7D is not yet loaded%60
 );%0A 

fix text heights