var goPlayer = function (game, options, api) {
    var self = {};
    self.name = "player";
    self.isOver = false;
    self.mouseDownStartPoint = null;
    var player = null;
    var exclamation = null;
    var startPos = null;

    // 0 = happy
    // 1 = moving
    // 2 = whutnow
    // 3 = dead
    var fishStatus = 0;
    var msInCurrentStatus = 0;

	var dieSfxarray = [];
	var moveSfxarray = [];
	var whatnowSfxarray = [];

    self.getObject = function () {
        return player;
    }

    self.moveObject = function (relX, relY) {
        player.x += relX;
    }

    self.setInitialPosition = function (point) {
        startPos = point;
    }

    self.preload = function () {
        
    }

    self.create = function () {
		//die-sounds
		dieSfxarray.push(game.add.audio('fish_die1'));
		dieSfxarray.push(game.add.audio('fish_die2'));
		dieSfxarray.push(game.add.audio('fish_die3'));
		dieSfxarray.push(game.add.audio('fish_die4'));
		dieSfxarray.push(game.add.audio('fish_die5'));
		//move-sounds
		moveSfxarray.push(game.add.audio('fish_move1'));
		moveSfxarray.push(game.add.audio('fish_move2'));
		moveSfxarray.push(game.add.audio('fish_move3'));
		moveSfxarray.push(game.add.audio('fish_move4'));
		moveSfxarray.push(game.add.audio('fish_move5'));
		moveSfxarray.push(game.add.audio('fish_move6'));
		moveSfxarray.push(game.add.audio('fish_move7'));
		//whatnow-sounds
		whatnowSfxarray.push(game.add.audio('fish_whatnow1'));
		whatnowSfxarray.push(game.add.audio('fish_whatnow2'));
		whatnowSfxarray.push(game.add.audio('fish_whatnow3'));
		whatnowSfxarray.push(game.add.audio('fish_whatnow4'));
		whatnowSfxarray.push(game.add.audio('fish_whatnow5'));
		whatnowSfxarray.push(game.add.audio('fish_whatnow6'));
		whatnowSfxarray.push(game.add.audio('fish_whatnow7'));
		whatnowSfxarray.push(game.add.audio('fish_whatnow8'));
		whatnowSfxarray.push(game.add.audio('fish_whatnow9'));
		whatnowSfxarray.push(game.add.audio('fish_whatnow10'));
		whatnowSfxarray.push(game.add.audio('fish_whatnow11'));


		player = game.add.sprite(startPos.x, startPos.y, self.name);
		player.getCustomState = function () {
		    return fishStatus;
		}
        //self.player = player;
        player.animations.add("swim");
        player.animations.play('swim', 20, true);
        player.anchor.setTo(0, 0);

        // Enable input events
        player.inputEnabled = true;
        player.events.onInputOver.add(self.onInputOver, this);
        player.events.onInputOut.add(self.onInputOut, this);
        player.events.onInputDown.add(self.onInputDown, this);
        player.events.onInputUp.add(self.onInputUp, this);

        game.physics.enable(player, Phaser.Physics.ARCADE);
        player.body.maxVelocity.setTo(options.MAX_SPEED, options.MAX_SPEED);

        // Add drag to the player that slows it down when it is moving
        player.body.drag.setTo(options.DRAG, options.DRAG); // x, y

        exclamation = game.add.sprite(startPos.x, startPos.y, "exclamation");
        exclamation.visible = false;
        exclamation.animations.add("notify");
        exclamation.animations.play("notify", 20, true);
        exclamation.anchor.setTo(0, 0);
    }

    var whutNowTimer = game.rnd.between(40, 60) * 100;

    self.update = function (timeDelta) {
        if (fishStatus == 3) {
            return;
        }

        //game.physics.arcade.collide(player, api.getObjectByName("ground"));
        var enemies = api.getObjectsByName("mureena");
        //game.physics.arcade.collide(player, enemies, self.Die, null, this);
        for (var i = 0; i < enemies.length; i++) {
            var overlaps = false;
            // Way too crude collision detection
            if (player.x > enemies[i].x - (enemies[i].width / 2) &&
                player.x < enemies[i].x + (enemies[i].width / 2) &&
                enemies[i].animations.currentFrame.index > 28) {
                overlaps = true;
            }
            if (overlaps === true) {
                self.Die();
            }
        }
        


        msInCurrentStatus += timeDelta;

        switch (fishStatus) {
            case 0: // Happy
                //TODO: animations
                if (msInCurrentStatus >= whutNowTimer) {
                    self.changeState(2);
                }
                break;
            case 1: // Moving
                if (msInCurrentStatus >= 2000) {
                    self.changeState(0);
                }
                break;
            case 2: // Whutnow
                // TODO: play sound
                if (msInCurrentStatus >= 5000) {
                    self.Die();
                }
            default:
                break;
        }

        self.pollKeyboard();
        player.body.acceleration.setTo(0, 0);
    }

    self.render = function () {
        //game.debug.text(fishStatus, player.x, player.y);
    }

    self.onInputOver = function (sprite, event) {
        if (fishStatus == 3)
            return;
        self.isOver = true;
    }

    self.onInputOut = function (sprite, event) {
        self.isOver = false;
    }

    self.onInputDown = function (sprite, event) {
        if (self.isOver === true) {
            self.mouseDownStartPoint = new Phaser.Point(event.x, event.y);
        }
    }

    self.onInputUp = function (sprite, event) {
        if (self.mouseDownStartPoint) {
            var mouseUpPoint = new Phaser.Point(event.x, event.y);
            var distance = self.mouseDownStartPoint.distance(mouseUpPoint);
            var angle = self.mouseDownStartPoint.angle(mouseUpPoint, true);
            player.body.velocity.x = (mouseUpPoint.x - self.mouseDownStartPoint.x) * 4;
            player.body.velocity.y = (mouseUpPoint.y - self.mouseDownStartPoint.y) * 4;

			moveSfxarray[game.rnd.between(0, 6)].play();

            //player.body.acceleration.x = Math.cos(angle) * distance;
            //player.body.acceleration.y = Math.sin(angle) * distance;

            //console.log("distance: " + distance + ", angle: " + angle);
            self.mouseDownStartPoint = null;

            self.changeState(1);
        }
    }

    self.pollKeyboard = function () {
        if (game.input.keyboard.isDown(Phaser.Keyboard.K)) {
            self.Die();
        }
        return;
    }

    self.Die = function () {
        //player.alive = false;
        self.changeState(3);
        game.world.setChildIndex(player, game.world.countLiving() - 1);
    }

    self.changeState = function (newState) {
        if (fishStatus == newState)
            return;

        switch (newState) {
            case 0: // Happy
                

                break;
            case 1: // Moving
                if(fishStatus == 2) {
                    player.loadTexture(self.name);
                    player.animations.play('swim', 20, true);
                    exclamation.visible = false;
                }
                break;
            case 2: // Whutnow
                player.loadTexture(self.name + "2");
                exclamation.visible = true;
                exclamation.x = player.x;
                exclamation.y = player.y;
                if (!player.animations.getAnimation("panic")) {
                    player.animations.add("panic");
                }
                player.animations.play('panic', 20, true);
                
				whatnowSfxarray[game.rnd.between(0, 10)].play();

                break;
            case 3:
                player.loadTexture(self.name + "3");
                exclamation.visible = false;
                if (!player.animations.getAnimation("die")) {
                    player.animations.add("die");
                }
                player.animations.play('die', 20, false);
				dieSfxarray[game.rnd.between(0, 4)].play();

				//rise and fade
				player.alpha = 1;
				game.add.tween(player).to( { alpha: 0 }, 2000, Phaser.Easing.Linear.None, true, 1500, 0, false);
                player.body.drag.setTo(0, 0);
				player.body.velocity.y=-30;
				break;
        }
        fishStatus = newState;
        msInCurrentStatus = 0;
    }

    return self;
}