//september 2019
var Tense = {};
var Utils = {};
Utils.path = "";
var questionId = 0,
    startTime = 0,
    scoredMarks = 0,
    totalMarks = 0,
    resId = 0,
    label, scoreFlag = false;
var word = "",
    totalNos = 0,
    type = "sentence";
Utils.mobileDeviceFlag = false;
$(document).ready(function() {
    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('makeRhymeWord');
    } */
    $(".se-pre-con").fadeOut("slow");
    setTimeout(function() {
        Tense.setImageSource();
        Tense.Init();
    }, 100);
});

Tense.setImageSource = function() {
    document.body.style.backgroundImage = Utils.path + "url('images/cover_page.jpg')";
    document.getElementById("queDiv").style.background = Utils.path + "url('images/text_box.png')";
    document.getElementById("home").src = Utils.path + "images/Home.png";
    document.getElementById("logo").src = Utils.path + "images/logo.png";
    document.getElementById("playbutton").src = Utils.path + "images/Play.png";
    document.getElementById("next").src = Utils.path + "images/Next.png";
    document.getElementById("help").src = Utils.path + "images/showHelp.png";
    document.getElementById("sound").src = Utils.path + "images/sound.png";
    document.getElementById("replatbtn").src = Utils.path + "images/replay.png";
    document.getElementById("creditbtn").src = Utils.path + "images/Credits.png";
    document.getElementById("check").src = Utils.path + "images/Check.png";
    if (Tense.hardcodedData.languageFont != null)
        setFontFamilyForLang(Tense.hardcodedData.languageFont);
}
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");
}

Tense.Init = function() {
        document.getElementById("home").style.display = "none";
        document.getElementById("gamePage").style.display = "none";
        document.getElementById("finalPage").style.display = "none";
    }
    //Initialize variables
Tense.initVariables = function() {
    $(".bgStyle").css({ "background-size": "100% 100%" });
   
    //Tense.type="learn";
    Tense.queCount = 0;
    Tense.practice = false;
    Tense.randOpt = 0;
    Tense.helpFlag = false;
}
Tense.playGame = function() {
        Tense.initVariables();
        Tense.playInstruction();
        if (Utils.mobileDeviceFlag)
            Tense.practice = Android.isPractice();
        if (Tense.practice)
            $("#help").hide();
        else {
            $("#help").show();
            document.getElementById("help").setAttribute("helpData", "off");
        }
        document.body.style.backgroundImage = Utils.path + "url('images/background.png')";
        document.getElementById("gamePage").style.display = "block";
        document.getElementById("coverPage").style.display = "none";
        document.getElementById("home").style.display = "block";
        document.getElementById("finalPage").style.display = "none";
        Tense.populateQue();
        Tense.createOptions();
        Tense.showQue();
    }
    // Populate 10 random questions from json
Tense.populateQue = function() {
        var noOfQue = 5;
        Tense.queArray = [];
        for (var i = 0; i < noOfQue; i++) {
            Tense.randomQue = Math.floor(Math.random() * Tense.data.length);
            while (Tense.queArray.includes(Tense.data[Tense.randomQue]))
                Tense.randomQue = Math.floor(Math.random() * Tense.data.length);
            Tense.queArray.push(Tense.data[Tense.randomQue]);
        }
        console.log(Tense.queArray);
    }
    //create three options as past present and future
