//startingDate: 09-04-2019
var Time={};
var Utils = {};
Utils.Path='';
Utils.mobileDeviceFlag=false;
var questionId=0,startTime=0,scoredMarks=0,totalMarks=40,resId=0,level=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("Time");  */
	setTimeout(function()
	{
		Time.init();
	},100);
});

Time.init=function()
{
	$("#coverPage").show();
	$("#gamePage").hide();
	$(".header").not("#logoDiv").css("visibility","hidden");
	document.getElementById("picture").style.backgroundImage = "url("+Utils.Path+"images/room.jpg"+")";
	Time.initializeVariables();
	Time.setAllImages();
}

Time.initializeVariables=function()
{
	Time.amPmArr=["AM","PM"];
	Time.questionCtr=0;
	Time.recordClickFlag=false;
	Time.sttArr=[];
	Time.ansArr=[];
	 Time.ans1=' ';
	
}
Time.setAllImages=function()
{
	
	document.getElementById("clockDiv").style.backgroundImage = "url("+Utils.Path+'images/clock.jfif'+")";
	document.getElementById("flowerPotDiv").style.backgroundImage = "url("+Utils.Path+'images/flowerPot.png'+")";
	document.getElementById("calendarDiv").style.backgroundImage = "url("+Utils.Path+'images/calender.png'+")";
	document.getElementById("table").style.backgroundImage = "url("+Utils.Path+'images/table.png'+")";
	document.getElementById("home").src=Utils.Path+"images/Home.png";
	document.getElementById("logo").src=Utils.Path+"images/logo.png";
	document.getElementById("playImg").src=Utils.Path+"images/playButton.png";
	document.getElementById("next").src=Utils.Path+"images/Next.png";
	document.getElementById("mike").src=Utils.Path+"images/Record.png";
	
}
Time.playGame=function()
{
	$("#coverPage").hide();
	$("#gamePage,#clockDiv,#question").show();
	$(".header").css("visibility","visible"); 
	Time.setQuestion();

}

Time.setQuestion=function()
{
	var hour,minute,rno,monthNo,dayNo,dateNo,question;
	Time.randHr=Math.floor(Math.random()*12+1);
	// while(Time.randHr>=10)
	// 	Time.randHr=Math.floor(Math.random()*12+1);
	hour=Time.randHr;
	if(Time.randHr<10)
		hour="0"+Time.randHr;
	Time.randMin=Math.floor(Math.random()*60);
	// while(Time.randMin>=10)
	// 	Time.randMin=Math.floor(Math.random()*60);
	minute=Time.randMin;
	if(Time.randMin<10)
		minute="0"+Time.randMin;
	rno=Math.floor(Math.random()*Time.amPmArr.length);
	Time.amPm=Time.amPmArr[rno];
	if((Time.amPm=="AM" && (Time.randHr>=6 && Time.randHr<12)) || Time.amPm=="PM" && Time.randHr<=6)
		document.getElementById("windowDiv").style.backgroundImage = "url("+Utils.Path+'images/windowDay.jpg'+")";
	else
		document.getElementById("windowDiv").style.backgroundImage = "url("+Utils.Path+'images/windowNight.jpg'+")";
	document.getElementById("timeSpan").innerHTML=hour+":"+minute+" "+Time.amPm;
	monthNo=Math.floor(Math.random()*Time.monthArr.length);
	Time.month=Time.monthArr[monthNo];
	if(monthNo<5)
	{
		if(monthNo==0)
			Time.date=Math.floor(Math.random()*29+1);
		else
			Time.date=Math.floor(Math.random()*30+1);
	}
	else	
		Time.date=Math.floor(Math.random()*31+1);
	dayNo=Math.floor(Math.random()*Time.daysArr.length);
	Time.day=Time.daysArr[dayNo];
	document.getElementById("month").innerHTML=Time.month;
	document.getElementById("date").innerHTML=Time.date;
	document.getElementById("day").innerHTML=Time.day;

	Time.randQue=Math.floor(Math.random()*Time.questionArr.length);
	question=Time.questionArr[Time.randQue].question;
	document.getElementById("questionDiv").innerHTML=question;
	Time.findAnswer();
}

Time.findAnswer=function()
{
	switch(Time.randQue)
	{
		case 0:Time.ans=Time.month;
				break;
		case 1:Time.ans=Time.date;
				break;		
		case 2:Time.ans=Time.day;
				break;
		case 3:Time.ans=Time.randHr+' '+Time.randMin+' '+Time.amPm;// document.getElementById("timeSpan").innerHTML;
			   Time.ans1=Time.randHr+''+Time.randMin+' '+Time.amPm;
				break;				
	}
	console.log(Time.ans);
}

