//startingDate:16-09-2019
var WordProb = {};
var Utils = {};
Utils.Path = '';
Utils.mobileDeviceFlag = false;
var questionId = 0, startTime = 0, scoredMarks = 0, totalMarks = 10, resId = 0, level = 1, label = '';
WordProb.questionCtr = 0;
WordProb.totalQue = 5;
WordProb.scoreFlag = false;
WordProb.questionArr = [];
WordProb.skipedQuestionArr = [];
WordProb.optionArr = [];
WordProb.ansStoredArr = [];
WordProb.skipCtr = 0;
randQuestion = 0;
WordProb.spliceFlag = false;
WordProb.correctAns = 0;
WordProb.replayFlag = false;
$(document).ready(function () {
	$(".se-pre-con").fadeOut("slow");
	if ((navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) && !(window.location.href.includes('http')))
		Utils.mobileDeviceFlag = true;
	/* if(Utils.mobileDeviceFlag) 
		Utils.Path=Android.getMediaPath("WordProb");  */
	setTimeout(function () {
		WordProb.init();
	}, 100);
});

WordProb.init = function () {
	document.title = Object.values(WordProb.AcknowledgementPageData)[0];
	$("#coverPage").show();
	$("#gamePage").hide();
	$("#endPage").hide();
	$(".header").not("#logo").css("visibility", "hidden");
	document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/" + WordProb.hardCodedData.cover_page;
	WordProb.initializeVariables();
	WordProb.setAllImages();
	WordProb.createStars();
	//WordProb.createAcknowledgementDivs();
	//WordProb.createOptionDivs();
}

WordProb.initializeVariables = function () {
	WordProb.seconds = 0;
	WordProb.correctCtr = 0;
	WordProb.ansArr = [];
	WordProb.corrArr = [];
	WordProb.wrongArr = [];
	WordProb.primeNumArr = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 71, 73, 79, 83, 89, 97];
	$('#optionDiv').empty();
}
WordProb.setAllImages = function () {
	//document.getElementById("homeImage").src = Utils.Path + "images/skin/Home.png";
	document.getElementById("logoImage").src = Utils.Path + "images/skin/logo.png";
	document.getElementById("playImg").src = Utils.Path + "images/skin/Play.png";
	document.getElementById("next").src = Utils.Path + "images/skin/Skip.png";
	document.getElementById("nextButton").src = Utils.Path + "images/skin/Next.png";
	// document.getElementById("creditImg").src = Utils.Path + "images/skin/credits.png";
	// document.getElementById("replayImg").src = Utils.Path + "images/skin/replay.png";
	document.getElementById("gameOver").innerHTML = WordProb.hardCodedData.gameOver;
	document.getElementById("questionMsg").innerHTML = WordProb.modalData.responseMsg;
	document.getElementById("Yes").innerHTML = WordProb.modalData.yes;
	document.getElementById("No").innerHTML = WordProb.modalData.no;
	//document.getElementById("fish1").src = Utils.Path + "images/Home_button.png";
}

WordProb.createStars = function () {
	var newImage, newDiv, i, numberOfStars = 5;
	for (i = 1; i <= Math.floor(numberOfStars); i++) {
		newDiv = document.createElement("div");
		newDiv.setAttribute("id", "starDiv" + i);
		newDiv.setAttribute("class", "nopadding stars col-xs-2 col-md-2 col-lg-2 col-sm-2");

		newImage = document.createElement("img");
		newImage.setAttribute("id", "starImg" + i);
		newImage.setAttribute("class", "img-responsive nopadding col-sm-12 col-lg-12 col-md-12 col-xs-12");
		newImage.setAttribute("src", Utils.Path + "images/skin/star1.png");
		$(newDiv).append(newImage);
		$("#starDiv").append(newDiv);
	}
	$("#starDiv").css("visibility", "hidden");
};