Tense.createOptions = function() {
    var noOfTense = 3;
    for (var i = 0; i < noOfTense; i++) {
        var optionDiv = document.createElement("div");
        optionDiv.setAttribute("id", "outerDiv" + i);
        optionDiv.setAttribute("class", "style bgStyle nopadding col-sm-3 col-lg-3 col-md-3 col-xs-3 col-sm-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-offset-1");
        document.getElementById('optionsDiv').appendChild(optionDiv);
        //document.getElementById("outerDiv"+i).style.background=Utils.path+"url('images/'+Tense.tensDataArray[i]+'.png')";

        var optionNameDiv = document.createElement("div");
        optionNameDiv.setAttribute("id", "outerNameDiv" + i);
        optionNameDiv.setAttribute("class", "tensNameStyle nopadding col-sm-3 col-lg-3 col-md-3 col-xs-3 col-sm-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-offset-1");
        document.getElementById('optionsNamesDiv').appendChild(optionNameDiv);
        document.getElementById("outerNameDiv" + i).innerHTML = Tense.tensDataArray_h[i];

        var options = document.createElement("div");
        options.setAttribute("id", "option" + i);
        options.setAttribute("class", "style1 Drop nopadding col-sm-12 col-lg-12 col-md-12 col-xs-12");
        document.getElementById("outerDiv" + i).appendChild(options);
        $("#option" + i).data("tensData", Tense.tensDataArray[i]);
        //document.getElementById("option"+i).setAttribute("soundData",Tense.tensDataArray[i]);
    }
    document.getElementById("outerDiv0").style.background = Utils.path + "url('images/Past.png')";
    document.getElementById("outerDiv1").style.background = Utils.path + "url('images/Present.png')";
    document.getElementById("outerDiv2").style.background = Utils.path + "url('images/Future.png')";
    $(".bgStyle").css({ "background-size": "100% 100%" });
}
Tense.showQue = function() //to display Question
    {

        var tensArray = [];
        scoreFlag = false;
        Tense.ansClicked = false;
        Tense.helpFlag = false;
        Tense.attemptCnt = 0; //how many times attempt the que.
        totalMarks = 10;
        $("#check").hide();
        document.getElementById("help").style.pointerEvents = "auto";
        document.getElementById("help").src = Utils.path + "images/showHelp.png";
        document.getElementById("help").setAttribute("helpData", "off");
        d = new Date(); //date object
        startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(); //to get the current date
        tensArray.push(Tense.queArray[Tense.queCount].tense);
        Tense.randOpt = Math.floor(Math.random() * Tense.tensDataArray.length);
        while (tensArray.includes(Tense.tensDataArray[Tense.randOpt]))
            Tense.randOpt = Math.floor(Math.random() * Tense.tensDataArray.length);

        var insideOption = document.createElement("div");
        insideOption.setAttribute("id", "innerOption");
        insideOption.setAttribute("class", "style2 Drag animation nopadding col-sm-5 col-lg-5 col-md-5 col-xs-5 col-sm-offset-0 col-lg-offset-0 col-md-offset-0 col-xs-offset-0");
        document.getElementById("option" + Tense.randOpt).appendChild(insideOption);

        var insideOptionPara = document.createElement("img");
        insideOptionPara.setAttribute("id", "innerOptionPara");
        insideOptionPara.setAttribute("class", "img-responsive nopadding col-sm-12 col-lg-12 col-md-12 col-xs-12");
        document.getElementById("innerOption").appendChild(insideOptionPara);
        document.getElementById("innerOptionPara").src = Utils.path + "images/queMark.png";
        $("#innerOption").addClass('wobble');

        Tense.dragdrop();
        document.getElementById("queDiv").innerHTML = Tense.queArray[Tense.queCount].Sentence;
        questionId = Tense.queArray[Tense.queCount].questionId;
        word = Tense.queArray[Tense.queCount].Sentence;
    }
    //check ans is right or wrong