Time.record=function()
{

	//Time.stopSound();
	Time.ansString="";
	if(Time.recordClickFlag==false)
	{
		Time.recordClickFlag=true;
		if(Utils.mobileDeviceFlag)
			Android.startSttArray("eng");
		$("#mike").prop('src',Utils.Path+"images/Stop.png");
		$("#mike").addClass("blink_me");
		if(Time.checkStatus)
		{
            clearInterval(Time.checkStatus);
            Time.checkStatus = null;
        }
		Time.checkStatus = setInterval(function() 
		{
			if (Utils.mobileDeviceFlag)
				Time.sttString = Android.getSttResult();
			console.log(Time.sttString);
			if('$$$'!== Time.sttString)
			{ 
				clearInterval(Time.checkStatus);
				if(Time.sttString !== "###" && Time.sttString !== "")
					 Time.checkAnswer();
				else
				{
					console.log('Stops');
					Time.seconds=0;
					//Time.startTimer();
				}
				Time.recordClickFlag = false;
				$("#mike").prop('src',Utils.Path+"images/Record.png");
				$('#mike').removeClass('blink_me');
			}
		},500);
	}
	else
	{
		Time.recordClickFlag = false;
		if(Utils.mobileDeviceFlag)
			Android.stopStt();
		Time.sttString="";
		clearInterval(Time.checkStatus);
		$("#mike").prop('src',Utils.Path+"images/Record.png");
		$("#mike").removeClass("blink_me");
		Time.seconds=0;
	//	Time.startTimer();
	}
}

Time.checkAnswer=function()
{
	var i,j,ansFlag=false,counter=0,splitArr=[],ansFoundFlag=false,lowercaseString;
	console.log(Time.sttString);
	
	//Time.sttString=Time.sttString.replace(/(~|`|!|@|#|$|%|^|&|\*|\(|\)|{|}|\[|\]|;|:|\"|'|<|,|\.|>|\?|\/|\\|\||-|_|\+|=)/g," ")
	Time.sttArr=Time.sttString.split(",");	
	console.log(Time.sttArr);
	Time.sttArr.splice(Time.sttArr.length-1);
	//alert(Time.sttArr);
	console.log(Time.sttString);
	Time.ans=Time.ans.toString()
	Time.ansArr=Time.ans.split(" ");
	Time.ansArr1=Time.ans1.split(" ");

	alert(Time.sttArr);
	console.log(Time.ansArr);
	if(Time.sttArr.includes(Time.ans))
	{
		ansFoundFlag=true;
		alert("correct");
	}
	else
	{
		for(i=0;i<Time.sttArr.length;i++)
		{
			counter=0;
			Time.sttArr[i]=regex(Time.sttArr[i]);
			console.log(Time.sttArr[i]);
			lowercaseString=Time.sttArr[i].toLowerCase();
			splitArr=lowercaseString.split(" ");
			for(j=0;j<Time.ansArr.length;j++)
			{
				if(splitArr.includes(Time.ansArr[j].toLowerCase()))
				{
					counter++;
				}
			}
			if(counter/Time.ansArr.length*100>=75)
			{
				ansFoundFlag=true;
				alert("correct");
				break;
			}
			else
			{
				counter=0;
				for(j=0;j<Time.ansArr1.length;j++)
				{
					if(splitArr.includes(Time.ansArr1[j].toLowerCase()))
					{
						counter++;
					}
				}
				if(counter/Time.ansArr1.length*100>=75)
				{
					ansFoundFlag=true;
					alert("correct");
					break;
				}
			}	
		}
	}
	if(ansFoundFlag)
		{
			if(Time.nextQueTimer)
				{
					clearTimeout(Time.nextQueTimer);
					Time.nextQueTimer=null;
				}
				Time.nextQueTimer=setTimeout(function(){Time.nextQuestion();},500);
			
		}
		else
		{
			ansFoundFlag=false;
			alert("wrong");

		}
	//alert(Time.ansString);
		
}

Time.nextQuestion=function()
{
	clearTimeout(Time.nextQueTimer);
	$("#mike").removeClass("blink_me");
	$("#mike").prop('src',Utils.Path+"images/Record.png");
	Time.questionCtr++;
	Time.setQuestion();

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

function regex (str) {
    str= str.replace(/(~|`|!|@|#|$|%|^|&|\(|\)|{|}|\[|\]|;|:|\"|'|<|,|>|\?|\/|\\|\||-|_|=)/g," ");
    return str.replace(/\./gi,"");
}