WordProb.playGame = function () {
	document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/BG_Underwater.png";
	$("#coverPage").hide();
	$("#gamePage").show();
	//$(".header").css("visibility", "visible");
	WordProb.setQuestion();

}

WordProb.setQuestion = function () {
	var rno = 0, num, tempArr = [], oddCtr = 0, evenCtr = 0, d;
	$(".fonts").css("pointer-events", "auto");
	WordProb.questionCtr++;
	d = new Date();
	startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
	if (WordProb.replayFlag) {
		randQuestion = WordProb.skipedQuestionArr[WordProb.skipCtr];
		WordProb.corrArr = WordProb.ansStoredArr[WordProb.skipCtr].slice(0);
		WordProb.shuffle(WordProb.optionArr[WordProb.skipCtr]);
		for (var i = 1; i <= 4; i++) {
			WordProb.createBubbleDivs(optionDiv, 1, i, WordProb.optionArr[WordProb.skipCtr][i - 1]);
			WordProb.animateBubblesTop("div" + i);
		}
	}
	else {
		randQuestion = Math.floor(Math.random() * WordProb.questions.length);
		while (WordProb.questionArr.includes(randQuestion))
			randQuestion = Math.floor(Math.random() * WordProb.questions.length);
		WordProb.questionArr.push(randQuestion);

		rno = Math.ceil(Math.random() * 3);
		switch (randQuestion) {
			//natural positive negative numbers.
			case 0: case 2: case 3: {
				for (i = 0; i < rno; i++) {
					num = Math.floor(Math.random() * 100 + 1);
					while (WordProb.corrArr.includes(num))
						num = Math.floor(Math.random() * 100 + 1);
					WordProb.corrArr.push(num);
					WordProb.ansArr.push(num);
				}
				for (i = 0; i < (4 - rno); i++) {
					wrongNum = Math.floor(Math.random() * 100 + 1);
					while (WordProb.wrongArr.includes(-wrongNum))
						wrongNum = Math.floor(Math.random() * 100 + 1);
					if (wrongNum > 0)
						wrongNum = -wrongNum;
					WordProb.wrongArr.push(wrongNum);
					WordProb.ansArr.push(wrongNum);
				}
				if (randQuestion == 3) {
					tempArr = WordProb.corrArr.slice(0);
					WordProb.corrArr = WordProb.wrongArr.slice(0);
					WordProb.wrongArr = tempArr.slice(0);

				}

			} break;
			//whole numbers
			case 1: {
				for (i = 0; i < rno; i++) {
					num = Math.floor(Math.random() * 100 + 1);
					while (WordProb.corrArr.includes(num))
						num = Math.floor(Math.random() * 100 + 1);
					WordProb.corrArr.push(num);
					WordProb.ansArr.push(num);
				}
				for (i = 0; i < (4 - rno); i++) {
					num1 = Math.floor(Math.random() * 10);
					num2 = Math.floor(Math.random() * 10 + 1);
					sign = Math.floor(Math.random() * 2);
					if (sign == 1)
						wrongNum = num1 + "." + num2;
					else
						wrongNum = num1 + "/" + num2;
					WordProb.wrongArr.push(wrongNum);
					WordProb.ansArr.push(wrongNum);

				}

			} break;
			//decimal numbers
			case 4: {
				for (i = 0; i < rno; i++) {
					num1 = Math.floor(Math.random() * 20);
					num2 = Math.floor(Math.random() * 100 + 1);
					num = eval(num1 + "." + num2);

					WordProb.corrArr.push(num);
					WordProb.ansArr.push(num);
				}
				for (i = 0; i < (4 - rno); i++) {
					wrongNum = Math.floor(Math.random() * 100);
					while (WordProb.wrongArr.includes(wrongNum))
						wrongNum = Math.floor(Math.random() * 100);
					sign = Math.floor(Math.random() * 2);
					if (sign)
						wrongNum = -wrongNum;
					WordProb.wrongArr.push(wrongNum);
					WordProb.ansArr.push(wrongNum);
				}
			} break;
			//even odd numbers.  	
			case 5: case 6: {
				for (i = 0; i < 4; i++) {
					num = Math.floor(Math.random() * 100);
					while (WordProb.ansArr.includes(num))
						num = Math.floor(Math.random() * 100);
					if (num % 2 == 0) {
						evenCtr++;
						if (evenCtr == 4) {
							evenCtr--;
							i--;
							continue;
						}
						else {
							WordProb.corrArr.push(num);
							WordProb.ansArr.push(num);
						}
					}
					else {
						oddCtr++;
						if (oddCtr == 4) {
							oddCtr--;
							i--;
							continue;
						}
						else {
							WordProb.wrongArr.push(num);
							WordProb.ansArr.push(num);
						}
					}
				}
				if (randQuestion == 6) {
					tempArr = WordProb.corrArr.slice(0);
					WordProb.corrArr = WordProb.wrongArr.slice(0);
					WordProb.wrongArr = tempArr.slice(0);
				}
			} break;
			//prime, composite numbers.
			// case 7: case 8: {
			// 	for (i = 0; i < 4; i++) {
			// 		if (i < rno) {
			// 			randomPrime = Math.floor(Math.random() * WordProb.primeNumArr.length);
			// 			num = WordProb.primeNumArr[randomPrime];
			// 			while (WordProb.corrArr.includes(num)) {
			// 				randomPrime = Math.floor(Math.random() * WordProb.primeNumArr.length);
			// 				num = WordProb.primeNumArr[randomPrime];
			// 			}
			// 			WordProb.corrArr.push(num);
			// 		}
			// 		else {
			// 			num = Math.floor(Math.random() * 100);
			// 			while (WordProb.wrongArr.includes(num) || WordProb.primeNumArr.includes(num))
			// 				num = Math.floor(Math.random() * 100);

			// 			WordProb.wrongArr.push(num);

			// 		}
			// 		WordProb.ansArr.push(num)
			// 	}
			// 	if (randQuestion == 8) {
			// 		tempArr = WordProb.corrArr.slice(0);
			// 		WordProb.corrArr = WordProb.wrongArr.slice(0);
			// 		WordProb.wrongArr = tempArr.slice(0);
			// 	}

			// } break;
			case 7: {
				for (i = 0; i < rno; i++) {

					num1 = Math.floor(Math.random() * 5 + 1);
					num2 = Math.floor(Math.random() * 20);
					while (num1 >= num2)
						num2 = Math.floor(Math.random() * 20);
					num = (num1 + "/" + num2);

					WordProb.corrArr.push(num);
					WordProb.ansArr.push(num);
				}
				for (i = 0; i < (4 - rno); i++) {
					wrongNum = Math.floor(Math.random() * 100);
					while (WordProb.wrongArr.includes(wrongNum))
						wrongNum = Math.floor(Math.random() * 100);
					sign = Math.floor(Math.random() * 2);
					if (sign)
						wrongNum = -wrongNum;
					WordProb.wrongArr.push(wrongNum);
					WordProb.ansArr.push(wrongNum);
				}
			} break;
			case 10: {
				num2Arr = [];
				for (i = 0; i < rno; i++) {
					num1 = 1;
					num2 = (Math.floor(Math.random() * 20 - 2) + 2);
					while (num2Arr.includes(num2))
						num2 = (Math.floor(Math.random() * 20 - 2) + 2);
					num = (num1 + "/" + num2);
					num2Arr.push(num2);
					WordProb.corrArr.push(num);
					WordProb.ansArr.push(num);
				}
				for (i = 0; i < (4 - rno); i++) {
					wrongNum1 = Math.floor(Math.random() * 20 + 2);
					wrongNum2 = Math.floor(Math.random() * 20 + 2);
					wrongNum = (wrongNum1 + "/" + wrongNum2);
					while (WordProb.wrongArr.includes(wrongNum)) {
						wrongNum1 = Math.floor(Math.random() * 20 + 2);
						wrongNum2 = Math.floor(Math.random() * 20 + 2);
						wrongNum = (wrongNum1 + "/" + wrongNum2);
					}
					WordProb.wrongArr.push(wrongNum);
					WordProb.ansArr.push(wrongNum);
				}
			} break;
			case 11: case 12: {
				num2Arr = [];
				corrFlag = false;
				wrongFlag = false;
				for (i = 0; i < 4; i++) {
					num1 = (Math.floor(Math.random() * 18) + 2);
					num2 = (Math.floor(Math.random() * 18) + 2);
					while (num2Arr.includes(num2) || num1 == num2)
						num2 = (Math.floor(Math.random() * 18) + 2);
					num = (num1 + "/" + num2);
					num2Arr.push(num2);
					if (num1 < num2) {
						corrFlag = true;
						WordProb.corrArr.push(num);
					}
					else {
						WordProb.wrongArr.push(num);
						wrongFlag = true;
					}
					WordProb.ansArr.push(num);
					if (i == 3) {
						if (corrFlag == false || wrongFlag == false || (WordProb.ansArr.length < 4)) {
							corrFlag = false;
							wrongFlag = false;
							num2Arr = [];
							WordProb.corrArr = [];
							WordProb.wrongArr = [];
							WordProb.ansArr = [];
							i = 0;
							continue;
						}
					}
				}
				if (randQuestion == 12) {
					tempArr = WordProb.corrArr.slice(0);
					WordProb.corrArr = WordProb.wrongArr.slice(0);
					WordProb.wrongArr = tempArr.slice(0);
				}
			} break;
		}
		WordProb.shuffle(WordProb.ansArr);
		for (var i = 1; i <= 4; i++) {
			WordProb.createBubbleDivs(optionDiv, 1, i, WordProb.ansArr[i - 1]);
			WordProb.animateBubblesTop("div" + i);
		}
	}
	document.getElementById("questionDiv").innerHTML = WordProb.questions[randQuestion].hindi + " " + WordProb.hardCodedData.questionText2;
	randomNo1 = Math.floor(Math.random() * WordProb.fishImgsLeft.length);
	document.getElementById('fish1').src = Utils.Path + "images/skin/" + WordProb.fishImgsLeft[randomNo1] + ".gif";
	randomNo2 = Math.floor(Math.random() * WordProb.fishImgsRight.length);
	document.getElementById('fish2').src = Utils.Path + "images/skin/" + WordProb.fishImgsRight[randomNo2] + ".gif";

	clearInterval(WordProb.noResponseTimer);
	clearTimeout(WordProb.timeOut);
	WordProb.startTimer();
}
WordProb.startTimer = function () {
	if (WordProb.noResponseTimer) {
		clearInterval(WordProb.noResponseTimer);
		WordProb.noResponseTimer = null;
		WordProb.seconds = 0;
	}
	WordProb.noResponseTimer = setInterval(function () {
		WordProb.seconds++;
		if (WordProb.seconds >= 7) {
			document.getElementById("playSound").src = Utils.Path + "sounds/instructions/please_ans_the_ques.mp3";
			document.getElementById("playSound").play();
			clearInterval(WordProb.noResponseTimer);
			if (WordProb.timeOut) {
				clearTimeout(WordProb.timeOut);
				WordProb.timeOut = null;
			}
			WordProb.timeOut = setTimeout(function () {
				WordProb.seconds = 0;
				WordProb.startTimer();
			}, 2000);
		}
	}, 1000);
}

