//24-09-2018

var RabbitGame = {};
var Utils = {};
Utils.Path = '';
Utils.mobileDeviceFlag = false;
var questionId = 0,
    startTime = 0,
    scoredMarks = 0,
    totalMarks = 10,
    resId = 0,
    label = "";
var firstAttempt = true,
    finalMarks = 0;
RabbitGame.init = function () {
    document.title = Object.values(RabbitGame.AcknowledgementPageData)[0];
    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("Daud"); */

    document.body.style.backgroundImage = "url(" + Utils.Path + "images/" + RabbitGame.hardCodedData.cover_page + ")";
    RabbitGame.setVariables();
    RabbitGame.setImages();
}

RabbitGame.setImages = function () {
    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("soundImage").src = Utils.Path + "images/sound.png";
    document.getElementById("replayImg").src = Utils.Path + "images/replay.png";
    document.getElementById("creditImg").src = Utils.Path + "images/credit.png";
    document.getElementById("gameOver").innerHTML = RabbitGame.hardCodedData.gameOver;
}
//set all variable
RabbitGame.setVariables = function () {
    RabbitGame.tortoisTimeInterval = 0;
    RabbitGame.tortoisPos = 0;
    RabbitGame.rabbitTimeInterval = 0;
    RabbitGame.rabbitPos = 0;
    RabbitGame.checkAnsValue = 0;
    RabbitGame.queCount = 1;
    RabbitGame.optionsArray = [];
    RabbitGame.loadmetaDataFlag = false;
    RabbitGame.playCarrotFlag = true;
    RabbitGame.playSoundTimeOut = null;
    RabbitGame.homeFlag = true;
}

//play click
RabbitGame.playgame = function () {
    RabbitGame.homeFlag = true;
    document.body.style.backgroundImage = "url(" + Utils.Path + "images/" + RabbitGame.hardCodedData.bg + ")";
    var BGMP3 = Utils.Path + "sounds/" + RabbitGame.BGSound;
    $('#bgSound').prop('src', BGMP3);
    document.getElementById("bgSound").play();
    document.getElementById("startPage").style.display = "none";
    document.getElementById("endGame").style.display = "none";

    document.getElementById("home").style.display = "block";
    document.getElementById("gamePage").style.display = "block";
    document.getElementById("tortoisAnimationDiv").style.display = "block";
    document.getElementById("rabbitAnimationDiv").style.display = "block";
    document.getElementById("destinationImg").src = Utils.Path + "images/destination.png";
    document.getElementById("destination").style.display = "block";
    document.getElementById("soundDiv").style.display = "block";
    document.getElementById("rabbitAnimationImg").src = Utils.Path + "images/rabbitStart.png";
    document.getElementById("tortoisAnimationImg").src = Utils.Path + "images/Tortois-walk.gif";
    RabbitGame.startGame();
    RabbitGame.animateTortois();


}

//Tortois animation
RabbitGame.animateTortois = function () {
    if (RabbitGame.tortoisTimeInterval) {
        clearTimeout(RabbitGame.tortoisTimeInterval);
        RabbitGame.tortoisTimeInterval = null;
    }
    RabbitGame.tortoisTimeInterval = setInterval(function () {
        RabbitGame.tortoisPos = RabbitGame.tortoisPos + 0.3;
        document.getElementById("tortoisAnimationDiv").style.left = RabbitGame.tortoisPos + "%";
        if (RabbitGame.tortoisPos >= 85) {
            clearInterval(RabbitGame.tortoisTimeInterval);
            RabbitGame.youLose();
        }
    }, 200);
}

//set carrot for count
RabbitGame.startGame = function () {
    var startSound;
    var d = new Date();
    $('.onlyImg').css({ pointerEvents: "none" });
    startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
    RabbitGame.createOption();
    RabbitGame.setOptionImages();
    startSound = Utils.Path + "sounds/" + RabbitGame.giveCarrotSound;
    RabbitGame.loadmetaDataFlag = true;
    RabbitGame.getDuration(startSound);
}

RabbitGame.nextQue = function () {
    var startSound;
    var d = new Date();
    $('.onlyImg').css({ pointerEvents: "none" });
    startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
    RabbitGame.createOption();
    RabbitGame.setOptionImages();
    RabbitGame.playOneTwoThreeSound();
}


