//startingDate:16-09-2019
var WordProb = {};
var Utils = {};
Utils.Path = '';
Utils.mobileDeviceFlag = false;
var questionId = 0,
    startTime = 0,
    scoredMarks = 0,
    totalMarks = 10,
    resId = 0,
    level = 1,
    label = '';
WordProb.questionCtr = 0;
WordProb.scoreFlag = false;
WordProb.questionArr = [];
WordProb.attemptFlag = false;
WordProb.firstAttemptFlag = true;
randQuestion = 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("WordProb");  */
    setTimeout(function() {
        WordProb.init();
    }, 100);
});

WordProb.init = function() {
    document.title = Object.values(WordProb.AcknowledgementPageData)[0];
    $("#coverPage").show();
    $("#gamePage").hide();
    $("#endPage").hide();
    $(".header").not("#logo").css("visibility", "hidden");
    document.body.style.backgroundImage = "url(" + Utils.Path + "images/Bg.png";
    document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/" + WordProb.hardCodedData.cover_page;
    WordProb.initializeVariables();
    WordProb.setAllImages();
    WordProb.createAcknowledgementDivs();
    //WordProb.createOptionDivs();
}

WordProb.initializeVariables = function() {
    WordProb.correctCtr = 0;
    WordProb.ansArr = [];
    WordProb.corrArr = [];
    WordProb.wrongArr = [];
    WordProb.primeNumArr = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 71, 73, 79, 83, 89, 97];
    $('#optionDiv').empty();
}
WordProb.setAllImages = function() {
    document.getElementById("homeImage").src = Utils.Path + "images/skin/Home.png";
    document.getElementById("logoImage").src = Utils.Path + "images/skin/logo.png";
    document.getElementById("playImg").src = Utils.Path + "images/skin/Play.png";
    document.getElementById("next").src = Utils.Path + "images/skin/Next.png";
    document.getElementById("responseImg").src = Utils.Path + "images/skin/winEmoji.png";
    document.getElementById("creditImg").src = Utils.Path + "images/skin/credits.png";
    document.getElementById("replayImg").src = Utils.Path + "images/skin/replay.png";
    document.getElementById("gameOver").innerHTML = WordProb.hardCodedData.gameOver;
    //document.getElementById("fish1").src = Utils.Path + "images/Home_button.png";
}


WordProb.playGame = function() {
    document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/BG_Underwater.png";
    $("#coverPage").hide();
    $("#gamePage").show();
    $(".header").css("visibility", "visible");
    WordProb.setQuestion();

}

