var Identifywords = {};
var Utils = {};
Utils.Path = "";
Utils.mobileDeviceFlag = false;
var questionId = 0, startTime = 0, scoredMarks = 0, totalMarks = 18, resId = 0, level = 1, label = '',scoreFlag = false,questionCnt = 0,nextBtnClick=0,initialOffset,time,wordClick=0,k=9,currentQues=0;
$(document).ready(function () {
    $(".se-pre-con").fadeOut("slow");
    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("hindiVowelConsonant");
    setTimeout(function () {
        Identifywords.Initilize();
    }, 200);
});

Identifywords.Initilize = function () {//shows coverPage
    document.title = Identifywords.hardcodedData.title;
    Identifywords.setImage();
    $("#endPage").hide();
    $("#homeDiv").css("visibility","hidden");
    $("#gamePage").hide();
    $("#levelPage").hide();
    Identifywords.variableInitilization();
    Identifywords.createAcknowledgementDivs();
}
Identifywords.variableInitilization = function(){
    Identifywords.rulesArray=[];
    Identifywords.selectedRule; //selectd verb number   
    Identifywords.quesArray = []; //unique 3 node
    Identifywords.totalQues =[];//27 ques
    Identifywords.randomSoundArray=[];
    Identifywords.backFrontFlag = true;
    Identifywords.homeFlag = false;
   Identifywords.rightAns=0;//total right ans out of 18 at both attempt
    Identifywords.currentQuesSound=0; //ques from quesArray
    Identifywords.interval;
    Identifywords.circularNumber = 0;
    Identifywords.circularTimerFlag = false;
}
Identifywords.setImage = function(){
     document.body.style.backgroundImage = "url('images/skin/bg.png')";
     document.getElementById("logoImg").src = Utils.Path + "images/skin/logo.png";
     document.getElementById("homeImg").src = Utils.Path + "images/skin/home.png";
     document.getElementById("playImage").src = Utils.Path + "images/skin/play.png";
     document.getElementById("soundImage").src = Utils.Path + "images/skin/sound.png";
     document.getElementById("nextImage").src = Utils.Path + "images/skin/next.png";
     document.getElementById("playAgain").src = Utils.Path + "images/skin/replay.png";
     document.getElementById("creditImg").src = Utils.Path + "images/skin/credits.png";
     document.getElementById("goImg").src = Utils.Path + "images/skin/go.png";
     document.getElementById("rulesID").style.backgroundImage =Utils.Path+"url('images/skin/swimimgTank.png')";
     document.getElementById("titleId").style.backgroundImage =Utils.Path+"url('images/skin/titleblock.png')";
     document.getElementById("ruleDiv").style.backgroundImage =Utils.Path+"url('images/skin/titleblock.png')";
     document.getElementById("titleId").innerHTML = Identifywords.hardcodedData.rulesTitle;
}
Identifywords.startGame = function(){
    $("#coverPage").hide();
    $("#homeDiv").css("visibility","visible");
    $("#levelPage").show();
    Identifywords.rulesArray = Identifywords.data.list;
    console.log(Identifywords.rulesArray);
   
    Identifywords.showRules();
}
Identifywords.showRules = function(){
   var i,currentQues,ruleListDiv,radioButtonDiv,ruleDiv;
    for(i=0;i< Identifywords.rulesArray.length;i++)
	{
	     ruleListDiv = document.createElement("div");
		 ruleListDiv.setAttribute("id","ruleListDivId"+i);
		 ruleListDiv.setAttribute("class"," rulesDivClass nopadding col-sm-6 col-xs-6 col-md-6 col-lg-6");
		 document.getElementById("rulesinnerID").appendChild(ruleListDiv);
		 //radio button
		 radioButtonDiv = document.createElement("INPUT");
		 radioButtonDiv.setAttribute("id","radioButtonId"+i);
		 radioButtonDiv.setAttribute("class","nopadding radioButtonClass checkmark col-sm-4 col-xs-4 col-md-4 col-lg-4");
		 radioButtonDiv.setAttribute("type", "radio");
		 radioButtonDiv.setAttribute("name", "verbs");
		 radioButtonDiv.setAttribute("onclick", "Identifywords.selectRule(this.id)");
		 $(radioButtonDiv).data("ruleIndex",i);
		 document.getElementById("ruleListDivId"+i).appendChild(radioButtonDiv);
		 ruleDiv = document.createElement("div");
         ruleDiv.setAttribute("id","ruleDiv"+i);
         ruleDiv.setAttribute("class","nopadding ruleClass col-sm-6 col-lg-6 col-md-6 col-xs-6");
		$(ruleDiv).text();
		document.getElementById("ruleListDivId"+i).appendChild(ruleDiv);
		$(ruleDiv).text(Identifywords.data.list[i].rule);
    }
    $("#radioButtonId0").prop("checked", true);
	Identifywords.selectedRule=$("#radioButtonId0").data("ruleIndex"); 
}
Identifywords.selectRule = function(selectedRule){
    Identifywords.selectedRule = $("#"+selectedRule).data("ruleIndex");
    console.log(Identifywords.selectedRule);  
}
Identifywords.learningPage = function(){
    document.getElementById("ruleDiv").style.visibility = "visible";
    Identifywords.homeFlag = true;
    $("#levelPage").hide();
    $("#gamePage").show();
    Identifywords.quesArray=[];
     var jsonObj ={};
    populate(Identifywords.data.list[Identifywords.selectedRule].data,Identifywords.quesArray,3);
    console.log(Identifywords.quesArray);
    for(var i=0;i<Identifywords.quesArray.length;i++){
        for(var j=0;j<9;j++){
            Identifywords.totalQues.push(Identifywords.data.list[Identifywords.selectedRule].data[Identifywords.quesArray[i]].wordsList[j]);
        }
    }
      console.log(Identifywords.totalQues);
     document.getElementById("ruleDiv").innerHTML = Identifywords.data.list[Identifywords.selectedRule].rule; 
      Identifywords.wordsDiv();
}
Identifywords.wordsDiv = function(){ //set the learning chart
     var i,j,wordDiv,tenseDiv,sellIndex=0,quesCount=0;
     $(".wordsStyleClass").css("pointer-events","auto");
     for(k=0;k<9;k++){
        wordDiv = document.createElement("div");
        wordDiv.setAttribute("id","wordDivId"+k);
        wordDiv.setAttribute("class","noPadding wordDivClass col-sm-3 col-xs-3 co-lg-3 col-md-3");
        wordDiv.style.backgroundImage = Utils.Path+"url('images/skin/block.png')";
        document.getElementById("wordsChart").appendChild(wordDiv);
     }
       for(j=0;j<3;j++){
          sellIndex = 0 ;
          for(i=0;i<9;i++){
          tenseDiv = document.createElement("div");
		  tenseDiv.setAttribute("id","tenseDiv"+i+j);
          tenseDiv.setAttribute("sellIndex",sellIndex);
          sellIndex++;
          tenseDiv.setAttribute("onclick","Identifywords.checkWord(this.id)");
         $(tenseDiv).text(Identifywords.totalQues[quesCount].langWord); 
         $(tenseDiv).data("sound",Identifywords.totalQues[quesCount].sound);
         document.getElementById("wordDivId"+i).appendChild(tenseDiv);
         quesCount++;
        if(j==0){
            tenseDiv.setAttribute("class","wordsStyleClass faceClass0 noPadding col-sm-12 col-lg-12 col-md-12 col-xs-12");
            Identifywords.randomSoundArray.push(Identifywords.totalQues[i].sound);
             shuffle(Identifywords.randomSoundArray);
        }
        else if(j==1)
            tenseDiv.setAttribute("class","wordsStyleClass faceClass1 noPadding col-sm-12 col-lg-12 col-md-12 col-xs-12");
        else
           tenseDiv.setAttribute("class","wordsStyleClass faceClass2 noPadding col-sm-12 col-lg-12 col-md-12 col-xs-12");
    }
 }
    Identifywords.playWord();
}
Identifywords.playNextSound = function(){
    Identifywords.currentQuesSound++;
    if(Identifywords.currentQuesSound<9)
       Identifywords.playWord();
    else
        Identifywords.nextQuestion();
}
Identifywords.playWord = function(){
   clearTimeout(Identifywords.pointerAutotimer);
     if(Identifywords.randomSoundArray !=0){
      document.getElementById("playSound").src = Utils.Path+"sounds/"+Identifywords.randomSoundArray[Identifywords.currentQuesSound];
       document.getElementById("playSound").play();
       $("#playSound").data("soundData",Identifywords.randomSoundArray[Identifywords.currentQuesSound]);
     }
     else
         $("#nextImage").addClass("blink_me");
     
    if(Identifywords.pointerAutotimer){
        clearTimeout(Identifywords.pointerAutotimer);
        Identifywords.pointerAutotimer = null;
    }
    Identifywords.pointerAutotimer = setTimeout(function(){
        $(".frontClass,.backClass").css("pointer-events","auto");
    },1500);
}
Identifywords.checkWord = function(clicked_Words){
    scoreFlag=true;
    document.getElementById("playSound").pause();
    var clickedSound = $("#"+clicked_Words).data("sound");
    var playedSound =$("#playSound").data("soundData");
    if(clickedSound == playedSound){
        clearInterval(Identifywords.interval);
        Identifywords.interval=null;
        Identifywords.rightAns++;
        var idNum = document.getElementById(clicked_Words).getAttribute("sellIndex");
        document.getElementById(clicked_Words).style.color = "green";
        $("#"+clicked_Words).addClass('animated flash');
        document.getElementById("playSound").src = Utils.Path+"sounds/instructions/Right_answer__Aditi.mp3";
        document.getElementById("playSound").play();
        if(Identifywords.correctAnsTimer ||  Identifywords.nextSoundTimer){
            clearTimeout(Identifywords.correctAnsTimer);
            clearTimeout( Identifywords.nextSoundTimer);
            Identifywords.nextSoundTimer = null;
            Identifywords.correctAnsTimer = null;
         }
         if(Identifywords.randomSoundArray.length>0){
            if($("#"+clicked_Words).hasClass("faceClass0")){
                var j=1;
                k = 9 + parseInt(idNum);
                Identifywords.correctAnsTimer = setTimeout(function(){
                    $(".flash").removeClass("flash");  $("#"+clicked_Words).parent().css("transform","rotateX(360deg)"); document.getElementById(clicked_Words).style.color = "black"; 
                    $("#"+clicked_Words).css("visibility","hidden"); $("#tenseDiv"+idNum+j).css("visibility","visible"); },900);

                    Identifywords.randomSoundArray.push(Identifywords.totalQues[k].sound);
            }
            else if($("#"+clicked_Words).hasClass("faceClass1")){
                    j=2;
                    k = 18 + parseInt(idNum);
                    Identifywords.correctAnsTimer = setTimeout(function(){
                        $(".flash").removeClass("flash");  $("#"+clicked_Words).parent().css("transform","rotateX(-360deg)"); document.getElementById(clicked_Words).style.color = "black"; 
                        $("#"+clicked_Words).css("visibility","hidden"); $("#tenseDiv"+idNum+j).css("visibility","visible"); },900);

                        Identifywords.randomSoundArray.push(Identifywords.totalQues[k].sound);
            }
            else
                $("#"+clicked_Words).css("pointer-events","none");
             
            Identifywords.randomSoundArray.splice(Identifywords.currentQuesSound,1);
             shuffle(Identifywords.randomSoundArray);
        }
        Identifywords.nextSoundTimer = setTimeout(function(){ Identifywords.playWord();},2500);
    }
    else{
        document.getElementById(clicked_Words).style.color = "red";
        $("#"+clicked_Words).addClass('animated shake');
        document.getElementById("playSound").src = Utils.Path+"sounds/instructions/Not_this_one__Aditi.mp3";
        document.getElementById("playSound").play();
        if(Identifywords.redcolorTimer){
            clearTimeout(Identifywords.redcolorTimer);
            Identifywords.redcolorTimer=null;
        } 
        Identifywords.redcolorTimer = setTimeout(()=>{
            document.getElementById(clicked_Words).style.color = "black"; $(".shake").removeClass("shake"); $(".frontClass,.backClass").css("pointer-events","auto");
        },800);
    }
}
Identifywords.playInstruction = function(){
   document.getElementById("playSound").src = Utils.Path+"sounds/"+Identifywords.randomSoundArray[Identifywords.currentQuesSound];
    document.getElementById("playSound").play();
}
Identifywords.nextQuestion = function()
{   
    nextBtnClick++;
    if(scoreFlag)
        scoredMarks = Identifywords.rightAns;
    else
        scoredMarks =-1;

    Identifywords.scoreFunction();
    Identifywords.rightAns =0;
    scoreFlag = false;
    document.getElementById("playSound").pause();
    clearTimeout(Identifywords.nextSoundTimer);
    clearTimeout(Identifywords.pointerAutotimer);
    $(".wordsStyleClass").css("pointer-events","none");
    $(".shake").removeClass('shake');
    $(".flash").removeClass('flash');
    $(".blink_me").removeClass('blink_me');
    if(nextBtnClick==2){
        $("#gamePage").hide();
        Identifywords.gameOver();
    }
    else{
        Identifywords.homeFlag = false;
      document.getElementById("wordsChart").innerHTML=" ";
      Identifywords.variableInitilization();
      $("#gamePage").hide();
      $("#levelPage").show();
    }
}
Identifywords.gameOver = function()
{
    Identifywords.homeFlag = false;
    document.getElementById("playSound").pause();
    clearTimeout(Identifywords.nextSoundTimer);
    clearTimeout(Identifywords.pointerAutotimer);
    $("#endPage").show()
    document.getElementById("ruleDiv").style.visibility = "hidden";
    document.getElementById("gameOver").innerHTML=Identifywords.hardcodedData.gameOverText;
    $("#acknowledgementDiv").hide();
}
Identifywords.callHome = function(){
    document.getElementById("playSound").pause();
    document.getElementById("ruleDiv").style.visibility = "hidden";
    document.getElementById("gameOver").innerHTML=" ";
    document.getElementById("wordsChart").innerHTML=" ";
    Identifywords.resetVar();
    if(Identifywords.homeFlag){
        Identifywords.homeFlag=false;
        nextBtnClick=0;
        $("#gamePage").hide();
        $("#edPage").hide();
        $("#levelPage").show();
    }
    else
        location.reload();
}
Identifywords.replayGame = function()
 {
    nextBtnClick=0;
	document.getElementById("gameOver").innerHTML = " ";
	$("#endPage").hide();
    $("#gamePage").hide();
    document.getElementById("wordsChart").innerHTML=" ";
    Identifywords.variableInitilization();
    Identifywords.homeFlag=false;
	$("#levelPage").show();
 }

Identifywords.createAcknowledgementDivs = function () {
    var i = 0, div;
    for (i = 0; i < Object.keys(Identifywords  .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(Identifywords  .AcknowledgementPageData)[i] + " : " + Object.values(Identifywords  .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.style.marginBottom = "2vh";
    acknowledgementDiv.appendChild(div);
}
Identifywords.showAcknowledgement = function () {
    $("#acknowledgementDiv").show();
}
Identifywords.scoreFunction = function () {
    console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
    if (Utils.mobileDeviceFlag) {
        Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime);
    }
}