//create options values
RabbitGame.createOption = function () {
    var i = 0,
        num;
    for (i = 0; i <= 3; i++) {
        num = (Math.floor(Math.random() * 7) + 1);
        if (RabbitGame.optionsArray.includes(num)) {
            i--;
            continue;
        } else
            RabbitGame.optionsArray[i] = num;
    }
}
//set option images
RabbitGame.setOptionImages = function () {
    var i = 0,
        j = 0,
        value1 = 0,
        value = 0,
        temp = [],
        num, divId, innrDiv, img;
    for (i = 0; i < 3; i++) {
        num = (Math.floor(Math.random() * 3) + 1);
        console.log(num);
        if (temp.includes(num)) {
            i--;
            continue;
        } else {
            temp[i] = num;
            divId = document.getElementById("optionImg" + num);
            divId.setAttribute("dataValue", RabbitGame.optionsArray[i]);
            for (j = 0; j < RabbitGame.optionsArray[i]; j++) {
                innrDiv = document.createElement('div');
                innrDiv.setAttribute("class", "optionAllImg img-responsive nopadding");
                img = document.createElement('img');
                img.setAttribute("class", "optionImageClass img-responsive nopadding");
                img.setAttribute("src", Utils.Path + "images/carrot.png");
                divId.appendChild(innrDiv);
                innrDiv.appendChild(img);
            }
        }
    }
}

//get answer
RabbitGame.getAnswer = function (answerID) {
    var c;
    //document.getElementsByClssName("onlyImg").style.pointerEvents = "none";

    RabbitGame.checkAnsValue = document.getElementById(answerID).getAttribute("dataValue");
    RabbitGame.checkAnswer(answerID);
}

// check answer onclick of ok 
RabbitGame.checkAnswer = function (answerID) {
    $('.onlyImg').css({ pointerEvents: "none" });
    var checkPos = 0;
    questionId = RabbitGame.optionsArray[0];
    if (RabbitGame.optionsArray[0] == RabbitGame.checkAnsValue) {
        $('#playSound').prop('src', Utils.Path + "sounds/Rabbit_Running.mp3");
        document.getElementById("playSound").play();
        finalMarks = finalMarks + 10;
        scoredMarks = 10;
        RabbitGame.scoreFun();
        document.getElementById("rabbitAnimationImg").src = Utils.Path + "images/runRabbit.gif";
        if (RabbitGame.rabbitTimeInterval) {
            clearTimeout(RabbitGame.rabbitTimeInterval);
            RabbitGame.rabbitTimeInterval = null;
        }
        RabbitGame.rabbitTimeInterval = setInterval(function () {
            RabbitGame.rabbitPos = RabbitGame.rabbitPos + 1.5;
            checkPos = checkPos + 1;
            document.getElementById("rabbitAnimationDiv").style.left = RabbitGame.rabbitPos + "%";

            if (checkPos == 10) {
                clearInterval(RabbitGame.rabbitTimeInterval);
                //document.getElementsByClssName("onlyImg").style.pointerEvents = "auto";
                $('.onlyImg').css({ pointerEvents: "auto" });
                document.getElementById("rabbitAnimationImg").src = Utils.Path + "images/standRabbit.gif";
                RabbitGame.reset();
            }
            if (RabbitGame.rabbitPos >= 85) {
                RabbitGame.youWin();
            }
        }, 300);



    } else {
        $("#" + answerID).addClass('animated shake');
        firstAttempt = false;
        setTimeout(function () {
            RabbitGame.scoreFun();
            $("#" + answerID).removeClass('animated shake');
            RabbitGame.reset();

        }, 700);
    }
}

//reset function
RabbitGame.reset = function () {
    firstAttempt = true;
    scoredMarks = 0;
    RabbitGame.playCarrotFlag = true;
    RabbitGame.optionsArray = [];
    $(".optionAllImg").remove();
    RabbitGame.queCount++;
    if (RabbitGame.rabbitPos <= 76) {
        RabbitGame.nextQue();
    } else {
        document.getElementById("optionDiv").style.display = "none";
    }
}

