var Game = {

	create: function() {
		this.mode = "release"; // "test";
		this.initVariables();

		this.dbConnectManager = new DBConnectManager();
		sessionStorageManager.setRecord(0);

		sessionStorageManager.setIsNewAppHighestRecord(false);

		var experienceAppTimer = new ExperienceAppTimer();
		experienceAppTimer.setTimeOverListener(
			(function() { this.timeOver(); }).bind(this)
		);

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

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

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


		// init game elements
		this.drawObjects();


		// bottom ui
		var screenBottomUI = new ScreenBottomUI();
		screenBottomUI.printLeftTextWithAppHighestRecord(sessionStorageManager.getAppHighestRecord());
		screenBottomUI.printCenterText(sessionStorageManager.getPlayingAppKoreanName());
		screenBottomUI.printRightText(sessionStorageManager.getPlayerName());
	},

	initVariables: function() {
		this.nextNumber = 1;

		this.difficulty = Chocoball.MODE_DIFFICULT;
		// this.difficulty = Chocoball.MODE_EASY;

		// if previous record is over 60 sec, play easy mode
		var prevRecord = sessionStorageManager.getRecord()
		// console.log(prevRecord);
		var secForEasyMode = this.mode == "test" ? 1 : 60;
		if(prevRecord > secForEasyMode) {
			this.difficulty = Chocoball.MODE_EASY;
		}

		this.firstChocoballArray = this.makeChocoballArray(1, 25);
		this.secondChocoballArray = this.makeChocoballArray(26, 50);

		this.timerEvent = null;
	},

	drawObjects: function() {
		var graphics = game.add.graphics(0, 0);
		var outLineRadius = 64;

		this.chocoballs = new Array();
		for(var i = 0; i < 25; i++) {
			graphics.lineStyle(0);
			graphics.beginFill(0x202020);
			graphics.drawCircle(Chocoball.getX(i), Chocoball.getY(i), outLineRadius);
			graphics.endFill();

			var chocoball = new Chocoball(
				i,
				this.difficulty,
				(function(chocoball) { this.onClickChocoball(chocoball); }).bind(this)
			);
			chocoball.setNumber(this.firstChocoballArray[i]);
			this.chocoballs.push(chocoball);
		}

		this.nextChocoballBubble = new NextChocoballBubble(God.POSITION_X, God.POSITION_Y - 260);
		this.nextChocoball = new Chocoball(
			100,
			this.difficulty,
			(function(chocoball) { console.log("no event") }).bind(this)
		);
		this.nextChocoball.setNumber(this.nextNumber);
		this.nextChocoballBubble.addChild(this.nextChocoball.body);

		this.god = new God(God.POSITION_X, God.POSITION_Y, this);
	},

	makeChocoballArray: function(firstNumber, lastNumber) {
		var numberArray = new Array();
		for(var i = firstNumber; i < lastNumber + 1; i++) {
			numberArray.push(i);
		}

		var randomArray = Phaser.ArrayUtils.shuffle(numberArray);
		return randomArray;
	},

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


	onClickChocoball: function(chocoball) {
		// console.log(chocoball.index);
		// console.log(chocoball.number);
		if(chocoball.number != this.nextNumber) {
			console.log("wrong");
			this.god.animateAngry();
			return;
		}

		if(chocoball.number == 50) {
			// console.log("game clear");
			this.cancelHintTimer();
			chocoball.setVisible(false);
			this.gameClear();
			return;
		}

		// clicked correct number
		if(chocoball.number == 1)
			this.stopWatch.start();

		this.nextNumber++;
		this.god.animateHappy();
		this.startHintTimer();

		if(chocoball.number <= 25)
			chocoball.setNumber(this.secondChocoballArray[chocoball.index]);
		else
			chocoball.setVisible(false);
		this.nextChocoball.setNumber(this.nextNumber);
		this.nextChocoballBubble.animateNextHint();

		if(this.mode == "test" && this.nextNumber == 3) {
			console.log("time over");
			this.timeOver();
		}
	},

	cancelHintTimer: function() {
		if(this.timerEvent) {
			game.time.events.remove(this.timerEvent);
			this.timerEvent = null;
		}
	},

	startHintTimer: function() {
		this.cancelHintTimer();
		this.timerEvent = game.time.events.loop(Game.SHOW_HINT_INTERVAL_MS, this.showHint, this);
	},

	showHint: function() {
		// console.log("show hint for " + this.nextNumber);
		for(var i = 0; i < 25; i++) {
			if(this.chocoballs[i].number == this.nextNumber) {
				// console.log("index : " + this.chocoballs[i].index);
				var chocoball = this.chocoballs[i].body;
				// console.log("x : " + chocoball.x + ", y : " + chocoball.y);
				this.showExplosionParticle(chocoball.x - 11, chocoball.y - 19);
				break;
			}
		}
	},

	showExplosionParticle: function(x, y) {
		var hitStarEmitter = game.add.emitter(x, y, 1);
		hitStarEmitter.makeParticles('star');
		hitStarEmitter.gravity = 0;
		hitStarEmitter.setAlpha(0.5, 0, Phaser.Timer.SECOND);
		hitStarEmitter.setScale(0.5, 1.5, 0.5, 1.5, Phaser.Timer.SECOND, Phaser.Easing.Quintic.Out);
		hitStarEmitter.setXSpeed(0, 0);
		hitStarEmitter.setYSpeed(0, 0);
		hitStarEmitter.start(true, 500, null, 10);
	},

	gameClear: function() {
		var recordTime = this.stopWatch.stop();
		sessionStorageManager.setRecord(recordTime);
		if(!isExperienceMaestroAccount())
			this.updateResultRecord();

		var missionClearText = new MissionClearText();
		game.time.events.add(Game.GAME_OVER_WAIT_TIME_MS, this.goResult, this);
	},

	timeOver: function() {
		var recordTime = this.stopWatch.stop();
		sessionStorageManager.setRecord(recordTime);
		if(!isExperienceMaestroAccount())
			this.updateResultRecord();

		var timeOverText = new TimeOverText();
		game.time.events.add(Game.GAME_OVER_WAIT_TIME_MS, this.goResult, this);
	},

	updateResultRecord: function() {
		if(sessionStorageManager.getMaestroAccountType() < 100) {	// experience account
			this.dbConnectManager.updateResultRecord(
				sessionStorageManager.getMaestroID(),
				sessionStorageManager.getPlayerID(),
				sessionStorageManager.getPlayingAppID(),
				sessionStorageManager.getRecord()
			);
		}
	},

	goResult: function() {
		location.href = '../../web/client/result.html?update=1905101';
	},

}


Game.GAME_OVER_WAIT_TIME_MS		= 3000;

Game.SHOW_HINT_INTERVAL_MS		= 3000;