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, 'ship_cockpit');
	game.add.existing(this);
	game.physics.arcade.enable(this);

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

	var ALIEN_OFFSET_Y = -12;
	var BODY_OFFSET_Y = 14;
	var JET_OFFSET_Y = 40;

	this.cockpit = this.makeAlien(0, ALIEN_OFFSET_Y);
	this.addChild(this.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);

	this.explodeEmitter = this.makeExplodeEmitter();


	var style = { font: "24px Arial", fill: FlyingSaucer.TEXT_COLOR_NORMAL};
	this.wordText = game.add.text(0, 0, "text", style);
	// this.wordText.fontWeight = "bold";
	// this.wordText.stroke = FlyingSaucer.TEXT_STROKE_COLOR;
	// this.wordText.strokeThickness = 3;
	this.wordText.anchor.set(0.5);

	this.setActive(false);
	this.setVisible(false);
}


FlyingSaucer.prototype.initVariables = function() {
	this.isActivated = false;
	this.isGameOver = false;

	this.wordLevel = 0;
	this.difficultyForLevel = 0;
	this.standardScore = 0;
	this.speed = FlyingSaucer.DEFAULT_SPEED;
	this.jetStatus = FlyingSaucer.JET_STATUS_HIGH;

	this.scoreManager = null;
	this.heartManager = null;
	this.extractWordManager = null;
	this.gameLevelManager = null;

	this.timerEventExplode = null;

	this.tweenScale = null;
}

FlyingSaucer.prototype.makeAlien = function(posX, posY) {
	var alien = game.make.sprite(posX, posY, 'alien_normal');
	alien.anchor.set(0.5);

	return alien;
}

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

	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.makeExplodeEmitter = function() {
	explodeEmitter = game.add.emitter(this.x, this.y, 10);
	explodeEmitter.makeParticles('star');
	explodeEmitter.minParticleScale = 1;
	explodeEmitter.maxParticleScale = 3;
	explodeEmitter.forEach( function(particle) { particle.tint = 0xadff2f; } );
	explodeEmitter.gravity = 0;

	return explodeEmitter;
}


FlyingSaucer.prototype.update = function() {
	if(this.isGameOver)
		return;

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

		this.updateJetColor();

		if(this.y > FlyingSaucer.CRASH_POS_Y)
			this.crash();
	} else {
		this.stop();
	}
}

FlyingSaucer.prototype.updateJetColor = function() {
	var landingRatio = this.getLandingRatio();

	if(this.jetStatus == FlyingSaucer.JET_STATUS_HIGH && landingRatio < GameLevelManager.LEVEL_UP_LANDING_RATIO) {
		this.jetStatus = FlyingSaucer.JET_STATUS_MIDDLE;
		this.changeJetColor(FlyingSaucer.JET_COLOR_MIDDLE);
	} else if(this.jetStatus == FlyingSaucer.JET_STATUS_MIDDLE && landingRatio < GameLevelManager.WARNING_LANDING_RATIO) {
		this.jetStatus = FlyingSaucer.JET_STATUS_LOW;
		this.changeJetColor(FlyingSaucer.JET_COLOR_LOW);
	}
}

FlyingSaucer.prototype.changeJetColor = function(color) {
	this.jetLeft.tint = color;
	this.jetCenter.tint = color;
	this.jetRight.tint = color;
}

FlyingSaucer.prototype.changeCockpitColor = function(color) {
	this.tint = GameLevelManager.COCKPIT_COLOR_LIST[this.wordLevel];
}


FlyingSaucer.prototype.setScoreManager = function(scoreManager) {
	this.scoreManager = scoreManager;
}

FlyingSaucer.prototype.setHeartManager = function(heartManager) {
	this.heartManager = heartManager;
}

FlyingSaucer.prototype.setExtractWordManager = function(extractWordManager) {
	this.extractWordManager = extractWordManager;
}

FlyingSaucer.prototype.setGameLevelManager = function(gameLevelManager) {
	this.gameLevelManager = gameLevelManager;
}




FlyingSaucer.prototype.setWord = function(wordText) {
	this.wordText.text = wordText;
}


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

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

FlyingSaucer.prototype.setVisible = function(isVisible) {
	this.isVisible = isVisible;

	if(this.isVisible)
		this.alpha = 1;
	else
		this.alpha = 0;
}

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

	this.scale.set(1);

	this.speed += FlyingSaucer.SPEED_UP_AMOUNT;

	this.changeCockpitColor();
	this.jetStatus = FlyingSaucer.JET_STATUS_HIGH;
	this.changeJetColor(FlyingSaucer.JET_COLOR_HIGH);

	this.wordText.x = this.x;
	this.wordText.y = this.y + FlyingSaucer.WORD_TEXT_OFFSET_Y;
	this.wordText.clearColors();
	this.wordText.addColor(FlyingSaucer.TEXT_COLOR_NORMAL, 0);
}


