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

function FlyingSaucer(lineIndex) {
	this.lineIndex = lineIndex;

	this.initVariables();


	Phaser.Sprite.call(this, game, game.world.centerX, game.world.centerY, 'alien_normal');
	game.add.existing(this);
	game.physics.arcade.enable(this);

	this.scale.set(1);
	this.anchor.set(0.5);
	this.halfWidth = this.width / 2;

	var COCKPIT_OFFSET_Y = -5;
	var BODY_OFFSET_Y = 28;
	var JET_OFFSET_Y = 52;

	var cockpit = this.makeCockpit(0, COCKPIT_OFFSET_Y);
	this.addChild(cockpit);

	this.jetLeft = this.makeJet(-50, JET_OFFSET_Y);
	this.addChild(this.jetLeft);

	this.jetCenter = this.makeJet(0, JET_OFFSET_Y);
	this.addChild(this.jetCenter);

	this.jetRight = this.makeJet(50, JET_OFFSET_Y);
	this.addChild(this.jetRight);

	var shipBody = this.makeShipBody(0, BODY_OFFSET_Y);
	this.addChild(shipBody);


	var style = { font: "20px Arial", fill: FlyingSaucer.TEXT_COLOR_NORMAL};
	this.wordText = game.add.text(0, 0, "text", style);
	this.wordText.anchor.set(0.5);

	this.moveInitialPos();

	// this.setActive(true);
}


FlyingSaucer.prototype.initVariables = function() {
	this.isActivated = false;
	this.speed = FlyingSaucer.DEFAULT_SPEED;
	this.timerEvent = null;
}

FlyingSaucer.prototype.makeCockpit = function(posX, posY) {
	var cockpit = game.make.sprite(posX, posY, 'ship_cockpit');
	cockpit.anchor.set(0.5);
	cockpit.alpha = 0.6;
	cockpit.tint = 0xff00ff;

	return cockpit;
}

FlyingSaucer.prototype.makeJet = function(posX, posY) {
	var jet = game.make.sprite(posX, posY, 'ship_jet');
	jet.anchor.set(0.5);
	jet.tint = 0xffff00;

	return jet;
}

FlyingSaucer.prototype.makeShipBody = function(posX, posY) {
	var body = game.make.sprite(posX, posY, 'ship_body');
	body.anchor.set(0.5);

	return body;
}



FlyingSaucer.prototype.update = function() {
	if(this.isActivated) {
		var deltaTime = game.time.elapsed / 1000;
		this.y += this.speed * deltaTime;

		this.wordText.y = this.y + FlyingSaucer.WORD_TEXT_OFFSET_Y;

		if(this.y > GAME_SCREEN_SIZE.y - 200)
			this.moveInitialPos();
	} else {
		this.stop();
	}
}


FlyingSaucer.prototype.setup = function(wordLevel, speedLevel) {
	this.wordLevel = wordLevel;
	this.speedLevel = speedLevel;
}



FlyingSaucer.prototype.stop = function() {
	this.body.velocity.setTo(0, 0);
}

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

	if(this.isActivated) {
		this.alpha = 1;
	} else {
		this.alpha = 0;
		this.wordText.text = "";
	}
}

FlyingSaucer.prototype.moveInitialPos = function() {
	console.log("moveInitialPos");
	this.x = 100 + this.lineIndex * 200;
	this.y = 100;

	this.wordText.x = this.x;
	this.wordText.y = this.y + FlyingSaucer.WORD_TEXT_OFFSET_Y;

	this.explodeEmitter = game.add.emitter(
		this.x,
		this.y,
		10
	);
	this.explodeEmitter.makeParticles('star');
	this.explodeEmitter.minParticleScale = 1;
	this.explodeEmitter.maxParticleScale = 3;
	this.explodeEmitter.forEach(
		function(particle) {
			particle.tint = 0xadff2f;
		}
	);
	this.explodeEmitter.gravity = 0;
}

FlyingSaucer.prototype.reset = function() {
	this.moveInitialPos();
	this.wordText.text = "text";
	this.setActive(true);
}

FlyingSaucer.prototype.checkInputText = function(inputText) {
	// console.log("inputText : " + inputText);
	// console.log("this.wordText : " + this.wordText);

	this.wordText.clearColors();
	this.wordText.addColor(FlyingSaucer.TEXT_COLOR_NORMAL, 0);

	if(inputText == null || inputText.length == 0)
		return;

	var word = this.wordText.text;
	var inputTextLength = inputText.length;
	var index = 0;
	for(; index < inputTextLength; index++) {
		// console.log("i : " + index);
		// console.log("inputTextLength : " + inputTextLength);
		// console.log("inputText.charAt(i) : " + inputText.charAt(index));
		// console.log("this.wordText.text.charAt(i) : " + this.wordText.text.charAt(index));

		if(index == 0 && word.charAt(index) == inputText.charAt(index)) {
			this.wordText.addColor(FlyingSaucer.TEXT_COLOR_CORRECT, index);
		} else if(word.charAt(index) != inputText.charAt(index)) {
			this.wordText.addColor(FlyingSaucer.TEXT_COLOR_NORMAL, index);
			return;
		}
	}
	this.wordText.addColor(FlyingSaucer.TEXT_COLOR_NORMAL, index);
}

FlyingSaucer.prototype.attack = function(inputText) {
	// console.log("inputText : " + inputText);
	// console.log("this.wordText : " + this.wordText);

	var word = this.wordText.text;

	if(word == inputText) {
		this.explode();
		return;
	}
}

FlyingSaucer.prototype.explode = function(inputText) {
	this.setActive(false);

	this.explodeEmitter.start(true, 1000, null, 100);

	this.timerEvent = game.time.events.add(Phaser.Timer.SECOND, this.reset, this);
}



FlyingSaucer.loadResource = function() {
	if(!game.cache.checkImageKey('alien_normal'))
		game.load.image('alien_normal',	'../../../resources/image/character/alien/green_alien.png');

	if(!game.cache.checkImageKey('ship_cockpit'))
		game.load.image('ship_cockpit',	'../../../resources/image/character/alien/cockpit.png');

	if(!game.cache.checkImageKey('ship_body'))
		game.load.image('ship_body',	'../../../resources/image/character/alien/ship.png');

	if(!game.cache.checkImageKey('ship_jet'))
		game.load.image('ship_jet',	'../../../resources/image/character/alien/jet.png');
}



FlyingSaucer.DEFAULT_SPEED		= 10;	// dot per sec

FlyingSaucer.WORD_TEXT_OFFSET_Y		= 30;	// px

FlyingSaucer.TEXT_COLOR_NORMAL			= "#000000";
FlyingSaucer.TEXT_COLOR_CORRECT			= "#ffff4d";