var Adverbs = {};
var Utils = {};
Utils.Path = "";
var questionId = 0, startTime = 0, scoredMarks = 0, totalMarks = 10, resId = 0, label = '', level = 1;

$(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 ((navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) && !(window.location.href.includes('http')))
        Utils.mobileDeviceFlag = true;
    else
        Utils.mobileDeviceFlag = false;
    setTimeout(function () {
        Adverbs.init();
    });
}, 200);

Adverbs.init = function () {
    $("#home").hide();
    $("#gamePage").hide();
    $("#finalPage").hide();
    document.title = Adverbs.hardCodedData.nameOfGame;
    //document.getElementById("playImage").classList.add('pulse');
    Adverbs.setImages();
    Adverbs.initVariables();
    Adverbs.createAcknowledgementDivs();
}

Adverbs.setImages = function () {
    document.body.style.backgroundImage = Utils.Path + "url('images/skin/BG.png')";
    document.getElementById("home").src = Utils.Path + "images/skin/Home.png";
    document.getElementById("logo").src = Utils.Path + "images/skin/logo.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("hintImage").src = Utils.Path + "images/skin/help.png";
    document.getElementById("signalImg1").src = Utils.Path + "images/skin/green light.png";
    document.getElementById("signalImg2").src = Utils.Path + "images/skin/red light.png";
    document.getElementById("playAgainBtn").src = Utils.Path + "images/skin/replay.png";
    document.getElementById("creditbtn").src = Utils.Path + "images/skin/Credits.png";
    document.getElementById("queOuterDiv").style.backgroundImage = "url('images/skin/game machine.png')";
    document.getElementById("queText").style.backgroundImage = "url('images/skin/Q box.png')";
    document.getElementById("gameOverTextDiv").innerHTML = Adverbs.hardCodedData.gameOver;
}

Adverbs.initVariables = function () {
    Adverbs.questionCnt = 0;
    Adverbs.queArray = [];
    Adverbs.randomNoArray = [];
    Adverbs.splitQuestion = [];
    Adverbs.attemptFlag = false;
    Adverbs.clickedOptionData = '';
    Adverbs.hintTimer = null;
    Adverbs.correctAnsTimer = null;
    Adverbs.wrongQuestionTimer = null;
}

Adverbs.play = function () {
    document.body.style.backgroundImage = Utils.Path + "url('images/skin/BG.png')";
    $("#home").show();
    $("#gamePage").show();
    $("#playImage").hide();
    $("#finalPage").hide();
    //document.getElementById("playImage").classList.remove('pulse');
    d = new Date();
    startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();

    if (Adverbs.questionCnt != 5) {   
        Adverbs.showQuestion();
    }
    else {
        Adverbs.gameOver();
    }
}

Adverbs.showQuestion = function ()      //Show Question and Answer.
{
    randomNum = Math.floor(Math.random() * Adverbs.dataArray.length);
    while (Adverbs.randomNoArray.includes(randomNum))
        randomNum = Math.floor(Math.random() * Adverbs.dataArray.length);
    Adverbs.randomNoArray.push(randomNum);
    Adverbs.queArray.push(Adverbs.dataArray[randomNum]);

    Adverbs.randomQue = Adverbs.queArray[Adverbs.questionCnt];
    Adverbs.splitQuestion = Adverbs.randomQue.questionText.split(" ");
    for(i=0; i<Adverbs.splitQuestion.length; i++){
        var span = document.createElement("span");
		span.setAttribute("id", "wordsdiv" + (i + 1));
		span.setAttribute("class", "nopadding text-center ");
		document.getElementById('engQueText').appendChild(span);
		span.innerHTML = Adverbs.splitQuestion[i];
    }  

    var img = document.createElement('img');
    $(img).prop('id', 'ballImg');
    $(img).prop('src', Utils.Path + "images/skin/ball.png");
    $('#ballDiv').append(img);
    document.getElementById("girlImg").src = Utils.Path + "images/skin/eye-blinking.gif";
    document.getElementById("signalImg1").src = Utils.Path + "images/skin/green light.png";
    document.getElementById("optionDiv1").style.backgroundImage = Utils.Path + "url('images/skin/opation box.png')";
    document.getElementById("optionDiv2").style.backgroundImage = Utils.Path + "url('images/skin/opation box.png')";
    $("#hindiQueText").css('visibility', 'hidden');
    $("#hindiQueText").text(Adverbs.randomQue.hindiText);
    shuffle(Adverbs.randomQue.optionArray);
    for(i=0; i<Adverbs.randomQue.optionArray.length; i++){    
        $("#optionDiv" + (i + 1)).text(Adverbs.randomQue.optionArray[i]);
        //$("#optionDiv" + (i + 1)).css('border', '1px solid black');
    }   
}

