//september 2019
var VerbLed = {};
var Utils = {};
Utils.path = "";
var questionId = 0, startTime = 0, scoredMarks = 0, totalMarks = 9, resId = 0, label, scoreFlag = false;
var word="",totalNos=0,type="sentence";
 Utils.mobileDeviceFlag = false;
 var index=0;
 VerbLed.queArr=[];
$(document).ready(function () {
	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('makeRhymeWord');
	} */
	$(".se-pre-con").fadeOut("slow");
	setTimeout(function () {
		VerbLed.setImageSource();
		VerbLed.Init();
	}, 100);
});

VerbLed.setImageSource = function () {
	document.body.style.backgroundImage = Utils.path + "url('images/coverPage.png')";
	document.getElementById("verbDiv").style.background=Utils.path+"url('images/titleblock.png')";
	document.getElementById("verbDiv").style.backgroundSize="100% 100%"; 
	document.getElementById("sound").src = Utils.path + "images/sound.png";
	document.getElementById("logo").src = Utils.path + "images/logo.png";
	document.getElementById("playbutton").src = Utils.path + "images/Play.png";
	document.getElementById("skip").src = Utils.path + "images/skip.png";
//	document.getElementById("replatbtn").src=Utils.path+"images/replay.png";
	document.getElementById("creditbtn").src=Utils.path+"images/Next.png";
	if(VerbLed.hardcodedData.languageFont!=null)
	setFontFamilyForLang(VerbLed.hardcodedData.languageFont);
}
setFontFamilyForLang = function (fontFilePath) {
var newStyle = "<style>" +
	"@font-face { " +
	"font-family: 'CustomFontFamily';" +
	"src: url('" + fontFilePath + "') format('truetype');" +
	"} </style>";
$("head").append(newStyle);
$("body").css("font-family", "CustomFontFamily");
}

