Meat = function(mainGame) {
	this.mainGame = mainGame;
	this.cookingSide = Meat.MEAT_NONE;
	this.cookingTime = [];
	this.cookingGrade = [];
	this.cookingTime[Meat.MEAT_BACK] = 0;
	this.cookingTime[Meat.MEAT_FRONT] = 0;
	this.cookingGrade[Meat.MEAT_BACK] = Meat.DONENESS_RARE;
	this.cookingGrade[Meat.MEAT_FRONT] = Meat.DONENESS_RARE;


	this.isActivated = true;
	this.isStartCook = false;
	this.isOnHeatPlate = false;

	this.downPositionX;
	this.downPositionY;

	Phaser.Sprite.call(this, game, this.mainGame.meatPlate.x, this.mainGame.meatPlate.y, 'meat1');
	this.anchor.set(0.5);
	this.scale.set(0.5);
	this.smoothed = false;

	this.inputEnabled = true;
	this.input.enableDrag();
	this.events.onInputDown.add(this.onDown, this);
	this.events.onInputUp.add(this.onUp, this);
	// this.events.onDragStart.add(this.onDragStart, this);
	// this.events.onDragStop.add(this.onDragStop, this);

	this.whiteSmokeParticle = new Smoke(Meat.DONENESS_RARE, 0, 0);
	this.redSmokeParticle = new Smoke(Meat.DONENESS_WELLDONE, 0, 0);
	this.blackSmokeParticle = new Smoke(Meat.DONENESS_BURN_BLACK, 0, 0);

	game.add.existing(this);
}

Meat.prototype = Object.create(Phaser.Sprite.prototype);
Meat.prototype.constructor = Meat;
Meat.prototype.update = function() {
	var positionY = this.y;

	if(positionY <= Game.GOD_POSITION_Y)
		this.scale.set(0.4);
	else if(positionY > Game.GOD_POSITION_Y && positionY < Game.HEAT_PLATE_POSITION_Y) {
		var scaleRate = (positionY - Game.GOD_POSITION_Y) / (Game.HEAT_PLATE_POSITION_Y - Game.GOD_POSITION_Y);
		this.scale.set(0.4 + 0.3 * scaleRate);
	} else
		this.scale.set(0.4 + 0.3);


	if(this.isOnHeatPlate == true && game.input.mousePointer.isUp == true) {
		this.cooking();
	}
}


Meat.prototype.onDown = function(sprite, pointer) {
	// console.log("-------------------------");
	// console.log("onDown : " + pointer.x + ", " + pointer.y);

	this.downPositionX = Math.floor(sprite.x);
	this.downPositionY = Math.floor(sprite.y);

	this.stopCook();
}

Meat.prototype.onUp = function(sprite, pointer) {
	// console.log("-------------------------");
	// console.log("onUp : " + pointer.x + ", " + pointer.y);

	var upPositionX = Math.floor(sprite.x);
	var upPositionY = Math.floor(sprite.y);

	// console.log("downPositionX : + " + this.downPositionX + ", downPositY : " + this.downPositionY);
	// console.log("upPositionX : + " + upPositionX + ", upPositionY : " + upPositionY);
	if(this.isInClickArea(this.downPositionX, this.downPositionY, upPositionX, upPositionY) == true)
		this.onClickListener();
	else {
		this.onDragStopListener();
	}
}


/*
Meat.prototype.onDragStart = function(sprite, pointer) {
	// console.log("onDragStart : " + pointer.x + ", " + pointer.y);
	console.log("onDragStart");

	this.stopCook();
}

Meat.prototype.onDragStop = function(sprite, pointer) {
	// console.log("onDragStop : " + pointer.x + ", " + pointer.y);
	console.log("onDragStop");

	if(isOverHeatPlate(this.x, this.y, this.width, this.height) == true) {
		console.log("isOverHeatPlate : " + isOverHeatPlate(this.x, this.y, this.width, this.height));
		this.startCook();
	} else {
		this.stopCook();
	}

	if(isOverGod(this.x, this.y) == true) {
		console.log("isOverGod : " + isOverGod(this.x, this.y));
		godEatMeat(this);
	}

}
*/


Meat.prototype.isInClickArea = function(downX, downY, upX, upY) {
	if(downX - 5 < upX && upX < downX + 5 && downY - 5 < upY && upY < downY + 5)
		return true;

	return false;
}

