//startingDate:15-11-2019
var WordProb = {};
var Utils = {};
Utils.Path = '';
Utils.mobileDeviceFlag = false;
var questionId = 0, startTime = 0, scoredMarks = 0, totalMarks = 10, resId = 0, level = 1, label = '';
var given1, given2, toFind, splitNum1, splitNum2, splitNum3, splitNum4, unit, digit;
var blinkCtr = 0, remainder;
WordProb.questionCtr = 0;
$(document).ready(function () {
	$(".se-pre-con").fadeOut("slow");
	if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i))
		Utils.mobileDeviceFlag = true;
	/* if(Utils.mobileDeviceFlag) 
		Utils.Path=Android.getMediaPath("WordProb");  */
	setTimeout(function () {
		WordProb.init();
	}, 100);
});

WordProb.init = function () {
	$("#coverPage").show();
	$("#gamePage").hide();
	$("#levelPage").hide();
	$(".header").not("#logo").css("visibility", "hidden");
	document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/" + WordProb.hardcodedData.coverPage + ")";
	WordProb.initializeVariables();
	WordProb.createNumberDivs(numberSystemDiv);
	WordProb.setAllImages();
	WordProb.createAcknowledgementDivs();
	WordProb.createTableDivs();
	//Thin Pencil Handwriting.ttf

	// var newStyle = "<style>" +
	// 	"@font-face {  " +
	// 	"font-family: 'loopy';" +
	// 	"src: url('fonts/Snake.ttf') format('truetype');" +
	// 	"}  </style>";
	// $("head").append(newStyle);
	// $("#gamePage").css("font-family", "loopy");
}

WordProb.initializeVariables = function () {//initialization.
	WordProb.finalAnsFlag = false;//for checking final answer or not
	WordProb.counter = 1;//counter for steps
	WordProb.checkFlag = false;//for checking whether answer is correct or not.
	WordProb.attemptFlag = false;//for attempt.
	WordProb.remainderDivFlag = true;//for creating remainder subdivs.
	digitCtr = 0;//for counting the digits of divident.
	traverse = 0;
	WordProb.stepCtr = 1;//for counting division steps.
	WordProb.highlighter = 1;//for highlighting the divident numbers.
	WordProb.currNum = '';
	WordProb.lenArr = [];
	WordProb.editFieldArr = [];//array for divs to be highlighted.
	WordProb.helpFlag = true;
}
WordProb.setAllImages = function () {
	document.getElementById("homeImage").src = Utils.Path + "images/skin/Home.png";
	document.getElementById("next").src = Utils.Path + "images/skin/Next.png";
	document.getElementById("logoImage").src = Utils.Path + "images/skin/logo.png";
	document.getElementById("playImg").src = Utils.Path + "images/skin/playButton.png";
	document.getElementById("questionDiv").style.backgroundImage = "url(" + Utils.Path + "images/skin/textBlock.png)";
	document.getElementById("innerDiv").style.backgroundImage = "url(" + Utils.Path + "images/skin/notebook.png)";
	document.getElementById("creditImg").src = Utils.Path + "images/skin/Credits.png";
	document.getElementById("replayImg").src = Utils.Path + "images/skin/Replay.png";
	document.getElementById("helpBtn").src = Utils.Path + "images/skin/help.png";
	document.getElementById("gameOver").innerHTML = WordProb.hardcodedData.gameOver;
}

WordProb.playGame = function () {
	$(".header").css("visibility", "visible");
	WordProb.level = 4;
	WordProb.questionCtr = 0;
	$("#coverPage").hide();
	$("#levelPage").hide();
	$("#gamePage").show();
	document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/Bg.png)";
	$("#inputDiv1").droppable(
		{
			drop: WordProb.checkGiven1
		});
	$("#inputDiv5").droppable(
		{
			drop: WordProb.checkGiven2
		});
	$("#inputDiv2").droppable(
		{
			drop: WordProb.checkToFind
		});
	WordProb.makeQuestion();
}

