var MagicLamp={};
var Utils = {};
Utils.Path='';
Utils.mobileDeviceFlag=false;
var questionId=0,startTime=0,scoredMarks=0,totalMarks=10,resId=0,level=0;
MagicLamp.recordClickFlag=false;
MagicLamp.ansString="";
$(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("MagicLamp");  */
	setTimeout(function()
	{
		MagicLamp.init();
	},100);
});

MagicLamp.init=function()
{
	document.body.style.backgroundImage="url("+Utils.Path+"images/Bg.png)";
	$("#coverPage").show();
	$("#gamePage").hide();
	$(".header").not("#logo").css("visibility","hidden");
	MagicLamp.initializeVariables();
	MagicLamp.setImages();
}
//Initialization of variables.
MagicLamp.initializeVariables=function()
{
	MagicLamp.wordArray=[];
	MagicLamp.questionCounter=0;
	MagicLamp.loadmetaDataFlag=false;
	MagicLamp.playWordSoundFlag=true;
	MagicLamp.lampFlag=false;
	MagicLamp.correctCounter=0;
	MagicLamp.attemptCounter=0;
	MagicLamp.score=0;
	MagicLamp.correctCnt=0;
	MagicLamp.recordFlag=true;
}
//Sets the images to all buttons and image tags.
MagicLamp.setImages=function()
{
	document.getElementById('play').src=Utils.Path+"images/playButton.png";
	document.getElementById('homeImage').src=Utils.Path+"images/Home.png";
	document.getElementById('logoImage').src=Utils.Path+"images/logo.png";
	//document.getElementById("soundImg").src=Utils.Path+"images/Sound.png";
	document.getElementById('nextImg').src=Utils.Path+"images/Next.png";
	document.getElementById("score").innerHTML=MagicLamp.hardCodedData.score+MagicLamp.score;
	document.getElementById("recordImg").src=Utils.Path+"images/Record.png";
}

MagicLamp.playGame=function()
{
	//document.body.style.backgroundColor="#387DED";
	$("#coverPage").hide();
	$("#gamePage").show();
	$(".header").css("visibility","visible");
	MagicLamp.createDivs();
	MagicLamp.setOptions();
//	MagicLamp.setQuestion();
}
MagicLamp.createDivs=function()
{
	var i,j,div,img,parentDiv,para,rNo,randArr=[];
	parentDiv=document.getElementById("optionDiv");
	
	for(j=0;j<5;j++)
	{
		rNo=Math.floor(Math.random()*25)+1;
		if(randArr.includes(rNo))
		{
			j--;
			continue;
		}
		else
		{
			randArr.push(rNo);
		}
	}
	
	for(i=1;i<=25;i++)
	{
		div=document.createElement("DIV");
		div.setAttribute("id","optionDiv"+i);
		if(i%5==1)
			div.setAttribute("class","options nopadding col-xs-2 col-md-2 col-lg-2 col-sm-2 col-xs-offset-1 col-md-offset-1 col-lg-offset-1 col-sm-offset-1");
		else
			div.setAttribute("class","options nopadding col-xs-2 col-md-2 col-lg-2 col-sm-2");
		div.style.backgroundImage="Url("+Utils.Path+"images/divBg.jpeg)";	
		div.setAttribute("onclick","MagicLamp.displayWord(this.id);");
		if(randArr.includes(i))
			div.setAttribute("lampFlag",1);
		else
			div.setAttribute("lampFlag",0);
		
		img=document.createElement("IMG");
		img.setAttribute("id","image"+i);
		img.setAttribute("class","img-responsive nopadding col-xs-12 col-md-12 col-lg-12 col-sm-12 images");
		//img.setAttribute("src",Utils.Path+"images/card.jpeg");
		para=document.createElement("P");
		para.setAttribute("id","para"+i);
		para.setAttribute("class","words");
		div.appendChild(img);
		div.appendChild(para);
		parentDiv.appendChild(div);
	}
	
}
MagicLamp.setOptions=function()
{
	var i, j,k,d,randomWord,rNo,imgArr=[],randomArr=[],randomIndex,lampCounter=0;
	d=new Date();
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	for(i=1;i<=25;i++)
	{
		randomWord=Math.floor(Math.random()*MagicLamp.wordData.length);
		if(MagicLamp.wordArray.includes(randomWord))
		{
			i--;
			continue;
		}
		else
		{
			MagicLamp.wordArray.push(randomWord);
			document.getElementById("image"+i).src=Utils.Path+"images/card.jpeg";
			document.getElementById("para"+i).innerHTML=MagicLamp.wordData[randomWord].word;
			document.getElementById("optionDiv"+i).setAttribute("questionId",MagicLamp.wordData[randomWord].questionId);
		}
	}	
}