Tense.checkAns = function(object) {
        scoreFlag = true;
        Tense.ansClicked = true;
        if (Tense.helpFlag)
            Tense.attemptCnt = 0;
        Tense.attemptCnt++;
        totalNos = Tense.data.length;
        document.getElementById("help").style.pointerEvents = "none";
        document.getElementById("innerOption").style.pointerEvents = "none";
        $("#innerOption").removeClass('wobble');
        console.log(Tense.queArray[Tense.queCount].tense);
        if (Tense.Data == Tense.queArray[Tense.queCount].tense) {
            console.log(word, totalNos, type);
            if (Utils.mobileDeviceFlag) {
                Android.addLearntData(word, totalNos, type);
            }
            document.getElementById("innerOptionPara").src = Utils.path + "images/correct.png";
            document.getElementById("audio").src = Utils.path + 'sounds/Instructions/Right_answer__Aditi.mp3';
            document.getElementById("innerOption").style.pointerEvents = "none";
            document.getElementById("sound").style.pointerEvents = "none";
            if (!Tense.helpFlag) {
                if (Tense.attemptCnt == 1) {
                    scoredMarks = 10;
                } else
                    scoredMarks = 5;
                Tense.scoreFunction();
            }
            if (Tense.helpFlag == true) {
                Tense.ansClicked = true;
            } else
                Tense.ansClicked = false;
            $("#check").hide();

            $("#next").addClass("blink_me");
        } else {
            if (Tense.attemptCnt == 2) {
                document.getElementById("audio").src = Utils.path + 'sounds/Instructions/wrong_answer!_Aditi.mp3';
                scoredMarks = 0;
                scoreFlag = true;
                /* 	if(Tense.helpFlag==false)
                	{ */
                $("#check").hide();
                $("#next").addClass("blink_me");
                document.getElementById("innerOption").style.pointerEvents = "none";

            }
            if (Tense.attemptCnt == 1) {
                document.getElementById("audio").src = Utils.path + 'sounds/Instructions/Not_this_one__Aditi.mp3';
                //document.getElementById("innerOption").style.pointerEvents="auto";
            }
            document.getElementById("innerOptionPara").src = Utils.path + "images/wrong.png";
            if (Tense.setTimeToPointerEvent) {
                clearInterval(Tense.setTimeToPointerEvent);
                Tense.setTimeToPointerEvent = null;
            }
            Tense.setTimeToPointerEvent = setTimeout(function() {
                document.getElementById("help").style.pointerEvents = "auto";
                if (Tense.attemptCnt == 1)
                    document.getElementById("innerOption").style.pointerEvents = "auto";
                if (Tense.attemptCnt < 2) {
                    document.getElementById("innerOptionPara").src = Utils.path + "images/queMark.png";
                    $("#innerOption").addClass('wobble');
                }
            }, 1000);
        }

        document.getElementById("audio").play();
        /* 	if(Tense.attemptCnt==2)
        	{
        		document.getElementById("innerOption").style.pointerEvents="none";
        	} */
    }
    //to show next question
Tense.nextQue = function() {
        document.getElementById("sound").style.pointerEvents = "auto";
        Tense.queCount++;
        //Tense.helpFlag=false;
        $("#check").show();
        $("#next").removeClass("blink_me");
        clearInterval(Tense.setTimeToPointerEvent);
        document.getElementById("audio").pause();
        document.getElementById("queDiv").innerHTML = "";
        document.getElementById("option" + Tense.randOpt).innerHTML = "";
        document.getElementById("optionsDiv").innerHTML = "";
        document.getElementById("optionsNamesDiv").innerHTML = "";
        if (scoreFlag == true && Tense.ansClicked) {
            scoredMarks = 0;
            Tense.scoreFunction();
        }

        if (Tense.attemptCnt == 0 && Tense.ansClicked == false) {
            scoredMarks = -1;
            Tense.scoreFunction();
        }
        if (Tense.queCount < 5) {
            Tense.createOptions();
            Tense.showQue();
        } else
            Tense.gameOver();
    }
    //display right ans on click help button
