var Ritual = Ritual || {};

Ritual.Player = function (game_state, position, properties) {
    "use strict";
    Ritual.Prefab.call(this, game_state, position, properties);	
	//this.animations.add('walk');
    //this.animations.play('walk', 10, true);
    
	this.speed = 0;
	this.steps = 0;
	this.curScale = 1;
	this.justHitted = false;
	this.lastSpeedChange = 0;
	this.leftPlayer = properties.leftPlayer;
	this.maxSpeed = 6;
	this.previousSpeedKey = 0;
    this.walking_speed = +properties.walking_speed;
    this.jumping_speed = +properties.jumping_speed;
    this.bouncing = +properties.bouncing;
	
	this.game_state.game.physics.arcade.enable(this);
    this.body.collideWorldBounds = true;
    
    this.anchor.setTo(0.5);
    
    //this.cursors = this.game_state.game.input.keyboard.createCursorKeys();
	if(properties.leftPlayer) {
		this.cursors = this.game_state.game.input.keyboard.addKeys( { 'up': Phaser.KeyCode.W, 'down': Phaser.KeyCode.S, 'left': Phaser.KeyCode.A, 'right': Phaser.KeyCode.D } );
	}
	else {
		this.cursors = this.game_state.game.input.keyboard.addKeys( { 'up': Phaser.KeyCode.UP, 'down': Phaser.KeyCode.DOWN, 'left': Phaser.KeyCode.LEFT, 'right': Phaser.KeyCode.RIGHT } );
	}
};

Ritual.Player.prototype = Object.create(Ritual.Prefab.prototype);
Ritual.Player.prototype.constructor = Ritual.Player;

Ritual.Player.prototype.update = function () {
    "use strict";
	
	if(this.game_state.readyFlag == false || this.speed < 0) {
		this.body.velocity.x = 0;
        //this.animations.stop();        
		return;
	}
	
	this.body.velocity.y = 1;
		
	this.game_state.game.physics.arcade.collide(this, this.game_state.groups.walls);//this.game_state.layers.collision);
    if(this.leftPlayer) {
		//this.game_state.game.physics.arcade.collide(this, this.game_state.groups.obstaclesLeft, this.hit_obstacle, null, this);
		this.game_state.game.physics.arcade.overlap(this, this.game_state.groups.obstaclesLeft, this.hit_obstacle);
	}
	else {
		//this.game_state.game.physics.arcade.collide(this, this.game_state.groups.obstaclesRight, this.hit_obstacle, null, this);
		this.game_state.game.physics.arcade.overlap(this, this.game_state.groups.obstaclesRight, this.hit_obstacle);
	}
    
	var seconds = new Date().getTime();
	
    if (this.cursors.right.isDown && this.body.velocity.x >= 0) {
        // move right
        this.body.velocity.x = this.walking_speed;
        this.scale.setTo(-1, 1);
    } else if (this.cursors.left.isDown && this.body.velocity.x <= 0) {
        // move left
        this.body.velocity.x = -this.walking_speed;
        this.scale.setTo(1, 1);
    }
	else {
        // stop
        this.body.velocity.x = 0;
        //this.frame = 3;
    }  
	
	if (this.cursors.down.isDown) {
		//speed down
		
		if(seconds - this.lastSpeedChange < 500) {
			return;
		}
		this.lastSpeedChange = seconds;
		
		this.speed = this.speed/2;
	} else if (this.cursors.up.isDown) {
		if(seconds - this.lastSpeedChange < 500) {
			return;
		}
		this.lastSpeedChange = seconds;
		
		// speed up
		if(this.speed < 0.2) {
			this.speed = 0.2;
		}
		else {
			this.speed = 2*this.speed;
			if(this.speed > this.maxSpeed) {
				this.speed = this.maxSpeed;
			}
		}
		
		if(this.speed > 0) {
			//console.log(this.speed);
			//this.animations.play('walk', 10, true);        
			//this.body.velocity.y = 0;
			this.curScale = -1 * this.curScale;
			this.scale.setTo(this.curScale, 1);
		}
		
		/*
		var speedUp;
		if(this.cursors.up.isDown) {
			speedUp = this.previousSpeedKey != this.cursors.up.keyCode;
			this.previousSpeedKey = this.cursors.up.keyCode;
		}
		else {
			speedUp = this.previousSpeedKey != this.cursors.down.keyCode;
			this.previousSpeedKey = this.cursors.down.keyCode;
		}
		
		if(speedUp) {
			if(this.speed <= 0) {
				this.speed = 0.5;
			}
			else {
				this.speed =  this.speed + this.speed * this.speed;
				if(this.speed > this.maxSpeed) {
					this.speed = this.maxSpeed;
				}
			}
		}
		*/
    }	
};

Ritual.Player.prototype.hit_obstacle = function (player, obstacle) {
    "use strict";
	//if(player.y > obstacle.y) {
	//	return;
	//}
	//console.log('hit_obstacle');
	// if the player is above the enemy, the enemy is killed, otherwise the player dies
    //if(obstacle.stone) {
		player.speed = 0;
		var seconds = new Date().getTime()/1000;
		if(seconds - obstacle.lastHit > 1) {
			obstacle.lastHit = new Date().getTime() / 1000;
			player.justHitted = true;
		}
	//}
	//else {
	//	obstacle.kill();
	//	player.speed = player.speed/4;
	//}
};