let dbConnectManager = new DBConnectManager();
dbConnectManager.setMaestroID(0);
dbConnectManager.setPlayerID(0);
dbConnectManager.setDateTime("2018-05-16", "14:50:00");

QUnit.test( "User login", function( assert ) {
	let returnPlayerID = -1;

	let done = assert.async();
	setTimeout(function() {
		dbConnectManager.requestCheckUserLogin(
			"jisangs", "test", "111111",
			(jsonData) => {
				returnPlayerID = jsonData["UserID"];
				assert.equal(returnPlayerID, 31);
				done();
			},
			(jsonData) => {
				console.log(jsonData);
				done();
			}
		);
	});
});

QUnit.test( "Player history", function( assert ) {
	sessionStorageManager.maestroID = 1;	// jisangs
	sessionStorageManager.playerUserID = "8";	// 부현율
	sessionStorageManager.playingAppID = 30;
	let date = "2018-05-16";

	let done = assert.async();
	setTimeout(function() {
		dbConnectManager.requestPlayerHistory(
			1,	// maestroID for jisangs
			date,
			(historyRecordManager) => {
				// console.log(historyRecordManager);

				assert.equal(historyRecordManager.getAppNameAt(0), "korean_word");
				assert.equal(historyRecordManager.getDateAt(0), "2018-05-16");
				assert.equal(historyRecordManager.getBestRecordAt(0), 108.248);

				assert.equal(historyRecordManager.getAppNameAt(4), "korean_word");
				assert.equal(historyRecordManager.getDateAt(4), "2018-04-23");
				assert.equal(historyRecordManager.getBestRecordAt(4), 74.2234);

				done();
			}
		);
	});
});

QUnit.test( "Player ranking", function( assert ) {
	sessionStorageManager.maestroID = 1;	// jisangs
	sessionStorageManager.playingAppID = 30;
	let date = "2018-05-16";
	let time = "14:50:00";

	let done = assert.async(3);

	setTimeout(function() {
		dbConnectManager.requestRanking(
			DBConnectManager.TYPE_MY_RANKING_HOUR,
			1,	// maestroID for jisangs
			date,
			time,
			(type, rankingRecordManager) => {
				assert.equal(rankingRecordManager.getRankAt(0), 1, "hour");
				assert.equal(rankingRecordManager.getBestRecordAt(0), 302.921, "hour");
				assert.equal(rankingRecordManager.getUserIDAt(0), 5, "hour");
				assert.equal(rankingRecordManager.getUserNameAt(0), "김지유", "hour");

				assert.equal(rankingRecordManager.getRankAt(5), 6, "hour");
				assert.equal(rankingRecordManager.getBestRecordAt(5), 128.436, "hour");
				assert.equal(rankingRecordManager.getUserIDAt(5), 22, "hour");
				assert.equal(rankingRecordManager.getUserNameAt(5), "모윤건", "hour");

				done();
			}
		);
	});

	setTimeout(function() {
		dbConnectManager.requestRanking(
			DBConnectManager.TYPE_MY_RANKING_DAY,
			1,	// maestroID for jisangs
			date,
			time,
			(type, rankingRecordManager) => {
				assert.equal(rankingRecordManager.getRankAt(0), 1, "day");
				assert.equal(rankingRecordManager.getBestRecordAt(0), 313.304, "day");
				assert.equal(rankingRecordManager.getUserIDAt(0), 19, "day");
				assert.equal(rankingRecordManager.getUserNameAt(0), "오지운", "day");

				assert.equal(rankingRecordManager.getRankAt(17), 18, "day");
				assert.equal(rankingRecordManager.getBestRecordAt(17), 65.337, "day");
				assert.equal(rankingRecordManager.getUserIDAt(17), 20, "day");
				assert.equal(rankingRecordManager.getUserNameAt(17), "김민성", "day");

				done();
			}
		);
	});

	setTimeout(function() {
		dbConnectManager.requestRanking(
			DBConnectManager.TYPE_MY_RANKING_MONTH,
			1,	// maestroID for jisangs
			date,
			time,
			(type, rankingRecordManager) => {
				assert.equal(rankingRecordManager.getRankAt(0), 1, "month");
				assert.equal(rankingRecordManager.getBestRecordAt(0), 617.231, "month");
				assert.equal(rankingRecordManager.getUserIDAt(0), 50, "month");
				assert.equal(rankingRecordManager.getUserNameAt(0), "강민준", "month");

				assert.equal(rankingRecordManager.getRankAt(17), 18, "month");
				assert.equal(rankingRecordManager.getBestRecordAt(17), 216.279, "month");
				assert.equal(rankingRecordManager.getUserIDAt(17), 9, "month");
				assert.equal(rankingRecordManager.getUserNameAt(17), "신예성", "month");

				done();
			}
		);
	});

});

QUnit.test( "HowToPlay", function( assert ) {
	let howToPlayForSpaceInvaders = "외계인이 나타났다!\\n마우스 왼쪽 버튼으로\\n외계인을 클릭해서 물리쳐 주세요.";

	let done = assert.async(2);

	setTimeout(function() {
		dbConnectManager.requestHowToPlay(
			101,	// space_invaders app ID
			(jsonData) => {
				let howToPlay = jsonData["HowToPlay"];
				assert.equal(howToPlay, howToPlayForSpaceInvaders);
				done();
			},
			(jsonData) => {
				console.log(jsonData);
				done();
			}
		);

		dbConnectManager.requestHowToPlay(
			1,	// korean basic typing
			(jsonData) => {
				let howToPlay = jsonData["HowToPlay"];
				assert.equal(howToPlay, "");
				done();
			},
			(jsonData) => {
				console.log(jsonData);
				done();
			}
		);
	});
});


QUnit.test( "UpdateRecord", function( assert ) {
	let todayBestRecord = 0;

	let done = assert.async();

	setTimeout(function() {
		dbConnectManager.requestTodayBestRecord(
			1,	// maestro ID for "jisangs"
			1,	// user ID for jisangs
			101,	// space_invaders app ID
			(jsonData) => {
				todayBestRecord = jsonData["BestRecord"];
			},
			(jsonData) => {
				console.log(jsonData);
				todayBestRecord = 0;
			}
		);
	});

	setTimeout(function() {
		dbConnectManager.updateTodayBestRecord(
			1,	// maestro ID for "jisangs"
			1,	// user ID for jisangs
			101,	// space_invaders app ID
			todayBestRecord + 1000	// new record
		);
	}, 100);

	setTimeout(function() {
		dbConnectManager.requestTodayBestRecord(
			1,	// maestro ID for "jisangs"
			1,	// user ID for jisangs
			101,	// space_invaders app ID
			(jsonData) => {
				let bestRecord = jsonData["BestRecord"];
				assert.equal(bestRecord, todayBestRecord + 1000);
				done();
			},
			(jsonData) => {
				console.log(jsonData);
				done();
			}
		);
	}, 200);
});