WordProb.makeQuestion = function ()						//generate word Sentence
{
	WordProb.questionCtr++;
	var randomPerson, randomObject, personArr = [], numArr = [], d;
	WordProb.numberArray = [];
	blinkCtr = 0;
	//WordProb.editFieldArr = ["t1", "u1", "t2", "u2", "a2", "a1", "finalAns"];
	$(".editField").not("#finalAns").css("pointer-events", "none");
	WordProb.operator = WordProb.operatorArr[WordProb.level - 1];
	d = new Date();
	startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
	randomDivisor = Math.floor(Math.random() * 19 + 2);
	randomAns = Math.floor(Math.random() * 40 + 11);
	WordProb.divident = randomDivisor * randomAns;
	//WordProb.editFieldArr = ["t1", "u1", "t2", "u2", "a2", "c1", "a1", "finalAns"];
	WordProb.answer = randomAns;
	document.getElementById("finalAns").setAttribute("data", WordProb.answer);
	WordProb.divisor = randomDivisor;
	if (String(WordProb.divident).length - String(WordProb.answer).length == 1)
		WordProb.answer = "0" + WordProb.answer;
	else
		if (String(WordProb.divident).length - String(WordProb.answer).length == 2)
			WordProb.answer = "00" + WordProb.answer;
	console.log(WordProb.answer);

	$("#multCalcDiv").show();
	//document.getElementById("divisor").innerHTML = randomDivisor;
	//document.getElementById("divident").innerHTML = divident;


	$("#numberSystemDiv").show();
	document.getElementById("questionText").innerHTML = WordProb.divident + " " + WordProb.operator + " " + randomDivisor;
	splitNum1 = Math.floor(WordProb.number1 / 10) * 10;
	splitNum2 = WordProb.number1 % 10;
	splitNum3 = Math.floor(WordProb.number2 / 10) * 10;
	splitNum4 = WordProb.number2 % 10;
	//WordProb.createSubAnsDivs();
	//WordProb.gotoHelp();
	WordProb.showCalculation();
	WordProb.createSpans("divident", String(WordProb.divident).length, "dividentSpan", WordProb.divident, true, 1);
	//WordProb.createSpans("divisor", String(randomDivisor).length, "divisorSpan", randomDivisor, true, 1);
	WordProb.editFieldArr.push("divisor");
	document.getElementById("divisor").setAttribute("data", WordProb.divisor);
	WordProb.createSpans("divisonAnsDiv", String(WordProb.answer).length, "ansSpan", WordProb.answer, false, 1);
	$("#" + WordProb.editFieldArr[blinkCtr]).addClass("blink_me");
	WordProb.id = WordProb.editFieldArr[blinkCtr];

}

//onclick of continue.
WordProb.showCalculation = function () {
	$("#buttonValue11").css("pointer-events", "none");
	$("#buttonValue11").css("opacity", 0.5);
	$("#buttonValue12").css("opacity", 0.5);
	WordProb.counter++;
	document.getElementById("playSound").pause();
	$(".dragSpans").draggable("disable");
	$("#continueDiv").hide();
	$(".input").empty();
	$("#ansStep").show();
	$("#GivenDiv,#toFindDiv,#OperatorDiv,#continueDiv").hide();
	$("#GivenDiv,#toFindDiv,#OperatorDiv,#continueDiv").removeClass("animated bounceOutLeft");
	$("#AnswerDiv").show();
	$("#bottomDiv").show();
	document.getElementById("finalAnsText").innerHTML = WordProb.hardcodedData.finalAns;
}
//function for creating subdivs in multiplication and remainder divs.
WordProb.createSpans = function (outerDiv, noOfSpans, id, number, flag, i) {
	for (i; i <= noOfSpans; i++) {
		span = document.createElement("DIV")
		span.setAttribute("id", id + i);
		span.setAttribute("class", "spans editField singleLetter col-xs-2 col-md-2 col-lg-2 col-sm-2");
		span.setAttribute("data", String(number)[i - 1]);
		if (flag == true)
			WordProb.editFieldArr.push(id + i);
		document.getElementById(outerDiv).appendChild(span)
	}
}
WordProb.displayInTextBox = function (element) {//inputs the number in selected textbox when user clicks the number on numberpad.
	var number, flag = false;

	if ($("#" + WordProb.id).hasClass("singleLetter")) {
		number = 1;
	} else if (WordProb.id == "finalAns") {
		number = String(WordProb.answer).length;
	}
	else if (WordProb.id == "divisor") {
		number = number = String(WordProb.divisor).length;
	}
	if (WordProb.id == "finalAns") {
		$("#buttonValue11").css("pointer-events", "auto");
		$("#buttonValue11").css("opacity", 1);
		number = String(WordProb.answer).length;
	}
	if ($("#" + WordProb.id).text().length < number)
		$("#" + WordProb.id).append($(element).text());
	if ($("#" + WordProb.id).text().length == number) {
		if (!(flag))
			WordProb.checkAnswer();
	}
	else {
		if (parseInt($("#" + WordProb.id).text()) == WordProb.answer)
			WordProb.checkAnswer();
	}
}