WordProb.createBubbleDivs = function (parentDiv, numberOfDivs, idNo, data) {
	var bubblesDiv, letter;
	for (var i = 1; i <= numberOfDivs; i++) {
		bubblesDiv = document.createElement("div");
		bubblesDiv.setAttribute("id", "div" + idNo);
		bubblesDiv.setAttribute("class", "bubbleClass bubble");
		bubblesDiv.setAttribute("onclick", "WordProb.checkAnswer(this.id);");
		bubblesDiv.style.backgroundImage = Utils.Path + 'url(images/skin/bubble-_Blue.png)';
		parentDiv.appendChild(bubblesDiv);

		letter = document.createElement("p");
		letter.setAttribute("id", "letter" + idNo);
		letter.setAttribute("class", "fonts  text-center");
		letter.setAttribute("data", data);
		letter.setAttribute('data-toggle', 'modal');
		letter.setAttribute('data-target', '#myModal');
		letter.innerHTML = data;
		bubblesDiv.appendChild(letter);
	}
}
//move bubbles upward
WordProb.animateBubblesTop = function (div) {
	var speed = 0;
	speed = Math.floor((Math.random() * 6000) + 10000);
	$("#" + div).css({ 'top': $('#optionDiv').height() + 200 });
	$("#" + div).animate(
		{
			'top': '-=' + ($(document).height() + ($("#" + div).outerHeight()))
		}, speed, function () {
			$(this).stop();
			id = $(this).prop('id');
			WordProb.animateBubblesTop(id);
		});
}

