function Chart(licenseDataManager) {
	this.dbConnectManager = new DBConnectManager();
	this.licenseDataManager = licenseDataManager;

	this.chartGraphics = game.add.graphics(0, 0);
	this.printChartTitle();
	this.printChartBaseLine();

	this.recordTexts = [];
	for(var i = 0; i < Chart.PAGE_MAX_RECORD_COUNT; i++)
		this.recordTexts.push(new ChartRecordText(i));

	this.scoreList = null;
	this.loadScoreFromServer();
}


Chart.prototype.updateScoreList = function(jsonData) {
	// console.log(jsonData);

	var jsonScoreList = jsonData.scoreList;
	this.scoreList = [];
	for(var i = 0; i < jsonScoreList.length; i++) {
		this.scoreList.push(
			new ChartRecordData(
				jsonScoreList[i].SubjectName,
				jsonScoreList[i].Score,
				jsonScoreList[i].ScoreDateTime
			)
		);
	}
}

Chart.prototype.printChartTitle = function() {
	var offsetY = -25;
	var fontStyle = { font: "30px Arial", fill: "#fff", align: "center" };

	Chart.drawText(Chart.DATE_POS_X, Chart.CHART_LINE_Y + offsetY, "날짜", fontStyle);
	Chart.drawText(Chart.SCORE_POS_X, Chart.CHART_LINE_Y + offsetY, "점수", fontStyle);
	Chart.drawText(Chart.GRADE_POS_X, Chart.CHART_LINE_Y + offsetY, "등급", fontStyle);
	Chart.drawText(Chart.SUBJECT_POS_X, Chart.CHART_LINE_Y + offsetY, "과목", fontStyle);
}

Chart.prototype.printChartBaseLine = function() {
	this.chartGraphics.lineStyle(3, 0xffd900, 1);
	this.chartGraphics.moveTo(Chart.CHART_LINE_START_X, Chart.CHART_LINE_Y);
	this.chartGraphics.lineTo(game.world.width - Chart.CHART_LINE_START_X, Chart.CHART_LINE_Y);

	/*
	for(var i = 0; i < 6; i++) {
		this.chartGraphics.lineStyle(1, 0x888888, 1);
		this.chartGraphics.moveTo(
			Chart.CHART_LINE_START_X,
			ChartRecordText.getPosY(i) + ChartRecordText.RECORD_HEIGHT
		);
		this.chartGraphics.lineTo(
			game.world.width - Chart.CHART_LINE_START_X,
			ChartRecordText.getPosY(i) + ChartRecordText.RECORD_HEIGHT
		);
	}
	*/
}

Chart.drawText = function(posX, posY, text, fontStyle) {
	var textObject = game.add.text(
		posX, posY,
		text, fontStyle
	);
	textObject.anchor.set(0.5);
	textObject.stroke = "#333";
	textObject.strokeThickness = 3;

	return textObject;
}

Chart.prototype.printPage = function(pageNo) {
	var startScoreListIndex = 0;
	var endScoreListIndex = Chart.PAGE_MAX_RECORD_COUNT - 1;

	for(var i = 0; i < Chart.PAGE_MAX_RECORD_COUNT; i++) {
		var scoreListIndex = startScoreListIndex + i;
		if(startScoreListIndex + i < this.scoreList.length)
			this.printChartRecord(i, scoreListIndex);
		else
			this.printChartRecord(i, null);
	}
}

Chart.prototype.printChartRecord = function(chartRecordIndex, scoreListIndex) {
	// console.log(chartRecordIndex);
	// console.log(this.recordTexts.length);
	// console.log(this.scoreList);

	if(scoreListIndex == null) {
		this.recordTexts[chartRecordIndex].printDate("-");
		this.recordTexts[chartRecordIndex].printScore("-");
		this.recordTexts[chartRecordIndex].printGrade("-");
		this.recordTexts[chartRecordIndex].printSubject("-");
	} else {
		this.recordTexts[chartRecordIndex].printDate(DateUtil.printMonthDay(this.scoreList[scoreListIndex].date));

		var scoreValue = RecordUtil.getRecordValueWithUnit(this.scoreList[scoreListIndex].score, 101);
		this.recordTexts[chartRecordIndex].printScore(scoreValue);

		var gradeValue = "-";
		var subjectData = this.licenseDataManager.getSubjectDataByName(this.scoreList[scoreListIndex].subject);
		if(subjectData != null)
			gradeValue = subjectData.getGrade(this.scoreList[scoreListIndex].score);
		this.recordTexts[chartRecordIndex].printGrade(gradeValue);

		this.recordTexts[chartRecordIndex].printSubject(this.scoreList[scoreListIndex].subject);
	}
}