WordProb.checkAnswer = function () {//checks the inputed answer is correct or not.
	$("#numberSystemDiv,#finalAns").css("pointer-events", "none");
	var data;
	WordProb.attemptFlag = true;
	WordProb.checkFlag = false;
	data = parseInt(document.getElementById(WordProb.id).getAttribute("data"));
	if (parseInt(document.getElementById(WordProb.id).innerHTML) == data) {
		WordProb.checkFlag = true;
	}
	else
		WordProb.checkFlag = false;

	if (WordProb.checkFlag) {//if input answer is correct. 
		document.getElementById(WordProb.id).style.backgroundColor = "green";
		document.getElementById(WordProb.id).style.color = "white";
		document.getElementById(WordProb.id).style.pointerEvents = "none";
		$("#" + WordProb.id).addClass("correctClass");
		$("#" + WordProb.id).css("pointer-events", "none");
		if (!(WordProb.directAnsFlag)) {
			WordProb.highlightTime = setTimeout(function () {
				$("#buttonValue11").css("pointer-events", "none");
				$("#buttonValue11").css("opacity", 0.5);
				$("#numberSystemDiv,#finalAns").css("pointer-events", "auto");
				$("#" + WordProb.id).removeClass("blink_me");
				if ($("#" + WordProb.id).parent()[0].id == "divident") {
				}
				blinkCtr++;
				multiplication = parseInt($("#" + WordProb.id).text());
				if (multiplication != 0 && $("#" + WordProb.id).parent()[0].id == "divisonAnsDiv") {
					WordProb.currMultiplier = multiplication;
					WordProb.createSpans("multiplication" + WordProb.stepCtr, String(multiplication * WordProb.divisor).length, "multSpan" + WordProb.stepCtr, multiplication * WordProb.divisor, true, 1)
					$("#solutionStep" + WordProb.stepCtr).css("visibility", "visible");

				}
				else if ($("#" + WordProb.id).parent()[0].id == ("multiplication" + WordProb.stepCtr)) {
					remainder = WordProb.currNum - WordProb.currMultiplier * WordProb.divisor;
					if ((String(parseInt(WordProb.currNum)).length - String(remainder).length == 1))
						remainder = "0" + remainder;
					else if ((String(parseInt(WordProb.currNum)).length - String(remainder).length == 2))
						remainder = "00" + remainder;
					if (WordProb.remainderDivFlag) {
						WordProb.createSpans("remainder" + WordProb.stepCtr, String(parseInt(WordProb.currNum)).length, "remSpan" + WordProb.stepCtr, remainder, true, 1)
						WordProb.remainderDivFlag = false;
					}
				}
				if (blinkCtr >= WordProb.editFieldArr.length) {
					if (WordProb.highlighter <= String(WordProb.divident).length) {
						WordProb.editFieldArr.push("ansSpan" + WordProb.highlighter);
						WordProb.highlightDivident();
					}
					else {
						WordProb.editFieldArr.push("finalAns");

					}
					WordProb.highlighter++;
				}
				WordProb.id = WordProb.editFieldArr[blinkCtr];
				$("#" + WordProb.id).addClass("blink_me");
				if (blinkCtr > 3) {
					digitCtr++;
					if (blinkCtr == 4) {
						$("#" + WordProb.traverseArr[traverse][0]).css("background-color", "pink");
						$("#" + WordProb.traverseArr[traverse][1]).css("background-color", "pink");
						digitCtr = 1;
						traverse++;
					}
					if (digitCtr > WordProb.lenArr[traverse - 1]) {
						digitCtr = 1;
						if (traverse > 0 && traverse < (WordProb.lenArr.length)) {
							$("#" + WordProb.traverseArr[traverse - 1][0]).css("background-color", "");
							$("#" + WordProb.traverseArr[traverse - 1][1]).css("background-color", "");
						}
						if (traverse < (WordProb.lenArr.length - 1)) {
							$("#" + WordProb.traverseArr[traverse][0]).css("background-color", "pink");
							$("#" + WordProb.traverseArr[traverse][1]).css("background-color", "pink");
						}
						console.log(traverse);
						traverse++;
					}
				}
				if (WordProb.id == "a2" && WordProb.level == 2) {
					$("#responseBoxDiv").show();
				}
				document.getElementById(WordProb.editFieldArr[blinkCtr - 1]).style.backgroundColor = "";
				document.getElementById(WordProb.editFieldArr[blinkCtr - 1]).style.color = "black";
			}, 500);
		}
	}
	else {
		document.getElementById(WordProb.id).style.backgroundColor = "red";
		if (!WordProb.directAnsFlag) {
			setTimeout(function () {
				$("#numberSystemDiv,#finalAns").css("pointer-events", "auto");
				document.getElementById(WordProb.id).style.backgroundColor = "";
				document.getElementById(WordProb.id).innerHTML = "";
			}, 1000);
		}
		else {
			setTimeout(function () {
				$("#numberSystemDiv,#finalAns").css("pointer-events", "auto");
				WordProb.id = WordProb.editFieldArr[blinkCtr];
				$("#" + WordProb.id).addClass("blink_me");
				$("#finalAns").removeClass("blink_me");
				document.getElementById("finalAns").innerHTML = '';
				document.getElementById("finalAns").style.backgroundColor = "";
				WordProb.directAnsFlag = false;
			}, 500);
		}

	}
	if (WordProb.id == "finalAns")
		WordProb.finalAnsFlag = true;
	if (WordProb.finalAnsFlag) {//if final answer is correct.
		if (parseInt(document.getElementById(WordProb.id).innerHTML) == WordProb.answer) {
			document.getElementById("playSound").src = Utils.Path + "sounds/instructions/thats_right.mp3";
			scoredMarks = 10;
			WordProb.correctWrongModalShow("winEmoji.png", 2000);
			setTimeout(function () {
				WordProb.nextQuestion();
			}, 2500);
		}
		else {
			scoredMarks = 0;
			WordProb.finalAnsFlag = false;
			document.getElementById("playSound").src = Utils.Path + "sounds/instructions/wrongSoundAksharkhadi.mp3";
			WordProb.correctWrongModalShow("rongAnsEmoji.png", 2000);
			setTimeout(function () {
				$("#subAnsDiv").show();
				if (WordProb.level == 3)
					$("#helpBtn").css("visibility", "visible");
			})
		}
		document.getElementById("playSound").play();
	}
}

