var Ranking = {

	preload: function() {
		game.load.image('icon_fullscreen', '../../../resources/image/icon/fullscreen_white.png');
	},

	create: function() {
		self = this;

		this.dbConnectManager = new DBConnectManager();

		this.jsonData = null;
		this.mode = Ranking.MODE_HOUR;

		this.game.stage.backgroundColor = '#4d4d4d';

		// var phaser = game.add.image(game.world.centerX, game.world.centerY, 'phaser');
		// phaser.anchor.set(0.5);
		// phaser.alpha = 0.1;


		// keyboard shortcut
		this.keyboardShortcut = new KeyboardShortcut();
		this.keyboardShortcut.addCallback(
			Phaser.KeyCode.ESC,	// keyCode
			null,	// keyDownHandler
			(function() { this.back(); }).bind(this),	// keyUpHandler
			"ranking"	// callerClassName
		);

		// top ui
		var screenTopUI = new ScreenTopUI();
		screenTopUI.makeBackButton( (function() { this.back(); }).bind(this) );
		screenTopUI.makeFullScreenButton();


		var resultTextStyle = textStyleBasic;
		resultTextStyle.font = "bold 42px Arial";

		this.textTitle = game.add.text(
			GAME_SCREEN_SIZE.x / 2, 32,
			sessionStorageManager.getPlayingAppKoreanName() + " - 수업시간 순위",
			resultTextStyle
		).setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2);
		this.textTitle.anchor.set(0.5);
		this.textTitle.addColor("#ff6666", 0);


		// contents

		// rank
		var rankAreaPositionX = 100;
		var rankAreaPositionY = 90;
		var style = { font: "34px Arial", fill: "#fff", tabs: [ 60, 200, 160 ] };
		this.textRanking1to10 = game.add.text(rankAreaPositionX, rankAreaPositionY , '', style)
			.setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2);
		this.textRanking11to20 = game.add.text(rankAreaPositionX + 500, rankAreaPositionY, '', style)
			.setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2);


		var buttonPositionY = 640;
		var buttonHour = this.makeTextButton(game.world.centerX - 220, buttonPositionY, "수업시간 순위", this.showRankingHour);
		// buttonHour.inputEnabled = false;
		var buttonDay = this.makeTextButton(game.world.centerX, buttonPositionY, "오늘의 순위", this.showRankingDay);
		// buttonDay.inputEnabled = false;
		var buttonMonth = this.makeTextButton(game.world.centerX + 220, buttonPositionY, "이달의 순위", this.showRankingMonth);
		// buttonMonth.inputEnabled = false;


		// bottom ui
		this.screenBottomUI = new ScreenBottomUI();
		// ScreenBottomUI.printLeftText("게임 진행 정보");
		// var playingAppName = appInfoManager.getAppNameKorean(sessionStorageManager.getPlayingAppName());
		// ScreenBottomUI.printCenterText(playingAppName);
		// this.screenBottomUI.printCenterText("수업시간 순위");
		this.screenBottomUI.printRightText(sessionStorageManager.getPlayerName());

		this.getRecordToRankingServer();
		game.time.events.loop(Phaser.Timer.SECOND * 10, this.getRecordToRankingServer, this);
	},


	back: function() {
		location.href = '../../web/client/start.html';
	},

	makeTextButton: function(x, y, buttonText, eventListener) {
		var setting = new RoundRectButtonSetting(x, y, 200, 100);
		setting.fontStyle.fontWeight = "bold";
		switch(buttonText) {
			case "수업시간 순위":
			setting.strokeColors = {
				out: 0xddcccc,
				over: 0xccaaaa,
				down: 0xbb9999,
				disabled: 0x333333
			};
			setting.buttonColors = {
				out: 0xffeeee,
				over: 0xddcccc,
				down: 0xccaaaa,
				disabled: 0x333333
			};
			setting.textColors = {
				out: "#f00",
				over: "#f00",
				down: "#f00",
				disabled: "#999"
			};
			break;

			case "오늘의 순위":
			setting.strokeColors = {
				out: 0xccccdd,
				over: 0xaaaacc,
				down: 0x9999bb,
				disabled: 0x333333
			};
			setting.buttonColors = {
				out: 0xeeeeff,
				over: 0xccccdd,
				down: 0xaaaacc,
				disabled: 0x333333
			};
			setting.textColors = {
				out: "#00f",
				over: "#00f",
				down: "#00f",
				disabled: "#999"
			};
			break;

			case "이달의 순위":
			setting.strokeColors = {
				out: 0xccddcc,
				over: 0xaaccaa,
				down: 0x99bb99,
				disabled: 0x333333
			};
			setting.buttonColors = {
				out: 0xeeffee,
				over: 0xccddcc,
				down: 0xaaccaa,
				disabled: 0x333333
			};
			setting.textColors = {
				out: "#0f0",
				over: "#0f0",
				down: "#0f0",
				disabled: "#999"
			};
			break;

		}

		return new RoundRectButton(setting, RoundRectButton.NONE_ICON, buttonText, eventListener);
	},

	getRecordToRankingServer: function() {
		// console.log("getRecordToRankingServer : " + this.mode);
		var self = this;

		this.dbConnectManager.requestAppRanking(
			sessionStorageManager.getMaestroID(),
			sessionStorageManager.getPlayingAppID(),
			(function(jsonData) {
				self.showRanking(jsonData);
			}),
			(function(jsonData) {
				self.showRanking(null);
			})
		);
	},

	showRankingHour: function() {
		self.mode = Ranking.MODE_HOUR;
		self.textTitle.text = sessionStorageManager.getPlayingAppKoreanName() + " - 수업시간 순위";
		self.textTitle.addColor("#ff6666", 0);
		self.getRecordToRankingServer();
	},

	showRankingDay: function() {
		self.mode = Ranking.MODE_DAY;
		self.textTitle.text = sessionStorageManager.getPlayingAppKoreanName() + " - 오늘의 순위";
		self.textTitle.addColor("#9999ff", 0);
		self.getRecordToRankingServer();
	},

	showRankingMonth: function() {
		self.mode = Ranking.MODE_MONTH;
		self.textTitle.text = sessionStorageManager.getPlayingAppKoreanName() + " - 이달의 순위";
		self.textTitle.addColor("#99ff99", 0);
		self.getRecordToRankingServer();
	},

	showRanking: function(jsonRankingData) {
		// console.log(jsonRankingData);

		if(sessionStorageManager.getMaestroAccountType() == 101) {
			var recordArray = this.getDummyRankList();
			this.printRanking(recordArray);

			return;
		}

		if(jsonRankingData === null) {
			var rankEmpty = [ ];
			this.textRanking1to10.parseList(rankEmpty);
			this.textRanking11to20.parseList(rankEmpty);

			return;
		}

		var jsonRankingList = null;
		if(this.mode === Ranking.MODE_HOUR)
			jsonRankingList = jsonRankingData.rankingHour;
		else if(this.mode === Ranking.MODE_DAY)
			jsonRankingList = jsonRankingData.rankingDay;
		else if(this.mode === Ranking.MODE_MONTH)
			jsonRankingList = jsonRankingData.rankingMonth;

		if(jsonRankingList === null) {
			var rankEmpty = [ ];
			this.textRanking1to10.parseList(rankEmpty);
			this.textRanking11to20.parseList(rankEmpty);

			return;
		}

		var recordArray = this.getRankingArrayFromJSON(jsonRankingList);
		this.printRanking(recordArray);
	},


	printRanking: function(recordArray) {
		var recordTop10 = [];
		var recordTop20 = [];
		for(var i = 0; i < 10; i++) {
			if(i < recordArray.length)
				recordTop10.push(recordArray[i]);
			if(i + 10 < recordArray.length)
				recordTop20.push(recordArray[i + 10]);
		}
		this.textRanking1to10.parseList(recordTop10);
		this.textRanking11to20.parseList(recordTop20);
	},

	getRankingArrayFromJSON: function(jsonRankingList) {
		// if(isDebugMode())
		//	return this.getDummyRankList();

		var rankingListCount = jsonRankingList.length;

		var recordArray = [];
		for(var i = 0; i < rankingListCount; i++) {
			var bestRecordRow = [];
			// bestRecordRow[0] = jsonRankingList[i]['UserID'];
			bestRecordRow[0] = i + 1;
			bestRecordRow[1] = jsonRankingList[i]['Name'];
			bestRecordRow[2] = RecordUtil.getRecordValueWithUnit(
				jsonRankingList[i]['BestRecord'],
				sessionStorageManager.getPlayingAppID()
			);
			// console.log("$BestRecordRow : " + bestRecordRow[0] + ", "  + bestRecordRow[1] + ", "  + bestRecordRow[2]);

			recordArray.push(bestRecordRow);
		}

		return recordArray;
	},

	getDummyRankList: function() {
		var dummyRankList = [
			[ '1', 'test1', '400' ],
			[ '2', 'test2', '300' ],
			[ '3', 'test3', '200' ],
			[ '4', 'test4', '100' ],
			[ '5', 'test5', '90' ],
			[ '6', 'test6', '80' ],
			[ '7', 'test7', '70' ],
			[ '8', 'test8', '60' ],
			[ '9', 'test9', '50' ],
			[ '10', 'test10', '40' ],
			[ '11', 'test11', '30' ],
			[ '12', 'test12', '20' ],
			[ '13', 'test13', '10' ],
			[ '14', 'test14', '9' ],
			[ '15', 'test15', '8' ],
			[ '16', 'test16', '7' ],
			[ '17', 'test17', '6' ],
			[ '18', 'test18', '5' ],
			[ '19', 'test19', '4' ],
			[ '20', 'test20', '3' ],
			[ '21', 'test21', '2' ],
			[ '22', 'test22', '1' ],
		];
		return dummyRankList;
	}

}

Ranking.MODE_HOUR	= "mode_hour";
Ranking.MODE_DAY	= "mode_day";
Ranking.MODE_MONTH	= "mode_month";