Chart.prototype.printContents = function() {
	if(this.scoreList == null || this.scoreList.length == 0) {
		for(var i = 0; i < this.recordTexts.length; i++) {
			this.recordTexts[i].printDate("-");
			this.recordTexts[i].printScore("-");
			this.recordTexts[i].printGrade("-");
			this.recordTexts[i].printSubject("-");
		}
		return;
	}


	if(isDebugMode()) {
		for(var i = 0; i < this.recordTexts.length; i++) {
			this.recordTexts[i].printDate("12월 10일");
			this.recordTexts[i].printScore(360);
			this.recordTexts[i].printGrade("B");
			this.recordTexts[i].printSubject("ITQ 파워포인트");
		}
	}
}


Chart.prototype.loadScoreFromServer = function() {
	this.dbConnectManager.requestLicenseScore(
		sessionStorageManager.getMaestroID(),
		sessionStorageManager.getPlayerID(),
		(function(replyJson) {
			// console.log(replyJson);

			this.updateScoreList(replyJson);
			this.printPage(0);
		}).bind(this),

		(function(replyJson) {
			console.log(replyJson);

			this.updateScoreList(null);
		})
	);
}



Chart.CHART_LINE_START_X	= 230;
Chart.CHART_LINE_Y			= 370;

Chart.CHART_GAP_PX			= 120;
Chart.CHART_HEIGHT			= 120;

Chart.CHART_GRAPH_OFFSET_X	= 50;
Chart.CHART_DATE_OFFSET_Y	= 20;
Chart.CHART_SCORE_OFFSET_Y	= 30;

Chart.DATE_POS_X			= Chart.CHART_LINE_START_X + 70;
Chart.SCORE_POS_X			= Chart.DATE_POS_X + 120;
Chart.GRADE_POS_X			= Chart.DATE_POS_X + 200;
Chart.SUBJECT_POS_X			= Chart.DATE_POS_X + 360;

Chart.PAGE_MAX_RECORD_COUNT	= 7;



function ChartRecordData(subject, score, date) {
	this.subject = subject;
	this.score = score;
	this.date = date;

	return this;
}



function ChartRecordText(indexRow) {
	this.date = new DateData(indexRow);
	this.score = new ScoreData(indexRow);
	this.grade = new GradeData(indexRow);
	this.subject = new SubjectData(indexRow);

	return this;
}


ChartRecordText.prototype.printDate = function(date) {
	this.date.text = date;
}

ChartRecordText.prototype.printScore = function(score) {
	this.score.text = score; //RecordUtil.getRecordValueWithUnit(score, 101); //sessionStorageManager.getPlayingAppID());
}

ChartRecordText.prototype.printGrade = function(grade) {
	this.grade.text = grade;
}

ChartRecordText.prototype.printSubject = function(subject) {
	this.subject.text = subject;
}

ChartRecordText.getPosX = function(columnName) {
	var posX = Chart.CHART_LINE_START_X;
	switch(columnName) {
		case "date":
		posX = Chart.DATE_POS_X;
		break;

		case "score":
		posX = Chart.SCORE_POS_X;
		break;

		case "grade":
		posX = Chart.GRADE_POS_X;
		break;

		case "subject":
		posX = Chart.SUBJECT_POS_X;
		break;
	}

	return posX;
}

ChartRecordText.getPosY = function(rowIndex) {
	var offsetY = 30;
	return Chart.CHART_LINE_Y + offsetY + ChartRecordText.GAP_RECORD_Y * rowIndex;
}

ChartRecordText.getDefaultFontStyle = function() {
	return { font: "26px Arial", fill: "#ffc", align: "center" };
}

ChartRecordText.GAP_RECORD_Y	= 40;
ChartRecordText.RECORD_HEIGHT	= 16;


function DateData(index) {
	this.dateText = Chart.drawText(
		ChartRecordText.getPosX("date"), ChartRecordText.getPosY(index),
		"-", ChartRecordText.getDefaultFontStyle()
	);

	return this.dateText;
}

function ScoreData(index) {
	this.scoreText = Chart.drawText(
		ChartRecordText.getPosX("score"), ChartRecordText.getPosY(index),
		"-", ChartRecordText.getDefaultFontStyle()
	);

	return this.scoreText;
}

function GradeData(index) {
	this.gradeText = Chart.drawText(
		ChartRecordText.getPosX("grade"), ChartRecordText.getPosY(index),
		"-", ChartRecordText.getDefaultFontStyle()
	);

	return this.gradeText;
}

function SubjectData(index) {
	this.subjectText = Chart.drawText(
		ChartRecordText.getPosX("subject"), ChartRecordText.getPosY(index),
		"-", ChartRecordText.getDefaultFontStyle()
	);

	return this.subjectText;
}