var FindWord={};

var Utils={};
Utils.Path='';
Utils.mobileDeviceFlag=false;

FindWord.totalscore=0;
FindWord.wordID=1;
FindWord.indexArray=[];
FindWord.nextTimeout=0;
FindWord.playWord=0;
FindWord.wrongTimeout=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("FindWord"); 
	setTimeout(function()
	{
		FindWord.setImages();
		if(FindWord.hardCodedData['languageFont']!=null)
			setFontFamilyForLang("fonts/"+FindWord.hardCodedData['languageFont']);		
	},200);
});

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");            
}

FindWord.setImages=function()
{
	document.body.style.backgroundImage = "url("+Utils.Path+FindWord.hardCodedData.cover_page+")";
	document.getElementById('home').src=Utils.Path+"images/Home.png";
	document.getElementById('logo').src=Utils.Path+"images/logo.png";
	document.getElementById('playImage').src=Utils.Path+"images/Play.png";
	document.getElementById('playSoundBtn').src=Utils.Path+"images/Sound.png";
	document.getElementById('nextQueBtn').src=Utils.Path+"images/Next.png";
}

// set suffix and sound
FindWord.playgame=function()
{
	FindWord.correct=0;
	FindWord.wrong=0;
	FindWord.round++;
	document.getElementById("home").style.display="block";
	document.getElementById("coverPage").style.display="none";
	document.getElementById("gamePage").style.display="block";
	FindWord.createDiv();	
	FindWord.setQuestionAndOption();
}	
//create 16 div
FindWord.createDiv=function()			
{
	var parentDiv,wordDiv,outerDiv,wordSpan;
	parentDiv=document.getElementById("grid");
	wordDiv=document.createElement('div');
	wordDiv.setAttribute("id","div1");
	wordDiv.setAttribute("class","col-xs-12 col-sm-12 col-md-12 col-lg-12 nopadding");
	parentDiv.appendChild(wordDiv);
	for(var i=1;i<=4;i++)
	{
		outerDiv=document.createElement('div');
		outerDiv.setAttribute("class","col-xs-12 col-sm-12 col-md-12 col-lg-12 nopadding");
		wordDiv.appendChild(outerDiv);
		for(var j=1;j<=4;j++)
		{
			wordSpan=document.createElement('span');
			wordSpan.setAttribute("id","word"+FindWord.wordID);
			wordSpan.setAttribute("class","col-xs-3 col-sm-3 col-md-3 col-lg-3 nopadding");
			FindWord.wordID++;
			wordSpan.setAttribute("onclick","FindWord.checkSuffix(this.id)");
			outerDiv.appendChild(wordSpan);
		}	
	}	
}

//set Question and other option
FindWord.setQuestionAndOption=function()
{
	var randomGridDiv=0,randomQuestion=0,num=0,indexOfSuffix,tempArr2=[],tempArr=[];
	FindWord.correct=0;
	FindWord.wrong=0;
	FindWord.indexArray=[],FindWord.suffixCount=0;
	clearTimeout(FindWord.nextTimeout);
	clearTimeout(FindWord.wrongTimeout);
	document.getElementById("playSound").pause();
	$(".spanclass2").css({backgroundColor:'#0097A7'});
	//set Question
	randomQuestion=(Math.floor(Math.random() *(FindWord.wordsData.length-1)) + 0);
	FindWord.playWord=FindWord.wordsData[randomQuestion].word;
	document.getElementById("suffixValue").innerHTML=FindWord.playWord;

	if(Utils.mobileDeviceFlag)
		Android.playTts(FindWord.playWord,'eng');
	else
		saySomething(FindWord.playWord);	
	//document.getElementById("playSound").src=Utils.Path+"sounds/"+FindWord.wordsData[randomQuestion].sound;

	FindWord.suffixCount=Math.floor(Math.random() * 4) + 2;
	for(var i=0;i<FindWord.suffixCount;i++)
	{
		randomGridDiv=(Math.floor(Math.random()*16)+1);
		if(FindWord.indexArray.includes(randomGridDiv))
		{
			i--;
			continue;
		}
		else
		{
			FindWord.indexArray[i]=randomGridDiv;
			document.getElementById("word"+randomGridDiv).setAttribute('class',"nopadding col-sm-offset-1 col-xs-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-2 col-sm-2 col-md-2 col-lg-2  spanclass1 spanclass2 ");
			document.getElementById("word"+randomGridDiv).innerHTML=FindWord.wordsData[randomQuestion].word;
			document.getElementById("word"+randomGridDiv).setAttribute('index',randomGridDiv);
		}
	}
	for(var i=0;i<16-(FindWord.indexArray.length);i++)
	{
		num=(Math.floor(Math.random()*16)+1);
		indexOfSuffix=(Math.floor(Math.random() *(FindWord.wordsData.length-1)) + 0);
		if(FindWord.indexArray.includes(num) || tempArr2.includes(indexOfSuffix) || tempArr.includes(num) || indexOfSuffix==randomQuestion)
		{
			i--;
			continue;
		}
		else
		{
			tempArr[i]=num;
			tempArr2[i]= indexOfSuffix;
			document.getElementById("word"+num).setAttribute('class',"nopadding col-sm-offset-1 col-xs-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-2 col-sm-2 col-md-2 col-lg-2 spanclass1 spanclass2");
			document.getElementById("word"+num).innerHTML=FindWord.wordsData[indexOfSuffix].word;
			document.getElementById("word"+num).setAttribute('index',num);		
		}			
	}
	$('.spanclass1').css('pointer-events','auto');
}