Adverbs.showHint = function()
{
    $("#hindiQueText").css('visibility', 'visible');
    if (Adverbs.hintTimer) {
        clearTimeout(Adverbs.hintTimer);
        Adverbs.hintTimer = null;
    }
    Adverbs.hintTimer = setTimeout(function(){
        $("#hindiQueText").css('visibility', 'hidden');
    }, 3000);
}

Adverbs.selectOption = function (clickedOptId) 
{    
    Adverbs.attemptFlag = true;
    Adverbs.clickedOptionData = $("#" + clickedOptId).text();
    //console.log(Adverbs.clickedOptionData);  
    label =  Adverbs.clickedOptionData;

    for(i=0; i<=Adverbs.splitQuestion.length; i++){
        if(Adverbs.splitQuestion[i].includes('________')){
            $("#wordsdiv"+(i+1)).text(Adverbs.clickedOptionData);
            break;
        }
    }
    
    clearTimeout(Adverbs.correctAnsTimer);
    Adverbs.correctAnsTimer = null;
    clearTimeout(Adverbs.wrongQuestionTimer);
    Adverbs.wrongQuestionTimer = null;

    if (Adverbs.clickedOptionData == Adverbs.randomQue.correctAns) {        //For correct answer
        scoredMarks = 10;
        $("#wordsdiv"+(i+1)).css('color', 'green');
        $("#wordsdiv"+(i+1)).css('border-bottom', 'dashed green');
        $("#hintBtn").css('pointer-events', 'none');
        $("#soundBtn").css("pointer-events", "none");
        $("#ansOptionDiv").css("pointer-events", "none");
        document.getElementById("signalImg1").src = Utils.Path + "images/skin/green-light-blinking.gif";
        document.getElementById("girlImg").src = Utils.Path + "images/skin/happy-girl-animation.gif";
        document.getElementById("playSound").src = Utils.Path + "sounds/instructions/thats_right.mp3";
        document.getElementById("playSound").play();
        document.getElementById("engQueText").classList.add('flash');
        $("#wordsdiv"+(i+1)).css('background-color','yellow');

        removeBallTimer = setTimeout(function(){
            document.getElementById("ballDiv").innerHTML = '';      //To remove ball image
        }, 500);

        if (Adverbs.correctAnsTimer) {
            clearTimeout(Adverbs.correctAnsTimer);
            Adverbs.correctAnsTimer = null;
        }
        Adverbs.correctAnsTimer = setTimeout(function(){
            $("#wordsdiv"+(i+1)).css('background-color','white');
            document.getElementById("nextBtn").classList.add('blink_me');
            document.getElementById("engQueText").classList.remove('flash');
            document.getElementById("signalImg1").src = Utils.Path + "images/skin/green light.png";
        },2500);
    }
    else {
        scoredMarks = 0;
        $("#wordsdiv"+(i+1)).css('color', 'red');
        $("#wordsdiv"+(i+1)).css('text-decoration', 'line-through');
        $("#soundBtn").css("pointer-events", "none");
        document.getElementById("signalImg2").src = Utils.Path + "images/skin/red-light-blinking.gif";
        document.getElementById("playSound").src = Utils.Path + "sounds/instructions/BuzzerWrongAns.mp3";
        document.getElementById("playSound").play();
        if (Adverbs.wrongQuestionTimer) {
            clearTimeout(Adverbs.wrongQuestionTimer);
            Adverbs.wrongQuestionTimer = null;
        }
        Adverbs.wrongQuestionTimer = setTimeout(function () {
            $("#wordsdiv"+(i+1)).text('________');
            $("#wordsdiv"+(i+1)).css('color', 'black');
            $("#soundBtn").css("pointer-events", "auto");
            $("#wordsdiv"+(i+1)).css('text-decoration', 'none');
            document.getElementById("signalImg2").src = Utils.Path + "images/skin/red light.png";
        },500);
    }
}