Meat.prototype.onClickListener = function(pointer) {
	// console.log("onClickListener");

	if(this.mainGame.isOverHeatPlate(this.x, this.y, this.width, this.height) == true) {
		if(this.isFront() == true)
			this.flipToBack()
		else
			this.flipToFront();
		// console.log("flip cooking side : " + this.cookingSide);

		this.startCook();
	}
}

Meat.prototype.onDragStopListener = function(pointer) {
	// console.log("onDragStop");

	if(this.mainGame.isOverHeatPlate(this.x, this.y, this.width, this.height) == true) {
		// console.log("isOverHeatPlate : " + isOverHeatPlate(this.x, this.y, this.width, this.height));
		this.startCook();
	} else {
		this.stopCook();
	}

	if(this.mainGame.isOverGod(this.x, this.y) == true) {
		// console.log("isOverGod : " + isOverGod(this.x, this.y));
		this.mainGame.godEatMeat(this);
	}
}


Meat.prototype.setScale = function(size) {
	this.scale.set(size);
	this.backupScale = size;
}



Meat.prototype.isFront = function() {
	if(this.cookingSide == Meat.MEAT_BACK)
		return true;

	return false;
}

Meat.prototype.flipToFront = function() {
	this.cookingSide = Meat.MEAT_BACK;
	// console.log("flipToFront : " + this.cookingSide);

	this.angle = Meat.ANGLE_FRONT;
	// console.log("flipToFront");
	// console.log("back : " + this.getMeatGradeBySide(Meat.MEAT_BACK) + ", front : " + this.getMeatGradeBySide(Meat.MEAT_FRONT));
	if(this.getMeatGradeBySide(Meat.MEAT_FRONT) == Meat.DONENESS_BURN_BLACK) {
		this.loadTexture('meatBurn1');
	} else if(this.getMeatGradeBySide(Meat.MEAT_FRONT) == Meat.DONENESS_WELLDONE) {
		this.loadTexture('meatWellDone1');
	} else {
		this.loadTexture('meat1');
	}
}

Meat.prototype.flipToBack = function() {
	this.cookingSide = Meat.MEAT_FRONT;
	// console.log("flipToBack : " + this.cookingSide);

	this.angle = Meat.ANGLE_BACK;
	// console.log("flipToBack");
	// console.log("back : " + this.getMeatGradeBySide(Meat.MEAT_BACK) + ", front : " + this.getMeatGradeBySide(Meat.MEAT_FRONT));
	if(this.getMeatGradeBySide(Meat.MEAT_BACK) == Meat.DONENESS_BURN_BLACK) {
		this.loadTexture('meatBurn1');
	} else if(this.getMeatGradeBySide(Meat.MEAT_BACK) == Meat.DONENESS_WELLDONE) {
		this.loadTexture('meatWellDone1');
	} else {
		this.loadTexture('meat1');
	}
}

Meat.prototype.show = function() {
	this.isActivated = true;
	this.alpha = 1;
}

Meat.prototype.hide = function() {
	this.isActivated = false;
	this.alpha = 0;
	this.stopCook();
}

Meat.prototype.startCook = function() {
	this.isStartCook = true;
	this.isOnHeatPlate = true;
	// console.log("this.isOnHeatPlate : " + this.isOnHeatPlate);

	// console.log("startCook : " + this.cookingSide);
	if(this.cookingSide == Meat.MEAT_NONE)
		this.flipToFront();

	this.startAnimateSmoke();
}

Meat.prototype.stopCook = function() {
	this.isOnHeatPlate = false;
	// console.log("this.isOnHeatPlate : " + this.isOnHeatPlate);
	this.stopAnimateSmoke();
}

Meat.prototype.getMeatGrade = function() {
	if(this.cookingGrade[Meat.MEAT_FRONT] == Meat.DONENESS_WELLDONE && this.cookingGrade[Meat.MEAT_BACK] == Meat.DONENESS_WELLDONE)
		return Meat.DONENESS_WELLDONE;
	else if(this.cookingGrade[Meat.MEAT_FRONT] == Meat.DONENESS_BURN_BLACK || this.cookingGrade[Meat.MEAT_BACK] == Meat.DONENESS_BURN_BLACK)
		return Meat.DONENESS_BURN_BLACK;
	else
		return Meat.DONENESS_RARE;
}

Meat.prototype.getMeatGradeBySide = function(cookingSide) {
	return this.cookingGrade[cookingSide];
}