////for tts
function saySomething(whatToSay)
{
	var synth,speech;
	synth = window.speechSynthesis;
	if('speechSynthesis' in window)
	{
		speech = new SpeechSynthesisUtterance(whatToSay);
		speech.lang = 'hi-IN'; //en-US
		window.speechSynthesis.speak(speech);
	}
} 

//on sound click
FindWord.playSound=function()
{
	//document.getElementById("playSound").play();
	if(Utils.mobileDeviceFlag)
		Android.playTts(FindWord.playWord,'eng');
	else
		saySomething(FindWord.playWord);	
}

//check suffix right or wrong and move jerry
FindWord.checkSuffix=function(myID)
{
	var getClickedAttribute;
	getClickedAttribute=document.getElementById(myID).getAttribute('index');
	$('#'+myID).css('pointer-events','none');
	//for correct suffix
	if(FindWord.indexArray.includes(Number(getClickedAttribute)))		
	{
		FindWord.correct++;
		document.getElementById(myID).style.backgroundColor="green";
		if(FindWord.wrong+FindWord.correct==9 || FindWord.correct==FindWord.suffixCount)
		{
			$(".spanclass1").css({pointerEvents:'none'});
			document.getElementById('playSound').src=Utils.Path+"sounds/CorrectSound.mp3";
			document.getElementById('playSound').play();
			if(FindWord.nextTimout)
			{
				clearTimeout(FindWord.nextTimout);
				FindWord.nextTimout=null;
			}
			FindWord.nextTimeout=setTimeout(function()
			{
				FindWord.calculateResult();
			},3000);
		}
	}
	//for wrong suffix
	else 			
	{
		FindWord.wrong++;
		document.getElementById(myID).style.backgroundColor="red";
		if(FindWord.wrong+FindWord.correct==9)
		{
			$(".spanclass1").css({pointerEvents:'none'});
			document.getElementById('playSound').src=Utils.Path+"sounds/wrong.mp3";
			document.getElementById('playSound').play();
			if(FindWord.wrongTimeout)
			{
				clearTimeout(FindWord.wrongTimeout);
				FindWord.wrongTimeout=null;
			}
			FindWord.wrongTimeout=setTimeout(function()
			{
				FindWord.calculateResult();
			},3000);
		}
	}
}

//results
FindWord.calculateResult=function()
{
	document.getElementById("playSound").pause();
	$('.spanclass1').css('pointer-events','auto');
	clearTimeout(FindWord.wrongTimeout);
	clearTimeout(FindWord.nextTimeout);
	if(FindWord.correct==FindWord.suffixCount && FindWord.wrong==0)
	{
		FindWord.totalscore+=10;
		if(FindWord.totalscore==20)
		{
			document.getElementById("suffixDiv").style.display="none";
		}
	}
	else if(FindWord.correct==FindWord.suffixCount && FindWord.wrong!=0)
	{
		FindWord.totalscore+=5;
	}
	FindWord.setQuestionAndOption();
}

FindWord.callHome=function()
{
	document.getElementById("playSound").pause();
	location.reload();
}