WordProb.setQuestion = function() {
    var rno = 0,
        num, tempArr = [],
        oddCtr = 0,
        evenCtr = 0,
        d;
    $(".fonts").css("pointer-events", "auto");
    WordProb.questionCtr++;
    d = new Date();
    startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();

    randQuestion = Math.floor(Math.random() * WordProb.questions.length);
    while (WordProb.questionArr.includes(randQuestion))
        randQuestion = Math.floor(Math.random() * WordProb.questions.length);
    WordProb.questionArr.push(randQuestion);
    document.getElementById("questionDiv").innerHTML = WordProb.questions[randQuestion].hindi + " " + WordProb.hardCodedData.questionText2;
    randomNo1 = Math.floor(Math.random() * WordProb.fishImgsLeft.length);
    document.getElementById('fish1').src = Utils.Path + "images/skin/" + WordProb.fishImgsLeft[randomNo1] + ".gif";
    randomNo2 = Math.floor(Math.random() * WordProb.fishImgsRight.length);
    document.getElementById('fish2').src = Utils.Path + "images/skin/" + WordProb.fishImgsRight[randomNo2] + ".gif";
    rno = Math.ceil(Math.random() * 3);
    switch (randQuestion) {
        //natural positive negative numbers.
        case 0:
        case 2:
        case 3:
            {
                for (i = 0; i < rno; i++) {
                    num = Math.floor(Math.random() * 100 + 1);
                    while (WordProb.corrArr.includes(num))
                        num = Math.floor(Math.random() * 100 + 1);
                    WordProb.corrArr.push(num);
                    WordProb.ansArr.push(num);
                }
                for (i = 0; i < (4 - rno); i++) {
                    wrongNum = Math.floor(Math.random() * 100 + 1);
                    while (WordProb.wrongArr.includes(-wrongNum))
                        wrongNum = Math.floor(Math.random() * 100 + 1);
                    if (wrongNum > 0)
                        wrongNum = -wrongNum;
                    WordProb.wrongArr.push(wrongNum);
                    WordProb.ansArr.push(wrongNum);
                }
                if (randQuestion == 3) {
                    tempArr = WordProb.corrArr.slice(0);
                    WordProb.corrArr = WordProb.wrongArr.slice(0);
                    WordProb.wrongArr = tempArr.slice(0);

                }

            }
            break;
            //whole numbers
        case 1:
            {
                for (i = 0; i < rno; i++) {
                    num = Math.floor(Math.random() * 100 + 1);
                    while (WordProb.corrArr.includes(num))
                        num = Math.floor(Math.random() * 100 + 1);
                    WordProb.corrArr.push(num);
                    WordProb.ansArr.push(num);
                }
                for (i = 0; i < (4 - rno); i++) {
                    num1 = Math.floor(Math.random() * 10);
                    num2 = Math.floor(Math.random() * 10);
                    sign = Math.floor(Math.random() * 2);
                    if (sign == 1)
                        wrongNum = num1 + "." + num2;
                    else
                        wrongNum = num1 + "/" + num2;
                    WordProb.wrongArr.push(wrongNum);
                    WordProb.ansArr.push(wrongNum);

                }

            }
            break;
            //decimal numbers
        case 4:
            {
                for (i = 0; i < rno; i++) {
                    num1 = Math.floor(Math.random() * 20);
                    num2 = Math.floor(Math.random() * 10);
                    num = eval(num1 + "." + num2);

                    WordProb.corrArr.push(num);
                    WordProb.ansArr.push(num);
                }
                for (i = 0; i < (4 - rno); i++) {
                    wrongNum = Math.floor(Math.random() * 100);
                    while (WordProb.wrongArr.includes(wrongNum))
                        wrongNum = Math.floor(Math.random() * 100);
                    sign = Math.floor(Math.random() * 2);
                    if (sign)
                        wrongNum = -wrongNum;
                    WordProb.wrongArr.push(wrongNum);
                    WordProb.ansArr.push(wrongNum);
                }
            }
            break;
            //even odd numbers.  	
        case 5:
        case 6:
            {
                for (i = 0; i < 4; i++) {
                    num = Math.floor(Math.random() * 100);
                    while (WordProb.ansArr.includes(num))
                        num = Math.floor(Math.random() * 100);
                    if (num % 2 == 0) {
                        evenCtr++;
                        if (evenCtr == 4) {
                            evenCtr--;
                            i--;
                            continue;
                        } else {
                            WordProb.corrArr.push(num);
                            WordProb.ansArr.push(num);
                        }
                    } else {
                        oddCtr++;
                        if (oddCtr == 4) {
                            oddCtr--;
                            i--;
                            continue;
                        } else {
                            WordProb.wrongArr.push(num);
                            WordProb.ansArr.push(num);
                        }
                    }
                }
                if (randQuestion == 6) {
                    tempArr = WordProb.corrArr.slice(0);
                    WordProb.corrArr = WordProb.wrongArr.slice(0);
                    WordProb.wrongArr = tempArr.slice(0);
                }
            }
            break;
            //prime, composite numbers.
            // case 7: case 8: {
            // 	for (i = 0; i < 4; i++) {
            // 		if (i < rno) {
            // 			randomPrime = Math.floor(Math.random() * WordProb.primeNumArr.length);
            // 			num = WordProb.primeNumArr[randomPrime];
            // 			while (WordProb.corrArr.includes(num)) {
            // 				randomPrime = Math.floor(Math.random() * WordProb.primeNumArr.length);
            // 				num = WordProb.primeNumArr[randomPrime];
            // 			}
            // 			WordProb.corrArr.push(num);
            // 		}
            // 		else {
            // 			num = Math.floor(Math.random() * 100);
            // 			while (WordProb.wrongArr.includes(num) || WordProb.primeNumArr.includes(num))
            // 				num = Math.floor(Math.random() * 100);

            // 			WordProb.wrongArr.push(num);

            // 		}
            // 		WordProb.ansArr.push(num)
            // 	}
            // 	if (randQuestion == 8) {
            // 		tempArr = WordProb.corrArr.slice(0);
            // 		WordProb.corrArr = WordProb.wrongArr.slice(0);
            // 		WordProb.wrongArr = tempArr.slice(0);
            // 	}

            // } break;
        case 7:
            {
                for (i = 0; i < rno; i++) {

                    num1 = Math.floor(Math.random() * 5 + 1);
                    num2 = Math.floor(Math.random() * 20);
                    while (num1 >= num2)
                        num2 = Math.floor(Math.random() * 20);
                    num = (num1 + "/" + num2);

                    WordProb.corrArr.push(num);
                    WordProb.ansArr.push(num);
                }
                for (i = 0; i < (4 - rno); i++) {
                    wrongNum = Math.floor(Math.random() * 100);
                    while (WordProb.wrongArr.includes(wrongNum))
                        wrongNum = Math.floor(Math.random() * 100);
                    sign = Math.floor(Math.random() * 2);
                    if (sign)
                        wrongNum = -wrongNum;
                    WordProb.wrongArr.push(wrongNum);
                    WordProb.ansArr.push(wrongNum);
                }
            }
            break;
        case 10:
            {
                num2Arr = [];
                for (i = 0; i < rno; i++) {
                    num1 = 1;
                    num2 = (Math.floor(Math.random() * 20 - 2) + 2);
                    while (num2Arr.includes(num2))
                        num2 = (Math.floor(Math.random() * 20 - 2) + 2);
                    num = (num1 + "/" + num2);
                    num2Arr.push(num2);
                    WordProb.corrArr.push(num);
                    WordProb.ansArr.push(num);
                }
                for (i = 0; i < (4 - rno); i++) {
                    wrongNum1 = Math.floor(Math.random() * 20 + 2);
                    wrongNum2 = Math.floor(Math.random() * 20 + 2);
                    wrongNum = (wrongNum1 + "/" + wrongNum2);
                    while (WordProb.wrongArr.includes(wrongNum)) {
                        wrongNum1 = Math.floor(Math.random() * 20 + 2);
                        wrongNum2 = Math.floor(Math.random() * 20 + 2);
                        wrongNum = (wrongNum1 + "/" + wrongNum2);
                    }
                    WordProb.wrongArr.push(wrongNum);
                    WordProb.ansArr.push(wrongNum);
                }
            }
            break;
        case 11:
        case 12:
            {
                num2Arr = [];
                corrFlag = false;
                wrongFlag = false;
                for (i = 0; i < 4; i++) {
                    num1 = (Math.floor(Math.random() * 18) + 2);
                    num2 = (Math.floor(Math.random() * 18) + 2);
                    while (num2Arr.includes(num2) || num1 == num2)
                        num2 = (Math.floor(Math.random() * 18) + 2);
                    num = (num1 + "/" + num2);
                    num2Arr.push(num2);
                    if (num1 < num2) {
                        corrFlag = true;
                        WordProb.corrArr.push(num);
                    } else {
                        WordProb.wrongArr.push(num);
                        wrongFlag = true;
                    }
                    WordProb.ansArr.push(num);
                    if (i == 3) {
                        if (corrFlag == false || wrongFlag == false || (WordProb.ansArr.length < 4)) {
                            corrFlag = false;
                            wrongFlag = false;
                            num2Arr = [];
                            WordProb.corrArr = [];
                            WordProb.wrongArr = [];
                            WordProb.ansArr = [];
                            i = 0;
                            continue;
                        }
                    }
                }
                if (randQuestion == 12) {
                    tempArr = WordProb.corrArr.slice(0);
                    WordProb.corrArr = WordProb.wrongArr.slice(0);
                    WordProb.wrongArr = tempArr.slice(0);
                }
            }
            break;
    }
    for (var i = 1; i <= 4; i++) {
        WordProb.createBubbleDivs(optionDiv, 1, i, WordProb.ansArr[i - 1]);
        WordProb.animateBubblesTop("div" + i);
    }
}