FlyingSaucer.prototype.startAttack = function() {
	if(this.isGameOver)
		return;

	this.wordLevel = this.gameLevelManager.getWordLevel();
	this.difficultyForLevel = this.gameLevelManager.getDifficultyForLevel(this.wordLevel);
	// console.log("====");
	// console.log("this.wordLevel : " + this.wordLevel);
	// console.log("this.difficultyForLevel : " + this.difficultyForLevel);
	this.word = this.extractWordManager.getWord(this.wordLevel, this.difficultyForLevel);
	// console.log("this.word : " + this.word);
	this.setWord(this.word);
	this.standardScore = this.gameLevelManager.getStandardScore(this.wordLevel, this.difficultyForLevel, this.speed);
	// console.log(this.standardScore);

	this.moveInitialPos();
	this.setActive(true);
	this.setVisible(true);
}

FlyingSaucer.prototype.checkInputText = function(inputText) {
	if(!this.isActivated)
		return;

	// 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;
	if(word.length < inputTextLength)
		return;

	var index = 0;
	for(; index < inputTextLength; 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.getLandingRatio = function(inputText) {
	var heightDown = FlyingSaucer.CRASH_POS_Y - FlyingSaucer.START_POS_Y;
	var distanceDown = this.y - FlyingSaucer.START_POS_Y;
	return (heightDown - distanceDown) / heightDown;
}

FlyingSaucer.prototype.checkWord = function(inputText) {
	if(!this.isActivated)
		return;

	// console.log("inputText : " + inputText);
	// console.log("this.wordText : " + this.wordText);

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

	if(this.word == inputText) {
		this.gameLevelManager.clearWord(this.wordLevel, this.getLandingRatio());
		this.extractWordManager.giveBackWord(this.word, this.wordLevel, this.difficultyForLevel);
		this.setActive(false);
		this.setVisible(false);
		this.setWord("");

		this.explode();
		this.plusScore();
		return;
	}
}

FlyingSaucer.prototype.explode = function(inputText) {
	this.explodeEmitter.x = this.x;
	this.explodeEmitter.y = this.y;
	this.explodeEmitter.start(true, 1000, null, 100);

	var randomDelayTime = Phaser.Timer.SECOND / 2 + Math.random() * Phaser.Timer.SECOND * 3;
	this.timerEventExplode = game.time.events.add(randomDelayTime, this.startAttack, this);
}

FlyingSaucer.prototype.plusScore = function(inputText) {
	// console.log(this.standardScore);
	// console.log(this.getLandingRatio());
	var totalScore = Math.floor(this.standardScore * this.getLandingRatio());
	this.scoreManager.plusScore(totalScore);
	var scoreText = new ScoreText(this.x, this.y, totalScore);
}

FlyingSaucer.prototype.crash = function() {
	this.setActive(false);
	this.wordText.text = "";

	this.tweenScale = game.add.tween(this.scale);
	this.tweenScale.to( {x: 10, y: 10}, Phaser.Timer.SECOND / 2, Phaser.Easing.Linear.None);
	this.tweenScale.onComplete.addOnce(this.respawn, this);
	this.tweenScale.start();

	this.heartManager.breakHearts(1);
}

FlyingSaucer.prototype.respawn = function() {
	this.tweenScale = null;

	if(this.isGameOver) {
		this.setActive(false);
		this.setVisible(false);
		this.setWord("");

		return;
	}

	this.startAttack();
}

FlyingSaucer.prototype.destroy = function() {
	this.isGameOver = true;

	if(this.timerEventExplode)
		game.time.events.remove(this.timerEventExplode);

	if(this.tweenScale == null) {
		this.setActive(false);
		this.setVisible(false);
		this.setWord("");
	}
}



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			= 2;	// dot per sec
FlyingSaucer.SPEED_UP_AMOUNT		= 4;	// dot per sec
FlyingSaucer.SPEED_DOWN_AMOUNT		= FlyingSaucer.SPEED_UP_AMOUNT / 2;	// dot per sec

FlyingSaucer.START_POS_Y			= 100;	// px
FlyingSaucer.CRASH_POS_Y			= GAME_SCREEN_SIZE.y - 200;	// px

FlyingSaucer.WORD_TEXT_OFFSET_Y		= 18;	// px

FlyingSaucer.TEXT_COLOR_NORMAL		= "#ffffff";
FlyingSaucer.TEXT_COLOR_CORRECT		= "#ffcc00";	// "#ffff4d";
FlyingSaucer.TEXT_STROKE_COLOR		= "#ffffff";

FlyingSaucer.JET_STATUS_HIGH		= 0;
FlyingSaucer.JET_STATUS_MIDDLE		= 1;
FlyingSaucer.JET_STATUS_LOW			= 2;

FlyingSaucer.JET_COLOR_HIGH			= 0x00ff00; //0x00ffff;
FlyingSaucer.JET_COLOR_MIDDLE		= 0xffff00;	//0xffff00;
FlyingSaucer.JET_COLOR_LOW			= 0xff0000;