WordProb.highlightDivident = function () {
	if ($("#" + WordProb.id).parent()[0].id == "remainder" + WordProb.stepCtr && WordProb.highlighter <= String(WordProb.divident).length + 1) {
		$(".highLighted").css("background-color", "");
		remainder = remainder + String(WordProb.divident)[WordProb.highlighter - 1];
		if (WordProb.highlighter <= String(WordProb.divident).length) {
			WordProb.createSpans("remainder" + WordProb.stepCtr, (WordProb.highlighter + 1), "remSpan" + WordProb.stepCtr, remainder, false, WordProb.highlighter);
			document.getElementById("remSpan" + WordProb.stepCtr + (WordProb.highlighter)).innerHTML = String(WordProb.divident)[WordProb.highlighter - 1];
			WordProb.currNum = remainder;
			WordProb.remainderDivFlag = true;
			setTimeout(function () {
				for (i = 1; i <= String(remainder).length; i++) {
					document.getElementById("remSpan" + WordProb.stepCtr + i).style.backgroundColor = "pink";

					//WordProb.currNum = WordProb.currNum + $("#dividentSpan" + WordProb.highlighter).text();
					if (i == String(remainder).length)
						WordProb.stepCtr++;
				}
				var temp = String(parseInt(WordProb.currNum)).length;
				//if ($("#remSpan" + (WordProb.stepCtr - 1) + 1).text() == "0")
				temp = WordProb.highlighter - temp + 1;
				// else
				// 	temp = temp + 1;
				$("#minusSign" + (WordProb.stepCtr)).prop("class", "nopadding  col-xs-1 col-md-1 col-lg-1 col-sm-1 col-xs-offset-" + temp + " col-md-offset-" + temp + " col-lg-offset-" + temp + " col-sm-offset-" + temp);
				$("#remainder" + (WordProb.stepCtr)).prop("class", "nopadding height col-xs-6 col-md-6 col-lg-6 col-sm-6 col-xs-offset-" + (temp + 1) + " col-md-offset-" + (temp + 1) + " col-lg-offset-" + (temp + 1) + " col-sm-offset-" + (temp + 1));
			}, 700);
		}
	}
	else {
		document.getElementById("dividentSpan" + WordProb.highlighter).style.backgroundColor = "pink";
		$("#dividentSpan" + WordProb.highlighter).addClass("highLighted");
		WordProb.currNum = WordProb.currNum + $("#dividentSpan" + WordProb.highlighter).text();
	}
}