MagicLamp.displayWord=function(div)
{
	questionId=parseInt(document.getElementById(div).getAttribute("questionId"));
	console.log(questionId);
	MagicLamp.answer=$("#"+div).children('p').text();
	if(MagicLamp.recordFlag==false)
	{
		$("#"+MagicLamp.clickedDiv.children[0].id).css("display","block");
		$("#"+MagicLamp.clickedDiv.children[1].id).css("display","none");
	}
	else
		MagicLamp.recordFlag=false;
	MagicLamp.clickedDiv=document.getElementById(div);
	document.getElementById("record").style.pointerEvents="auto";
	//$(".words").css("display","none");
	//$(".images").css("display","block");
	//$("#"+div).children('img').addClass('animated bounceOut');
	
	$('.options').removeClass("blink_me");
	$('#'+div).addClass("blink_me");
	$('#'+div).children('img').css("display","none");
	$("#"+div).children('p').css("display","block");
}	
MagicLamp.startRecording=function()
{
	$(".options").css("pointer-events","none");
	if(Utils.mobileDeviceFlag)
            Android.startStt("eng");
	if(MagicLamp.recordClickFlag==false)
	{
		MagicLamp.recordClickFlag=true;
		$("#soundDiv").css("pointer-events","none");
		$("#playAnswerDiv").css("pointer-events","none");
		document.getElementById("recordImg").src=Utils.Path+"images/Stop.png";
		$("#recordImg").addClass("blink_me");
		if(MagicLamp.checkStatus)
		{
            clearInterval(MagicLamp.checkStatus);
            MagicLamp.checkStatus = null;
        }
		MagicLamp.checkStatus = setInterval(function() 
		{
			if (Utils.mobileDeviceFlag)
				MagicLamp.ansString = Android.getSttResult();
			if('$$$'!== MagicLamp.ansString)
			{
				clearInterval(MagicLamp.checkStatus);
				if(MagicLamp.ansString !== "###" && MagicLamp.ansString !== "")
				{
					 MagicLamp.checkAnswer(MagicLamp.ansString);
				} 
				else
				{
					console.log('Stops');
					$(".options").css("pointer-events","auto");	
					$(".noPointer").css("pointer-events","none");
				}
				clearInterval(MagicLamp.checkStatus);
				MagicLamp.recordClickFlag = false;
				document.getElementById('recordImg').src ='images/Record.png';
				$('#recordImg').removeClass('blink_me');	
			}	
		},300);
	}
	else
	{
		$(".options").css("pointer-events","auto");	
		$(".noPointer").css("pointer-events","none");
		MagicLamp.ansString="";
		clearInterval(MagicLamp.checkStatus);
		MagicLamp.recordClickFlag=false;
		$("#soundDiv").css("pointer-events","auto");
		$("#playAnswerDiv").css("pointer-events","auto");
		document.getElementById("recordImg").src=Utils.Path+"images/Record.png";
		$("#recordImg").removeClass("blink_me");
		if(Utils.mobileDeviceFlag)
			Android.stopStt();
		setTimeout(function() {
		if (Utils.mobileDeviceFlag)
			MagicLamp.ansString = Android.getSttResult();
		if('$$$'!== MagicLamp.ansString)
		{
		   if(MagicLamp.ansString !== "###" && MagicLamp.ansString !== "")
		   {
				MagicLamp.checkAnswer(MagicLamp.ansString);
		   } 
		}	
		 },1500);
	}
}

MagicLamp.checkAnswer=function(str)
{
	var divId;
	divId=$(MagicLamp.clickedDiv).prop('id');
	$("#"+divId).addClass("noPointer");
	$("#gamePage").hide();
	$("#responseBoxDiv").show();
	MagicLamp.recordFlag=true;
	MagicLamp.clickedDiv.style.pointerEvents="none";
	console.log(str);
	console.log(MagicLamp.answer);
	str=str.trim();
	MagicLamp.answer=MagicLamp.answer.trim();
	
	if(str.toLowerCase()==MagicLamp.answer.toLowerCase())
	{	
		scoredMarks=10;
		MagicLamp.correctCnt++;
		if((MagicLamp.clickedDiv).getAttribute("lampFlag")==1)
		{
			$("#"+MagicLamp.clickedDiv.children[0].id).css("display","block");
			$("#"+MagicLamp.clickedDiv.children[0].id).prop("src",Utils.Path+"images/lamp.gif");
			$("#"+MagicLamp.clickedDiv.children[1].id).css("display","none");
			MagicLamp.score+=50;			
			document.getElementById("ansImg").src=Utils.Path+"images/lamp.gif";
			document.getElementById("playSound").src=Utils.Path+"sounds/SoundForCorrect.mp3";
		}
		else
		{
			MagicLamp.score+=10;
			document.getElementById("ansImg").src=Utils.Path+"images/correct.png";
			document.getElementById("playSound").src=Utils.Path+"sounds/Buzzercorrect.mp3";
		}	
	}
	else
	{
		scoredMarks=0;
		document.getElementById("ansImg").src=Utils.Path+"images/wrong.png";
		document.getElementById("playSound").src=Utils.Path+"sounds/BuzzerWrong.mp3";
	}
	document.getElementById("playSound").play();
	document.getElementById("score").innerHTML=MagicLamp.hardCodedData.score+MagicLamp.score;
	document.getElementById("record").style.pointerEvents="none";
	
	console.log(resId,questionId,scoredMarks,totalMarks,level,startTime);
	if(Utils.mobileDeviceFlag)
		Android.addScore(resId,questionId,scoredMarks,totalMarks,level,startTime);	//score
	setTimeout(function()
		{
			$("#gamePage").show();
			$("#responseBoxDiv").hide();
			$(".options").css("pointer-events","auto");
			$(".noPointer").css("pointer-events","none");
		},2000);
			
}

MagicLamp.showNextQuestion=function()
{
	MagicLamp.initializeVariables();
	document.getElementById("score").innerHTML=MagicLamp.hardCodedData.score+MagicLamp.score;
	$('.options').removeClass("blink_me");
	$(".options").removeClass("noPointer");
	$(".options").children('img').css("display","block");
	$(".options").children('p').css("display","none");
	document.getElementById("record").style.pointerEvents="none";
	MagicLamp.setOptions();
}

MagicLamp.callHome=function()
{
	location.reload();
}