WordProb.checkAnswer = function (divId) {
	var number;
	if (WordProb.replayFlag)
		WordProb.spliceFlag = true;
	scoredMarks = 0;
	clearInterval(WordProb.noResponseTimer);
	clearTimeout(WordProb.timeOut);
	//divId = (document.getElementById(divId).children[0]);
	WordProb.attemptFlag = true;
	if (randQuestion < 7)
		number = eval(document.getElementById(divId).children[0].innerHTML);
	else
		number = (document.getElementById(divId).children[0].innerHTML);
	if (WordProb.corrArr.includes(number)) {
		document.getElementById(divId).style.pointerEvents = "none";
		document.getElementById("playSound").src = Utils.Path + "sounds/instructions/thats_right.mp3";
		document.getElementById(divId).children[0].style.color = "green";
		$('#' + divId).stop();
		WordProb.correctCtr++;
		//document.getElementById("playSound").pause();
		WordProb.startTimer();
		if (WordProb.correctCtr == WordProb.corrArr.length) {
			$(".bubbleClass").css("pointer-events", "none");
			WordProb.correctAns++;
			document.getElementById("responseImg").src = Utils.Path + "images/skin/winEmoji.png";
			$("#correctWrongModal").show();
			scoredMarks = 10;
			if (!WordProb.scoreFlag) {
				WordProb.scoreFlag = true;
				console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
				if (Utils.mobileDeviceFlag)
					Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
			}
			WordProb.nextQuestionTimer = setTimeout(function () {
				WordProb.nextQuestion();
			}, 3000);
		}
		else {
			scoredMarks = 0;
		}
	}
	else {
		$(".bubbleClass").css("pointer-events", "none");
		document.getElementById("playSound").src = Utils.Path + "sounds/instructions/wrongSoundAksharkhadi.mp3";
		document.getElementById(divId).children[0].style.color = "red";
		var id = $('#' + divId).prop('id');
		//$('#' + id).addclass("shake")
		document.getElementById(id).classList.add('shake');
		scoredMarks = 0;
		if (!WordProb.scoreFlag) {
			WordProb.scoreFlag = true;
			console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
			if (Utils.mobileDeviceFlag)
				Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
		}
		WordProb.resetscreenTime = setTimeout(function () {
			document.getElementById("responseImg").src = Utils.Path + "images/skin/WrongAnsEmoji.png";
			$("#correctWrongModal").show();
			document.getElementById(divId).children[0].style.color = "rgb(221,19,123)";
			document.getElementById(id).classList.remove('shake');
		}, 1500);
		WordProb.nextQuestionTimer = setTimeout(function () {
			WordProb.nextQuestion();
		}, 3000);
	}
	document.getElementById("playSound").play();
}