Adverbs.callNext = function () {
    if (!Adverbs.attemptFlag){
        scoredMarks = -1;
        label = 'null';
    }
    console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
    if (Utils.mobileDeviceFlag)
        Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);

    document.getElementById("nextBtn").classList.remove('blink_me');
    $("#ansOptionDiv").css("pointer-events", "auto");
    $("#soundBtn").css("pointer-events", "auto");
    $("#hintBtn").css('pointer-events', 'auto');
    document.getElementById("engQueText").innerHTML = '';
    for(i=0; i<Adverbs.randomQue.optionArray.length; i++)
        $("#optionDiv"+ (i+1)).text("");
    
    clearTimeout(Adverbs.correctAnsTimer);
    Adverbs.correctAnsTimer = null;
    Adverbs.attemptFlag = false;
    Adverbs.questionCnt++;
    Adverbs.play();
}

Adverbs.gameOver = function () {
    $("#acknowledgementDiv").hide();
    $("#gamePage").hide();
    $("#finalPage").show();
    //Adverbs.questionCnt = 0;
}

Adverbs.playAgain = function () {
    $("#acknowledgementDiv").hide();
    $("#finalPage").hide();
    $("#gamePage").show();
    document.getElementById("engQueText").classList.remove('flash');
    clearTimeout(Adverbs.correctAnsTimer);
    Adverbs.correctAnsTimer = null;
    clearTimeout(Adverbs.wrongQuestionTimer);
    Adverbs.wrongQuestionTimer = null;
    Adverbs.initVariables();
    Adverbs.play();
}

Adverbs.callSound = function () {
    document.getElementById("playSound").src = Utils.Path + "sounds/instructions/chooseOption_Eng.mp3";
    document.getElementById("playSound").play();
}

Adverbs.callHome = function () {
    if (Adverbs.attemptFlag) {
        console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
        if (Utils.mobileDeviceFlag)
            Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
    }
    location.reload();
}

Adverbs.createAcknowledgementDivs = function () {
    var i = 0, div;
    for (i = 0; i < Object.keys(Adverbs.AcknowledgementPageData).length; i++) {
        div = document.createElement('div');
        div.className = "nopadding fontClass firstLetter col-xs-12 col-sm-12 col-md-12 col-lg-12";
        div.innerHTML = Object.keys(Adverbs.AcknowledgementPageData)[i] + " : " + Object.values(Adverbs.AcknowledgementPageData)[i];
        acknowledgementDiv.appendChild(div);
    }
    div = document.createElement('div');
    div.className = "nopadding fontClass firstLetter col-xs-12 col-sm-12 col-md-12 col-lg-12";
    div.innerHTML = Adverbs.hardCodedData.createdOn;
    div.style.marginBottom = "2vh";
    acknowledgementDiv.appendChild(div);
}

Adverbs.showAcknowledgement = function () {
    $("#acknowledgementDiv").show();
}

function shuffle(randomArray) {
    var arrayLength = randomArray.length;
    var temp, random_index;
    while (arrayLength > 0) {
        random_index = Math.floor(Math.random() * arrayLength);
        arrayLength--;
        temp = randomArray[arrayLength];
        randomArray[arrayLength] = randomArray[random_index];
        randomArray[random_index] = temp;
    }
    return (randomArray);
}