WordProb.nextQuestion = function () {
	clearTimeout(WordProb.highlightTime);
	if (!WordProb.attemptFlag) {
		scoredMarks = -1;
	}
	console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
	if (Utils.mobileDeviceFlag)
		Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
	if (WordProb.questionCtr < 5) {
		WordProb.resetVariables();
		WordProb.initializeVariables();
		WordProb.makeQuestion();
	}
	else {
		$("#gamePage").hide();
		$("#endPage").show();
	}
}
WordProb.resetVariables = function () {
	$("#numberSystemDiv,#finalAns").css("pointer-events", "auto");
	WordProb.directAnsFlag = false;
	//$("#AnswerDiv").hide();
	$(".inputNum,#finalAns,.input").css("background-color", "white");
	$(".inputNum,#finalAns").css("color", "black");
	$(".editField").css("color", "black");
	$(".editField").text("");
	$(".editField").css("background-color", "");
	$(".editField").css("pointer-events", "auto");
	$(".editField").removeClass("correctClass");
	$(".editField").removeClass("blink_me");
	$(".oprSpans").css("background-color", "white");
	$("#bottomDiv").hide();
	$(".editField", ".signs").css("visibility", "visible");
	$("#subAnsDiv").empty();
	$(".height").empty();
	$(".steps").css("visibility", "hidden");
	$("#tableDiv").css("visibility", "hidden");
}

WordProb.callHome = function () {//reload.
	if (WordProb.attemptFlag) {
		console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
		if (Utils.mobileDeviceFlag)
			Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
	}
	location.reload();
}

WordProb.correctWrongModalShow = function (str, duration) {
	document.getElementById("responseImg").src = Utils.Path + "images/skin/" + str;
	document.getElementById("correctWrongModal").style.display = "block";
	setTimeout(function () {
		document.getElementById("correctWrongModal").style.display = "none";
	}, duration);
}

WordProb.createAcknowledgementDivs = function () {
	var i = 0, div;
	for (i = 0; i < Object.keys(WordProb.AcknowledgementPageData).length; i++) {
		div = document.createElement('div');
		div.className = "fontClass firstLetter col-xs-12 col-sm-12 col-md-12 col-lg-12";
		div.innerHTML = Object.keys(WordProb.AcknowledgementPageData)[i] + " : " + Object.values(WordProb.AcknowledgementPageData)[i];
		acknowledgementDiv.appendChild(div);
	}
	div = document.createElement('div');
	div.className = "fontClass firstLetter col-xs-12 col-sm-12 col-md-12 col-lg-12";
	div.innerHTML = WordProb.hardcodedData['createdOn'];
	div.style.marginBottom = "2vh";
	acknowledgementDiv.appendChild(div);
}

WordProb.showAcknowledgement = function () {
	$("#acknowledgementDiv").show();

}

WordProb.playAgain = function () {
	$("#acknowledgementDiv").hide();
	$("#endPage").hide();
	$("#gamePage").show();
	WordProb.questionCtr = 0;
	WordProb.nextQuestion();
}



WordProb.showTable = function () {
	if (WordProb.helpFlag) {
		WordProb.helpFlag = false;
		$("#tableDiv").css("visibility", "visible");
		for (i = 1; i <= 10; i++) {
			document.getElementById("tableDiv" + i).innerHTML = WordProb.divisor * i;
		}
	}
	else {
		WordProb.helpFlag = true;
		$("#tableDiv").css("visibility", "hidden");
	}

	// var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
	// arr2 = [2, 4, 6];
	// ctr = 0;
	// for (i = 0; i < arr.length; i++) {
	// 	if (arr2.includes(i)) {
	// 		arr.splice(i - ctr, 1);
	// 		ctr++;
	// 	}

	// }
	// console.log(arr);
}

WordProb.createTableDivs = function () {
	var div, i;
	var parendDiv = document.getElementById("tableDiv");
	for (i = 1; i <= 10; i++) {
		div = document.createElement("div");
		div.setAttribute("id", "tableDiv" + i);
		div.setAttribute("class", "nopadding tables col-xs-12 col-md-12 col-lg-12 col-sm-12");
		parendDiv.appendChild(div);
	}
}