WordProb.nextQuestion = function () {
	clearInterval(WordProb.noResponseTimer);
	clearTimeout(WordProb.timeOut);
	clearTimeout(WordProb.resetscreenTime);
	if ((!WordProb.scoreFlag) && WordProb.attemptFlag) {
		WordProb.scoreFlag = true;
		console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
		if (Utils.mobileDeviceFlag)
			Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
	}
	if (WordProb.spliceFlag && WordProb.replayFlag) {
		WordProb.spliceFlag = false;
		WordProb.spliceElements();
	}
	WordProb.scoreFlag = false;
	$(".bubbleClass").css("color", "rgb(221,19,123)");
	$(".bubbleClass").removeClass('shake');
	$("#correctWrongModal").hide();
	document.getElementById("playSound").pause();
	clearTimeout(WordProb.nextQuestionTimer);
	if (!(WordProb.attemptFlag)) {
		WordProb.skipCtr++;
	}
	if (WordProb.replayFlag) {
		if (WordProb.skipCtr < WordProb.skipedQuestionArr.length) {
			WordProb.attemptFlag = false;
			WordProb.initializeVariables();
			WordProb.setQuestion();
		}
		else {
			if (WordProb.skipedQuestionArr.length == 0) {
				WordProb.gameOver();
			}
			else {
				$("#responseBoxDiv").show();
				WordProb.skipCtr = 0;
			}
		}
	}
	else {
		if (!(WordProb.attemptFlag)) {
			WordProb.skipedQuestionArr.push(randQuestion);
			WordProb.optionArr.push(WordProb.ansArr);
			WordProb.ansStoredArr.push(WordProb.corrArr);
		}
		if (WordProb.questionCtr < 5) {
			WordProb.attemptFlag = false;
			WordProb.initializeVariables();
			WordProb.setQuestion();
		}
		else {
			WordProb.gameOver();
		}
	}
}
WordProb.showSkippedQuestions = function () {
	console.log(WordProb.ansStoredArr);
	WordProb.replayFlag = true;
	$("#responseBoxDiv").hide();
	WordProb.skipCtr = 0;
	WordProb.initializeVariables();
	WordProb.setQuestion();

}
WordProb.gameOver = function () {
	$("#responseBoxDiv").hide();
	$("#gamePage").hide();
	$("#endPage").show();
	WordProb.giveStars();
	WordProb.totalQue = 5;
	if (Utils.mobileDeviceFlag)
		Android.StudentCertification(WordProb.correctAns, WordProb.totalQue, "1");
}