WordProb.createBubbleDivs = function(parentDiv, numberOfDivs, idNo, data) {
        var bubblesDiv, letter;
        for (var i = 1; i <= numberOfDivs; i++) {
            bubblesDiv = document.createElement("div");
            bubblesDiv.setAttribute("id", "div" + idNo);
            bubblesDiv.setAttribute("class", "bubbleClass bubble");
            bubblesDiv.setAttribute("onclick", "WordProb.checkAnswer(this.id);");
            bubblesDiv.style.backgroundImage = Utils.Path + 'url(images/skin/bubble-_Blue.png)';
            parentDiv.appendChild(bubblesDiv);

            letter = document.createElement("p");
            letter.setAttribute("id", "letter" + idNo);
            letter.setAttribute("class", "fonts  text-center");
            letter.setAttribute("data", data);
            letter.setAttribute('data-toggle', 'modal');
            letter.setAttribute('data-target', '#myModal');
            letter.innerHTML = data;
            bubblesDiv.appendChild(letter);
        }
    }
    //move bubbles upward
WordProb.animateBubblesTop = function(div) {
    var speed = 0;
    speed = Math.floor((Math.random() * 6000) + 10000);
    $("#" + div).css({ 'top': $('#optionDiv').height() + 200 });
    $("#" + div).animate({
        'top': '-=' + ($(document).height() + ($("#" + div).outerHeight()))
    }, speed, function() {
        $(this).stop();
        id = $(this).prop('id');
        WordProb.animateBubblesTop(id);
    });
}