//after win
RabbitGame.youWin = function () {
    scoredMarks = 100;
    questionId = 0;
    totalMarks = 100;
    RabbitGame.scoreFun();
    clearInterval(RabbitGame.tortoisTimeInterval);
    clearInterval(RabbitGame.rabbitTimeInterval);
    //document.getElementById("result").innerHTML=RabbitGame.hardCodedData.you_win;
    //document.getElementById("endText").innerHTML=RabbitGame.hardCodedData.the_end;
    document.getElementById("endGame").style.display = "block";
    document.getElementById("gamePage").style.display = "none";
    document.getElementById("soundDiv").style.display = "none";
    document.getElementById("rabbitAnimationImg").src = Utils.Path + "images/rabbitWin.gif";
    document.getElementById("tortoisAnimationImg").src = Utils.Path + "images/Tortoi_sad.gif";
    document.getElementById("rabbitAnimationDiv").style.left = "86%";
    RabbitGame.AcknowledgementPage();
}
//after loss
RabbitGame.youLose = function () {
    scoredMarks = 0;
    totalMarks = 100;
    RabbitGame.scoreFun();
    clearInterval(RabbitGame.tortoisTimeInterval);
    clearInterval(RabbitGame.rabbitTimeInterval);
    //document.getElementById("result").innerHTML=RabbitGame.hardCodedData.you_loss;
    //document.getElementById("endText").innerHTML=RabbitGame.hardCodedData.the_end;
    document.getElementById("endGame").style.display = "block";
    document.getElementById("gamePage").style.display = "none";
    document.getElementById("soundDiv").style.display = "none";
    document.getElementById("tortoisAnimationImg").src = Utils.Path + "images/Tortois_happy.gif";
    document.getElementById("rabbitAnimationImg").src = Utils.Path + "images/rabbit_sad.gif";
    document.getElementById("tortoisAnimationDiv").style.left = "90%";
    RabbitGame.AcknowledgementPage();
}

//get duration and play sound
RabbitGame.getDuration = function (soundFile) {
    var duration = 0;
    $('#playSound').prop('src', soundFile);
    if (RabbitGame.loadmetaDataFlag) {
        $('#playSound').on("loadedmetadata", function () {
            duration = $('#playSound').prop('duration');
            RabbitGame.loadmetaDataFlag = false;
            $('#playSound').unbind("loadedmetadata");
            if (RabbitGame.playCarrotFlag == true) {
                RabbitGame.playCarrotFlag = false;
                RabbitGame.playGiveCarrotSound(duration);
            } else {
                $('.onlyImg').css({ pointerEvents: "auto" });
                RabbitGame.playSound();
            }
        });
    } else
        $('#playSound').unbind("loadedmetadata");
}

//paly sound
RabbitGame.playSound = function () {
    document.getElementById("playSound").play();

}
RabbitGame.playGiveCarrotSound = function (duration) {
    var soundFile;
    document.getElementById("playSound").play();
    if (RabbitGame.playSoundTimeOut) {
        clearTimeout(RabbitGame.playSoundTimeOut);
        RabbitGame.playSoundTimeOut = null;
    }
    RabbitGame.playSoundTimeOut = setTimeout(function () {

        RabbitGame.playOneTwoThreeSound();
    }, duration * 1000);
}

//one two three sound
RabbitGame.playOneTwoThreeSound = function () {
    var soundFile;
    soundFile = Utils.Path + "sounds/" + RabbitGame.oneTwoThreeSounds[(RabbitGame.optionsArray[0]) - 1];
    $('#playSound').prop('src', soundFile);
    $('.onlyImg').css({ pointerEvents: "auto" });
    RabbitGame.loadmetaDataFlag = true;
    RabbitGame.playSound();
}

//Add_score function 
RabbitGame.scoreFun = function () {
    questionId = RabbitGame.optionsArray[0];
    if (Utils.mobileDeviceFlag)
        Android.addScore(resId, questionId, scoredMarks, totalMarks, 1, startTime, label);
    else
        console.log(resId, questionId, scoredMarks, totalMarks, 1, startTime, label);
}

//Acknowledgement page
RabbitGame.AcknowledgementPage = function () {
    var i = 0,
        div;
    RabbitGame.homeFlag = false;
    // document.getElementById("acknowledgementDiv").style.display = "block";
    acknowledgementDiv.innerHTML = "";
    //gameOver.innerHTML="GAME OVER";
    console.log(Object.keys(RabbitGame.AcknowledgementPageData).length)
    for (i = 0; i < Object.keys(RabbitGame.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(RabbitGame.AcknowledgementPageData)[i] + " : " + Object.values(RabbitGame.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 = RabbitGame.hardCodedData['createdOn'];
    acknowledgementDiv.appendChild(div);
}
RabbitGame.showAcknowledgement = function () {
    $("#acknowledgementDiv").css("visibility", "visible");
}

RabbitGame.playAgain = function () {
    $("#acknowledgementDiv").css("visibility", "hidden");

    firstAttempt = true;
    scoredMarks = 0;
    totalMarks = 10;
    RabbitGame.playCarrotFlag = true;
    RabbitGame.optionsArray = [];
    $(".optionAllImg").remove();
    RabbitGame.setVariables();
    document.getElementById("rabbitAnimationDiv").style.left = RabbitGame.rabbitPos + "%";
    RabbitGame.playgame();
}
//
RabbitGame.homeClickFun = function () {
    location.reload();
}