// Chocoball.prototype = Object.create(Phaser.Sprite.prototype);
Chocoball.constructor = Chocoball;

function Chocoball(index, difficulty, onClickHandler) {
	this.index = index;
	this.difficulty = difficulty;
	this.onClickHandler = onClickHandler;

	this.initVariables();
	this.drawChocoball();

	this.setActive(true);
}

Chocoball.prototype.initVariables = function(textContent) {
	this.isActivated = false;
	this.number = 0;
}

Chocoball.prototype.drawChocoball = function() {
	var sizeRatio = 0.6;

	var x = Chocoball.getX(this.index);
	var y = Chocoball.getY(this.index);
	this.body = game.add.sprite(x, y, 'choco_body');
	this.body.scale.set(sizeRatio);
	this.body.anchor.set(0.5);
	this.body.inputEnabled = true;
	this.body.events.onInputDown.add(this.onClick, this);

	this.numberText = this.makeNumber("");
	this.body.addChild(this.numberText);

	this.gloss = game.add.sprite(0, 0, 'choco_gloss');
	this.gloss.anchor.set(0.5);
	this.body.addChild(this.gloss);
}

Chocoball.prototype.makeNumber = function(numberText) {
	var width = 0;
	var height = this.width / 30;

	var number = game.add.text(width, height, numberText, Chocoball.FONT_STYLE);
	number.anchor.set(0.5);
	number.lineSpacing = Chocoball.DEFAULT_TEXT_LINE_SPACING_PX;

	return number;
}

Chocoball.prototype.setNumber = function(number) {
	this.number = number;
	this.numberText.text = number;

	this.setBodyColor(this.number);
}

Chocoball.prototype.setBodyColor = function(number) {
	if(this.difficulty == Chocoball.MODE_DIFFICULT) {
		this.body.tint = Chocoball.COLOR_BLACK;
		return;
	}

	if(number < 11)
		this.body.tint = Chocoball.COLOR_1_TO_10;
	else if(number < 21)
		this.body.tint = Chocoball.COLOR_11_TO_20;
	else if(number < 31)
		this.body.tint = Chocoball.COLOR_21_TO_30;
	else if(number < 41)
		this.body.tint = Chocoball.COLOR_31_TO_40;
	else
		this.body.tint = Chocoball.COLOR_BLACK;

}


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

Chocoball.prototype.setVisible = function(visible) {
	this.body.visible = visible;
	this.gloss.visible = visible;
}

Chocoball.prototype.onClick = function() {
	if(typeof this.onClickHandler !== "undefined")
		this.onClickHandler(this);
}


Chocoball.getX = function(index) {
	if(index == 100)
		return 0;

	return 250 + (index % 5) * 70;
}

Chocoball.getY = function(index) {
	if(index == 100)
		return 0;

	return 250 + (Math.floor(index / 5)) * 70;
}

Chocoball.loadResources = function() {
	game.load.image('choco_gloss', '../../../resources/image/object/one_to_fifty/choco_gloss.png');
	game.load.image('choco_body', '../../../resources/image/object/one_to_fifty/choco_body.png');
}


// Chocoball.DEFAULT_SPEED		= 400;

Chocoball.IMAGE_SIZE		= 90;
Chocoball.IMAGE_CENTER		= Chocoball.IMAGE_SIZE / 2;

Chocoball.FONT_STYLE = {
	font: "50px Arial",
	boundsAlignH: "center",		// left, center. right
	boundsAlignV: "middle", 	// top, middle, bottom
	fill: "rgba(255, 255, 255, 0.9)",	// "#fff",
	fontWeight: "bold",
	stroke: "rgba(0, 0, 0, 0.3)", // "#333", // "#000",
	strokeThickness: 12,
	align: "center"
};

Chocoball.DEFAULT_TEXT_LINE_SPACING_PX = -10;	// in pixel
Chocoball.DEFAULT_STROKE_WIDTH_PX		= 5;	// in pixel

Chocoball.RADIUS			= 90;

Chocoball.COLOR_BLACK		= 0x553333;
Chocoball.COLOR_1_TO_10		= 0x3090FF;	// blue
Chocoball.COLOR_11_TO_20	= 0xD02000;	// red
Chocoball.COLOR_21_TO_30	= 0xFFFF00;	// yellow
Chocoball.COLOR_31_TO_40	= 0x00FF00;	// green

Chocoball.MODE_EASY			= 0;
Chocoball.MODE_DIFFICULT	= 1;