Tense.helpFun = function() {
        Tense.helpFlag = true;
        Tense.ansClicked = true;
        scoreFlag = true;
        document.getElementById("audio").pause();
        $("#innerOption").remove();
        //	$("#check").hide();
        $("#next").addClass("blink_me");

        Tense.attemptCnt++;
        var helpdata = document.getElementById("help").getAttribute("helpData");
        if (helpdata == "off") {
            $("#check").hide();
            document.getElementById("help").src = Utils.path + "images/hideHelp.png";
            document.getElementById("help").setAttribute("helpData", "on");
            for (var i = 0; i < 3; i++) {
                var j = $("#option" + i).data("tensData");
                if (j == Tense.queArray[Tense.queCount].tense) {
                    var insideOption = document.createElement("div");
                    insideOption.setAttribute("id", "innerOption");
                    insideOption.setAttribute("class", "style2 Drag animation nopadding col-sm-5 col-lg-5 col-md-5 col-xs-5 col-sm-offset-0 col-lg-offset-0 col-md-offset-0 col-xs-offset-0");
                    document.getElementById("option" + i).appendChild(insideOption);

                    var insideOptionPara = document.createElement("img");
                    insideOptionPara.setAttribute("id", "innerOptionPara");
                    insideOptionPara.setAttribute("class", "nopadding parastyle col-sm-12 col-lg-12 col-md-12 col-xs-12");
                    document.getElementById("innerOption").appendChild(insideOptionPara);
                    document.getElementById("innerOptionPara").src = Utils.path + "images/correct.png";
                }
            }
            document.getElementById("innerOption").style.pointerEvents = "none";
        } else {
            var tensArray = [];
            document.getElementById("help").src = Utils.path + "images/showHelp.png";
            tensArray.push(Tense.queArray[Tense.queCount].tense);
            Tense.randOpt = Math.floor(Math.random() * Tense.tensDataArray.length);
            while (tensArray.includes(Tense.tensDataArray[Tense.randOpt]))
                Tense.randOpt = Math.floor(Math.random() * Tense.tensDataArray.length);

            document.getElementById("help").setAttribute("helpData", "off");
            var insideOption = document.createElement("div");
            insideOption.setAttribute("id", "innerOption");
            insideOption.setAttribute("class", "style2 Drag animation nopadding col-sm-5 col-lg-5 col-md-5 col-xs-5 col-sm-offset-0 col-lg-offset-0 col-md-offset-0 col-xs-offset-0");
            document.getElementById("option" + Tense.randOpt).appendChild(insideOption);

            var insideOptionPara = document.createElement("img");
            insideOptionPara.setAttribute("id", "innerOptionPara");
            insideOptionPara.setAttribute("class", "img-responsive nopadding col-sm-12 col-lg-12 col-md-12 col-xs-12");
            document.getElementById("innerOption").appendChild(insideOptionPara);
            document.getElementById("innerOptionPara").src = Utils.path + "images/queMark.png";
            $("#innerOption").addClass('wobble');
            Tense.dragdrop();
        }
        document.getElementById("innerOption").style.pointerEvents = "auto";
    }
    //game over
Tense.gameOver = function() {
    document.getElementById("gamePage").style.display = "none";
    document.getElementById("finalPage").style.display = "block";
    document.getElementById("responseBoxDiv").style.display = "none";
    document.getElementById("acknowledgementDiv").innerHTML = "";
    document.getElementById('gameOver').innerHTML = Tense.hardcodedData.message2;
    document.getElementById("audio").pause();
    for (i = 0; i < Object.keys(Tense.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(Tense.AcknowledgementPageData)[i] + " : " + Object.values(Tense.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 = Tense.hardcodedData['createdOn'];
    acknowledgementDiv.appendChild(div);
    document.getElementById("acknowledgementDiv").style.display = "none";
}

Tense.scoreFunction = function() //score Function
    {
        console.log(resId, questionId, scoredMarks, totalMarks, 1, startTime);
        if (Utils.mobileDeviceFlag) {
            Android.addScore(resId, questionId, scoredMarks, totalMarks, 1, startTime);
        }
    }
Tense.returnback = function() {
    if (Tense.ansClicked) {
        scoredMarks = 0;
        Tense.ansClicked = false;
        Tense.scoreFunction();
    }
    location.reload();
}
Tense.showCredits = function() //show credits on click credit button
    {
        document.getElementById("acknowledgementDiv").style.display = "block";
    }
Tense.shuffle = function(alphaArray) //suffle the letterArray
    {
        for (var i = 0; i < alphaArray.length; i++) {
            var j = Math.floor(Math.random() * (alphaArray.length - 1));
            var temp = alphaArray[i];
            alphaArray[i] = alphaArray[j];
            alphaArray[j] = temp;
        }
        return alphaArray;
    }
Tense.playInstruction = function() { //play instruction
        document.getElementById('audio').src = Utils.path + 'sounds/Instructions/' + Tense.hardcodedData.instruction;
        document.getElementById('audio').play();
    }
    /*================================================================ */
Tense.dragdrop = function() {
    $(".drag").draggable({
        create: function(event, ui) {},
        axis: "x",
        containment: "#optionsDiv",
        drag: function(event, ui) {
            $("#innerOption").removeClass('wobble');
            scoreFlag = true;
            Tense.ansClicked = true;
        }
    });
    $(".drop").droppable({
        drop: function(event, ui) {
            $("#innerOption").removeClass('animation');
            Tense.Data = $(this).data("tensData");
            document.getElementById("audio").src = Utils.path + 'sounds/' + Tense.Data + '.mp3';
            document.getElementById("audio").play();
            $(this).append(ui.draggable);
            $(ui.draggable).css({ 'top': 0, 'left': '4.5vw' });
            $("#check").show();
        }
    });
}