WordProb.checkAnswer = function(divId) {
    var number;
    WordProb.attemptFlag = true;
    //divId = (document.getElementById(divId).children[0]);
    if (randQuestion < 7)
        number = eval(document.getElementById(divId).children[0].innerHTML);
    else
        number = (document.getElementById(divId).children[0].innerHTML);
    if (WordProb.corrArr.includes(number)) {
        document.getElementById(divId).style.pointerEvents = "none";
        document.getElementById("playSound").src = Utils.Path + "sounds/instructions/thats_right.mp3";
        document.getElementById(divId).children[0].style.color = "green";
        $('#' + divId).stop();
        WordProb.correctCtr++;
        if (WordProb.correctCtr == WordProb.corrArr.length) {
            $("#correctWrongModal").show();
            if (WordProb.firstAttemptFlag)
                scoredMarks = 10;
            WordProb.nextQuestionTimer = setTimeout(function() {
                $("#correctWrongModal").hide();
                WordProb.nextQuestion();
            }, 3000);
        }
    } else {
        WordProb.firstAttemptFlag = false;
        document.getElementById("playSound").src = Utils.Path + "sounds/instructions/wrongSoundAksharkhadi.mp3";
        document.getElementById(divId).children[0].style.color = "red";
        var id = $('#' + divId).prop('id');
        //$('#' + id).addclass("shake")
        document.getElementById(id).classList.add('shake');
        scoredMarks = 0;
        setTimeout(function() {
            document.getElementById(divId).children[0].style.color = "rgb(221,19,123)";
            document.getElementById(id).classList.remove('shake');
        }, 2000);
    }
    document.getElementById("playSound").play();
}

WordProb.onNextClick = function() {
    if (!(WordProb.attemptFlag))
        scoredMarks = -1;
    WordProb.nextQuestion();
}

WordProb.nextQuestion = function() {
    document.getElementById("playSound").pause();
    clearTimeout(WordProb.nextQuestionTimer);
    $(".options").css("color", "black");
    console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
    if (Utils.mobileDeviceFlag)
        Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);

    WordProb.scoreFlag = false;
    WordProb.attemptFlag = false;
    WordProb.firstAttemptFlag = true;
    if (WordProb.questionCtr < 5) {

        WordProb.initializeVariables();
        WordProb.setQuestion();
    } else {
        $("#gamePage").hide();
        $("#endPage").show();
    }
}
WordProb.createAcknowledgementDivs = function() {
    var i = 0,
        div;
    for (i = 0; i < Object.keys(WordProb.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(WordProb.AcknowledgementPageData)[i] + " : " + Object.values(WordProb.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.innerHTML = WordProb.hardCodedData.createdOn;
    div.style.marginBottom = "2vh";
    acknowledgementDiv.appendChild(div);
}

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

}

WordProb.playAgain = function() {
    WordProb.questionArr = [];
    $("#acknowledgementDiv").hide();
    $("#endPage").hide();
    $("#gamePage").show();
    WordProb.questionCtr = 0;
    WordProb.initializeVariables();
    WordProb.setQuestion();
}




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