Meat.prototype.setActive = function(isActivated, x, y, meat_type) {
	this.isActivated = isActivated;

	if(isActivated == false) {
		this.alpha = 0;
		this.x = -100;
		this.y = -100;

		return;
	}

	this.cookingSide = Meat.MEAT_NONE;
	this.cookingTime[Meat.MEAT_BACK] = 0;
	this.cookingTime[Meat.MEAT_FRONT] = 0;
	this.cookingGrade[Meat.MEAT_BACK] = Meat.DONENESS_RARE;
	this.cookingGrade[Meat.MEAT_FRONT] = Meat.DONENESS_RARE;

	this.x = x;
	this.y = y;
	this.loadTexture('meat1');
	this.angle = Meat.ANGLE_NONE;
	this.alpha = 1;
}

Meat.prototype.cooking = function() {
	this.cookingTime[this.cookingSide] += 1;
	// console.log("back : " + this.cookingTime[Meat.MEAT_BACK] + ", front : " + this.cookingTime[Meat.MEAT_FRONT]);

	if(this.cookingTime[Meat.MEAT_BACK] > Meat.COOK_TIME_BURN) {
		if(this.getMeatGradeBySide(Meat.MEAT_BACK) != Meat.DONENESS_BURN_BLACK) {
			this.cookingGrade[Meat.MEAT_BACK] = Meat.DONENESS_BURN_BLACK;
			this.changeAnimateSmokeColor(Meat.DONENESS_BURN_BLACK);
		}
	} else if(this.cookingTime[Meat.MEAT_BACK] > Meat.COOK_TIME_WELLDONE) {
		if(this.getMeatGradeBySide(Meat.MEAT_BACK) != Meat.DONENESS_WELLDONE) {
			this.cookingGrade[Meat.MEAT_BACK] = Meat.DONENESS_WELLDONE;
			this.changeAnimateSmokeColor(Meat.DONENESS_WELLDONE);
		}
	}

	if(this.cookingTime[Meat.MEAT_FRONT] > Meat.COOK_TIME_BURN) {
		if(this.getMeatGradeBySide(Meat.MEAT_FRONT) != Meat.DONENESS_BURN_BLACK) {
			this.cookingGrade[Meat.MEAT_FRONT] = Meat.DONENESS_BURN_BLACK;
			this.changeAnimateSmokeColor(Meat.DONENESS_BURN_BLACK);
		}
	} else if(this.cookingTime[Meat.MEAT_FRONT] > Meat.COOK_TIME_WELLDONE) {
		if(this.getMeatGradeBySide(Meat.MEAT_FRONT) != Meat.DONENESS_WELLDONE) {
			this.cookingGrade[Meat.MEAT_FRONT] = Meat.DONENESS_WELLDONE;
			this.changeAnimateSmokeColor(Meat.DONENESS_WELLDONE);
		}
	}
}

Meat.prototype.stopAnimateSmoke = function() {
	this.whiteSmokeParticle.stop();
	this.redSmokeParticle.stop();
	this.blackSmokeParticle.stop();
}

Meat.prototype.startAnimateSmoke = function() {
	var cookingSideGrade = this.cookingGrade[this.cookingSide];
	this.changeAnimateSmokeColor(cookingSideGrade);

}

Meat.prototype.changeAnimateSmokeColor = function(type) {
	// console.log("changeAnimateSmokeColor : " + type);
	this.stopAnimateSmoke();

	switch(type) {
		case Meat.DONENESS_RARE:
		this.whiteSmokeParticle.start(this.x, this.y);
		break;

		case Meat.DONENESS_WELLDONE:
		this.redSmokeParticle.start(this.x, this.y);
		break;

		case Meat.DONENESS_BURN_BLACK:
		this.blackSmokeParticle.start(this.x, this.y);
		break;
	}
}


Meat.MEAT_NONE	= 0;
Meat.MEAT_FRONT	= 1;
Meat.MEAT_BACK	= 2;

Meat.DONENESS_RARE			= 0;
Meat.DONENESS_WELLDONE		= 1;
Meat.DONENESS_BURN_BLACK	= 2;
Meat.DONENESS_NONE			= 3;

Meat.ANGLE_NONE		= 0;
Meat.ANGLE_FRONT	= 30;
Meat.ANGLE_BACK		= -30;

Meat.COOK_TIME_WELLDONE	= 200;
Meat.COOK_TIME_BURN		= 400;