VerbLed.Init = function () {
	document.getElementById("home").style.display = "none";
	document.getElementById("gamePage").style.display = "none";
	document.getElementById("finalPage").style.display="none";
}
//Initialize variables
VerbLed.initVariables = function ()
{
	index=0;
	VerbLed.queCnt=0;
	VerbLed.optionsArray=[];
	VerbLed.ans;
	VerbLed.ansData;
	VerbLed.flag=false;
	VerbLed.queCnt=0;
	VerbLed.highlightedCell;
	VerbLed.tenseSequence;
	VerbLed.total_No_of_Questions=2;
	VerbLed.correctCounter=0;
	VerbLed.tensArray= ["Present","Past","Future"];
	VerbLed.ansClicked=false;
	VerbLed.clickedCnt=0;
	VerbLed.seconds=0;
}
VerbLed.playGame = function () {
	document.body.style.backgroundImage = Utils.path + "url('images/bg.png')";
	document.getElementById("optionsDiv").style.background=Utils.path+"url('images/waterTenk.png')";
	document.getElementById("optionsDiv").style.backgroundSize="100% 100%"; 
	document.getElementById("gamePage").style.display = "block";
	document.getElementById("coverPage").style.display = "none";
	document.getElementById("home").style.display = "block";
	document.getElementById("finalPage").style.display="none";
	document.getElementById("tenseChart").innerHTML="";	
	document.getElementById("VerbLed").innerHTML="";
	document.getElementById("VerbType").innerHTML="";
	//getDuration(Utils.path + 'sounds/Instructions/' +VerbLed.hardcodedData.instruction);
	VerbLed.playInstruction();
	VerbLed.populateQue();
	VerbLed.initVariables();
	VerbLed.tenseDiv();
	VerbLed.showQue();
}
VerbLed.populateQue = function ()
{
	var noOfQue=2;
	VerbLed.queArr=[];
	var randLevel=Math.floor(Math.random()*2);
	for(var i=0;i<noOfQue;i++)
	{
		VerbLed.randomQue=Math.floor(Math.random()*VerbLed.data[randLevel].Array.length);
		while(VerbLed.queArr.includes(VerbLed.data[randLevel].Array[VerbLed.randomQue]))
		VerbLed.randomQue=Math.floor(Math.random()*VerbLed.data[randLevel].Array.length);
		VerbLed.queArr.push(VerbLed.data[randLevel].Array[VerbLed.randomQue]);
	}
	console.log(VerbLed.queArr);
}
VerbLed.tenseDiv=function()
{
	VerbLed.optionsArray=[];
	for(var i=0;i<3;i++)
	{
		var tenseDiv = document.createElement("div");
		tenseDiv.setAttribute("id","tenseDiv"+i);
		tenseDiv.setAttribute("class","VerbLed nopadding col-sm-4 col-lg-4 col-md-4 col-xs-4 col-sm-offset-0 col-lg-offset-0 col-md-offset-0 col-xs-offset-0");
		document.getElementById('VerbLed').appendChild(tenseDiv);
		$(tenseDiv).text(VerbLed.tensDataArray_h[i]);
	}

	for(var i=0;i<3;i++)
	{
		var tenseDiv = document.createElement("div");
		tenseDiv.setAttribute("id","tenseType"+i);
		tenseDiv.setAttribute("class","Verbtype ");
		document.getElementById('VerbType').appendChild(tenseDiv);
		$(tenseDiv).text(VerbLed.tensTypeArray[i]);
	}
	for(var i=0;i<3;i++)
	{
		VerbLed.selectTense();
		var tenseDiv = document.createElement("div");
		tenseDiv.setAttribute("id","rowDiv"+i);
		tenseDiv.setAttribute("class"," nopadding col-sm-4 col-lg-4 col-md-4 col-xs-4");
		document.getElementById('tenseChart').appendChild(tenseDiv);
		for(var j=0;j<3;j++)
		{
			if(j==0)
				var queTense=VerbLed.queArr[VerbLed.queCnt].list[VerbLed.tenseSequence].simple;
			else if(j==1)
				var queTense=VerbLed.queArr[VerbLed.queCnt].list[VerbLed.tenseSequence].continuous;
			else
				var queTense=VerbLed.queArr[VerbLed.queCnt].list[VerbLed.tenseSequence].perfect;

			var tenseDiv = document.createElement("div");
			tenseDiv.setAttribute("id","tenseTypeDiv"+i+""+j);
			tenseDiv.setAttribute("class","sentenceDiv style col-sm-12 col-lg-12 col-md-12 col-xs-12 col-sm-offset-0 col-lg-offset-0 col-md-offset-0 col-xs-offset-0");
			tenseDiv.setAttribute("onclick","VerbLed.selectSell(this.id)");
			tenseDiv.setAttribute("data","off");
			tenseDiv.setAttribute("audioData",queTense[0].audio);
			document.getElementById("rowDiv"+i).appendChild(tenseDiv);
			document.getElementById("tenseTypeDiv"+i+""+j).style.background=Utils.path+"url('images/block.png')";
			document.getElementById("tenseTypeDiv"+i+""+j).style.backgroundSize="100% 100%";   //inline css priority more than external css
			for(var k=0;k<2;k++)
			{
				var randNo=Math.floor(Math.random()*2);
				var tenseDiv = document.createElement("div");
				tenseDiv.setAttribute("id","langDiv"+i+""+j+""+k);
				tenseDiv.setAttribute("class","divStyle nopadding col-sm-12 col-lg-12 col-md-12 col-xs-12");
				document.getElementById("tenseTypeDiv"+i+""+j).appendChild(tenseDiv);
				if(k==1)
					tenseDiv.setAttribute("class","engText divStyle nopadding col-sm-12 col-lg-12 col-md-12 col-xs-12");
				if(k==0)
					$(tenseDiv).text(queTense[randNo].langSentence);
				else
				{
					$(".engText").css({"border-bottom": "3px dotted","height":"6.2vh"});
					$(tenseDiv).data("data",queTense[randNo].sentence);
					VerbLed.optionsArray.push(queTense[randNo]);
				}
			}
		}
	}
}
//select tense sequence
VerbLed.selectTense=function()
{
	for(var j=0;j<VerbLed.tensArray.length;j++)
	{
		for(var i=0;i<3;i++)
		{
			if(VerbLed.queArr[VerbLed.queCnt].list[i].tense==VerbLed.tensArray[j])
			{
				VerbLed.tensArray.splice(j,1);
				VerbLed.tenseSequence=i;
				break;
			}
		}
		break;
	}
}
VerbLed.showQue=function()
{
	VerbLed.ansClicked=false;
	VerbLed.clickedCnt=0;
	clearInterval(VerbLed.noResponseTimer);
	clearTimeout(VerbLed.timeOut);
	VerbLed.seconds=0;
	//VerbLed.startTimer();
	d = new Date();  //date object
	startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();  //to get the current date
	scoreFlag = false;
	label=word=VerbLed.queArr[VerbLed.queCnt].verb;
	totalNos=VerbLed.data.length;
	questionId=VerbLed.queArr[VerbLed.queCnt].questionId;
	$("#verbDiv").text(VerbLed.queArr[VerbLed.queCnt].verb+"-"+VerbLed.queArr[VerbLed.queCnt].langVerb);
	//document.getElementById("optionsDiv").style.display="none";
	VerbLed.tensArray= ["Present","Past","Future"];
	document.getElementById("optionsDiv").innerHTML="";
	VerbLed.createOptions();
	$("#optionsDiv").css({'pointer-events':'none'});
}
VerbLed.selectSell=function(object)	//learn chart
{
	document.getElementById("audio").pause();
	$("#optionsDiv").css({'pointer-events':'auto'});
	clearInterval(VerbLed.noResponseTimer);
	clearTimeout(VerbLed.timeOut);
	VerbLed.seconds=0;
	VerbLed.startTimer();
	$(".sentenceDiv").removeClass("highLight");
	$("#"+object).addClass("highLight");
	VerbLed.ans=$($("#"+object).children()[1]).prop('id');
	VerbLed.ansData=$($("#"+object).children()[1]).data("data");
	VerbLed.highlightedCell=$("#"+object).prop('id');
	$("#optionsDiv").css({'pointer-events':'auto'});
}
VerbLed.nextQue=function()	//skip and next btn
{
	document.getElementById("audio").pause();
	clearInterval(VerbLed.noResponseTimer);
	clearTimeout(VerbLed.timeOut);
	VerbLed.seconds=0;
	VerbLed.startTimer();
	if(VerbLed.ansClicked==true)
	{
		VerbLed.scoreFunction();
	}
	if(scoreFlag!=true)
		{
			VerbLed.queCnt++;
		}
		if(VerbLed.queArr.length==0)
		{
			VerbLed.gameOver();
		}
		else if(VerbLed.queCnt==VerbLed.queArr.length)
		{
			clearInterval(VerbLed.noResponseTimer);
			clearTimeout(VerbLed.timeOut);
			document.getElementById("responseBoxDiv").style.display="block";
			document.getElementById("No").innerHTML=VerbLed.modalData.no;
			document.getElementById("Yes").innerHTML=VerbLed.modalData.yes;
			document.getElementById("questionMsg").innerHTML=VerbLed.modalData.responseMsg;
		}
		else
		{
			document.getElementById("tenseChart").innerHTML="";	
			document.getElementById("VerbLed").innerHTML="";
			document.getElementById("VerbType").innerHTML="";
			VerbLed.tenseDiv();
			VerbLed.showQue();
		}	
}
VerbLed.createOptions=function()
{
	shuffleArray(VerbLed.optionsArray);
	for(var i=0;i<VerbLed.optionsArray.length;i++)
	{
		var optionDiv = document.createElement("Div");
		optionDiv.setAttribute("id","optionDiv"+i);
		optionDiv.setAttribute("class","nopadding options col-sm-4 col-lg-4 col-md-4 col-xs-4");
		document.getElementById('optionsDiv').appendChild(optionDiv);
		optionDiv.setAttribute("onclick","VerbLed.selectAns(this.id)");
		$(optionDiv).text(VerbLed.optionsArray[i].sentence);
		$(optionDiv).data("optionSound",VerbLed.optionsArray[i].audio);
	}
}
VerbLed.selectAns=function(obj)	//checkAns
{
	VerbLed.clickedCnt++;
	VerbLed.ansClicked=true;
	scoreFlag = true;
	$("#optionsDiv").css({'pointer-events':'none'});
	$("#tenseChart").css({'pointer-events':'none'});
	clearInterval(VerbLed.noResponseTimer);
	clearTimeout(VerbLed.timeOut);
	VerbLed.seconds=0;
	VerbLed.startTimer();
	if(VerbLed.clickedCnt==1)
		VerbLed.queArr.splice(VerbLed.queCnt,1);
	document.getElementById("audio").pause(); 
	var selectedText=document.getElementById(obj).innerHTML;
	$("#"+VerbLed.ans).text(selectedText);

	if(selectedText==VerbLed.ansData)
	{
		scoredMarks++;
		if(scoredMarks==9)
		{
			VerbLed.correctCounter++;
			console.log(word,totalNos,type);
			if(Utils.mobileDeviceFlag)
			{
				Android.addLearntData(word,totalNos,type);
			}
		}
		var optionSound=$("#"+obj).data("optionSound");
		getDuration(Utils.path + 'sounds/' +optionSound);
		//document.getElementById("audio").src=Utils.path+'sounds/Instructions/Right_answer__Aditi.mp3';
	}
	else
	{
		document.getElementById("audio").src=Utils.path+'sounds/Instructions/wrong_answer!_Aditi.mp3';
	}
	$("#"+VerbLed.highlightedCell).css({'pointer-events':'none',"opacity":"0.6"});
	document.getElementById("audio").play(); 
	document.getElementById("optionsDiv").innerHTML="";
	VerbLed.createOptions();
	if(VerbLed.clickedCnt==9)
	{
		if (VerbLed.nextQueTimer) {
			clearTimeout(VerbLed.nextQueTimer);
			VerbLed.nextQueTimer = null;
		}
		VerbLed.nextQueTimer = setTimeout(function () {
			VerbLed.nextQue();
		}, 2000);
	}
	if (VerbLed.Timer) {
		clearTimeout(VerbLed.Timer);
		VerbLed.Timer = null;
	}
	VerbLed.Timer = setTimeout(function () {
		
		$("#tenseChart").css({'pointer-events':'auto'});
	}, 800);
}
VerbLed.showSkippedQuestions=function()
{
	document.getElementById("responseBoxDiv").style.display="none";
	VerbLed.queCnt=0;
	document.getElementById("tenseChart").innerHTML="";	
	document.getElementById("VerbLed").innerHTML="";
	document.getElementById("VerbType").innerHTML="";
	VerbLed.tenseDiv();
	VerbLed.showQue();
}