WordProb.giveStars = function () {
	var fractionVar, i, numberOfStars = WordProb.correctAns;
	for (i = 1; i <= Math.floor(numberOfStars); i++)
		document.getElementById("starImg" + i).setAttribute("src", Utils.Path + "images/skin/star2.png");
	$("#starDiv").css("visibility", "visible");
	if (numberOfStars != 0) {
		document.getElementById("playSound").src = Utils.Path + "sounds/instructions/" + WordProb.sounds[(numberOfStars - 1)].sound;
		document.getElementById("playSound").play();
	}
};



WordProb.callHome = function () {
	if (!WordProb.scoreFlag) {
		WordProb.scoreFlag = true;
		console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
		if (Utils.mobileDeviceFlag)
			Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
	}
	location.reload();
}
WordProb.shuffle = function (array) {
	var i, j, temp;
	for (i = array.length - 1; i > 0; i--) {
		j = Math.floor(Math.random() * (i + 1));
		temp = array[i];
		array[i] = array[j];
		array[j] = temp;
	}
	return array;
}
WordProb.showNextGame = function () {
	if (Utils.mobileDeviceFlag)
		Android.GotoNextGame();
}
WordProb.spliceElements = function () {
	WordProb.skipedQuestionArr.splice(WordProb.skipCtr, 1);
	WordProb.optionArr.splice(WordProb.skipCtr, 1);
	WordProb.ansStoredArr.splice(WordProb.skipCtr, 1);
}