Alien.prototype = Object.create(Phaser.Sprite.prototype);
Alien.constructor = Alien;

function Alien(initialX, initialY, onGoOnstageFunction, onFiredFunction, onEscapedFunction) {
	// super();

	this.isActivated = false;
	this.isOnWaitingRoom = true;
	this.speedGrowing = Alien.DEFAULT_SPEED_SEC;

	this.initialX = initialX;
	this.initialY = initialY;

	this.tweenScaleUp = null;
	this.tweenScaleDown = null;

	this.onGoOnstageFunction = onGoOnstageFunction;
	this.onFiredFunction = onFiredFunction;
	this.onEscapedFunction = onEscapedFunction;

	Phaser.Sprite.call(this, game, this.initialX, this.initialY, 'alien_normal');
	this.anchor.set(0.5);
	this.goWaitingRoom();


	this.inputEnabled = true;
	this.events.onInputOut.add(this.onOut, this);
	this.events.onInputOver.add(this.onOver, this);
	this.events.onInputDown.add(this.onDown, this);

	game.add.existing(this);
}

Alien.prototype.onOut = function() {
	if(!this.isActivated)
		return;

	this.tint = 0xffffff;
}

Alien.prototype.onOver = function() {
	if(!this.isActivated)
		return;

	this.tint = 0xffaaaa;
}

Alien.prototype.onDown = function() {
	if(!this.isActivated)
		return;

	if(this.isOnWaitingRoom) {
		this.goOnstage();
		return;
	} else {
		this.onFired();
	}
}


Alien.prototype.stop = function() {
	this.setActive(false);
	this.removeTween();

	// if(this.isOnWaitingRoom !== true)
	// 	this.scale.set(0);
}

Alien.prototype.setActive = function(isActivated) {
	this.isActivated = isActivated;

	if(this.isActivated) {
		this.tint = 0xffffff;
	} else {
		this.tint = 0x555555;
	}
}

Alien.prototype.removeTween = function() {
	if(this.tweenScaleUp !== null) {
		this.tweenScaleUp.stop();
		this.tweenScaleUp = null;
	}
	if(this.tweenScaleDown !== null) {
		this.tweenScaleDown.stop();
		this.tweenScaleDown = null;
	}
}

Alien.prototype.goWaitingRoom = function() {
	this.isOnWaitingRoom = true;

	this.x = this.initialX;
	this.y = this.initialY;
	this.scale.set(Alien.WAITING_SCALE);
}

Alien.prototype.goOnstage = function() {
	this.isOnWaitingRoom = false;
	this.goRandomPosition();

	if(typeof this.onGoOnstageFunction !== "undefined")
		this.onGoOnstageFunction();
}

Alien.prototype.onFired = function() {
	if(typeof this.onFiredFunction !== "undefined")
		this.onFiredFunction(this);

	this.goRandomPosition();
}

Alien.prototype.goRandomPosition = function() {
	// set random position
	var SPAWN_BOX_LEFT		= 80;
	var SPAWN_BOX_WIDTH		= game.world.width - 160;
	var SPAWN_BOX_TOP		= 280;
	var SPAWN_BOX_HEIGHT	= game.world.height - 400;

	this.x = SPAWN_BOX_LEFT + Math.random() * SPAWN_BOX_WIDTH;
	this.y = SPAWN_BOX_TOP + Math.random() * SPAWN_BOX_HEIGHT;

	// if(isDebugMode()) {
	// 	this.game.add.graphics()
	// 		.beginFill(0xffffff, 0.1)
	// 		.drawRect(SPAWN_BOX_LEFT, SPAWN_BOX_TOP, SPAWN_BOX_WIDTH, SPAWN_BOX_HEIGHT);
	// }

	this.tint = 0xffffff;
	this.scale.set(Alien.ONSTAGE_MIN_SCALE);

	// start scale tween animation
	this.removeTween();

	this.tweenScaleUp = game.add.tween(this.scale)
		.to( { x: Alien.ONSTAGE_MAX_SCALE, y: Alien.ONSTAGE_MAX_SCALE }, Alien.DEFAULT_SPEED_SEC, Phaser.Easing.Linear.None, false);
	this.tweenScaleDown = game.add.tween(this.scale)
		.to( { x: 0, y: 0 }, Alien.DEFAULT_SPEED_SEC, Phaser.Easing.Linear.None, false);
	this.tweenScaleUp.chain(this.tweenScaleDown);
	this.tweenScaleDown.onComplete.addOnce(this.onEscaped, this);
	this.tweenScaleUp.start();
}

Alien.prototype.onEscaped = function() {
	if(!this.isActivated)
		return;

	if(typeof this.onEscapedFunction !== "undefined")
		this.onEscapedFunction();
}


Alien.DEFAULT_SPEED_SEC		= 3000;
Alien.WAITING_SCALE			= 2;
Alien.ONSTAGE_MIN_SCALE		= 0.1;
Alien.ONSTAGE_MAX_SCALE		= 3;