function shuffleArray(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;
}

VerbLed.scoreFunction=function()		//score Function
{
	console.log(resId,questionId,scoredMarks,totalMarks,1,startTime,label);
	if(Utils.mobileDeviceFlag)
	{
		Android.addScore(resId,questionId,scoredMarks,totalMarks,1,startTime,label);
	}
}
VerbLed.gameOver = function () {
	document.getElementById("audio").pause(); 
	$("#creditDiv").addClass("blink_me");
	clearInterval(VerbLed.noResponseTimer);
	document.getElementById("gamePage").style.display = "none";
	document.getElementById("finalPage").style.display="block";
	document.getElementById("responseBoxDiv").style.display="none";
	document.getElementById('gameOver').innerHTML=VerbLed.hardcodedData.message2;
	for(i=0;i<Object.keys(VerbLed.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(VerbLed.AcknowledgementPageData)[i]+" : "+Object.values(VerbLed.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=VerbLed.hardcodedData['createdOn'];
	acknowledgementDiv.appendChild(div);
	document.getElementById("acknowledgementDiv").style.display="none";
	VerbLed.createStars();
	VerbLed.giveStars();
}
VerbLed.showCredits=function()			//show credits on click credit button
{
	document.getElementById("acknowledgementDiv").style.display="block";
}
//function which creates star divs
VerbLed.createStars=function()
{
	var newImage,newDiv,i,numberOfStars=2;
	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/star1.png");
		$(newDiv).append(newImage);
		$("#starsDiv").append(newDiv);
	}
};

VerbLed.giveStars=function()
{
	var fractionVar,i,temp; 
	var numberOfStars=0;
	ans=VerbLed.total_No_of_Questions/2;
	temp=VerbLed.correctCounter/ans;
	numberOfStars=Math.floor(temp);
	temp=Math.round(temp);
	console.log(numberOfStars);
	console.log(temp);
	for(i=1;i<=Math.floor(numberOfStars);i++)
	document.getElementById("starImg"+i).setAttribute("src",Utils.path+"images/star2.png");
	/* if(temp>numberOfStars)
	document.getElementById("starImg"+i).setAttribute("src",Utils.path+"images/halfStar.png"); */
	$("#starsDiv").css("visibility","visible");
	if(numberOfStars!=0)
	{
	document.getElementById("audio").src=Utils.path+"sounds/Instructions/"+"Star"+numberOfStars+".mp3";
	document.getElementById("audio").play();
	}
	console.log(VerbLed.correctCounter,VerbLed.total_No_of_Questions,"1");
	if(Utils.mobileDeviceFlag)
		Android.StudentCertification(VerbLed.correctCounter,VerbLed.total_No_of_Questions,"1");
};

VerbLed.startTimer=function()
{
	if(VerbLed.noResponseTimer)
	{
		clearInterval(VerbLed.noResponseTimer);
		VerbLed.noResponseTimer=null;
		VerbLed.seconds=0;
	}	
	VerbLed.noResponseTimer=setInterval(function() 
	{
		VerbLed.seconds++;
		if(VerbLed.seconds>=5)
		{
			document.getElementById("audio").src=Utils.path+"sounds/instructions/please_ans_the_ques.mp3";
			document.getElementById("audio").play(); 
			clearInterval(VerbLed.noResponseTimer);
			if(VerbLed.timeOut)
			{
				clearTimeout(VerbLed.timeOut);
				VerbLed.timeOut=null;
			}
				VerbLed.timeOut=setTimeout(function()
			{
				VerbLed.seconds=0;
				VerbLed.startTimer();
			},2000);
		}
	},1000);
};
VerbLed.goNext=function()
{
	clearInterval(VerbLed.skipTimer);
	if(Utils.mobileDeviceFlag)
	Android.GotoNextGame();
}

function getDuration(src) {
	var duration = 0;
	//if (ShowMe.soundFlag) {
	$('#audio').on("loadedmetadata", function () {
		duration = $('#audio').prop('duration');
		$('#audio').unbind("loadedmetadata");
		VerbLed.playNextSound(duration);
	});
	$('#audio').prop('src', src);
}
VerbLed.playNextSound = function (duration)						//function for instruction
{
	document.getElementById('audio').play();
	if (VerbLed.instructionTimer) {
		clearTimeout(VerbLed.instructionTimer);
		VerbLed.instructionTimer = null;
	}
	VerbLed.instructionTimer = setTimeout(function () {
		VerbLed.startTimer();
	}, duration * 1000);
}

VerbLed.playInstruction = function () {			//play instruction
	clearInterval(VerbLed.noResponseTimer);
	clearTimeout(VerbLed.timeOut);
	VerbLed.seconds=0;
	getDuration(Utils.path + 'sounds/Instructions/' +VerbLed.hardcodedData.instruction);
	/* document.getElementById('audio').src=Utils.path+'sounds/Instructions/'+ VerbLed.hardcodedData.instruction;
	document.getElementById('audio').play(); */
}