var antonyms = {}; // namespace name
var Utils = {};
Utils.path = '';
Utils.mobileDeviceFlag = false;
var questionId = 0,
    startTime = 0,
    scoredMarks = 0,
    totalMarks = 0,
    resId = 0,
    label = "",
    level = 1;
var scoreFlag = false,
    helpFlag = 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;
    $(".se-pre-con").fadeOut("slow");
    setTimeout(function() {
        antonyms.setimage();
    }, 100);
    if (antonyms.hardcodedData.languageFont != null)
        setFontFamilyForLang(antonyms.hardcodedData.languageFont);
}, 200);
//set a font
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");
}
antonyms.globalvariables = function() {
        antonyms.QuetionArray = []; //to store the questions
        antonyms.flag = false; //used to call home
        antonyms.QueCounter = 0; //question counter 
        antonyms.timeout;
        antonyms.getDataFlag = false; //used for get data 
        antonyms.rightAnsCount = 0; //used to count right answer
        antonyms.optionsArray = []; //used to store options
        antonyms.Counter = 0; //used to display next question
        antonyms.selectdWord;
        antonyms.practice = false;
        antonyms.Attempt = 0;
        antonyms.helpClick = true;
        antonyms.helpCounter = 0;
        antonyms.correctWordArray = [];
    }
    //set the all images
antonyms.setimage = function() {
    document.body.style.backgroundImage = "url(" + Utils.path + antonyms.coverImage + ")";
    $("#playbtnid").prop("src", Utils.path + "images/Play.png");
    $("#homeid").prop("src", Utils.path + "images/Home.png");
    $("#logoid").prop("src", Utils.path + "images/logo.png");
    $("#bnext").prop("src", Utils.path + "images/Next.png");
    $("#helpBtn").prop("src", Utils.path + "images/Show-Help.png");
    $("#checkBtn").prop("src", Utils.path + "images/Done.png");
    $("#soundBtn").prop("src", Utils.path + "images/Sound.png");
}
antonyms.gamePage = function() {
        antonyms.globalvariables();
        if (Utils.mobileDeviceFlag == true)
            antonyms.practice = Android.isPractice();
        document.getElementById("acknowledgementDiv").innerHTML = "";
        document.getElementById('questions').innerHTML = "";
        document.getElementById('ansid').innerHTML = "";
        document.getElementById("coverpage").style.display = "none";
        document.getElementById("endpage").style.display = "none";
        document.getElementById("gamepage").style.display = "block";
        document.getElementById("homeid").style.visibility = "visible";
        document.body.style.backgroundImage = Utils.path + "url('images/BG.jpg')";
        if (antonyms.practice == true)
            document.getElementById("helpBtn").style.visibility = "hidden";
        else
            document.getElementById("helpBtn").style.visibility = "visible";
        antonyms.playInstructions();
        $("#helpBtn").prop("src", Utils.path + "images/Show-Help.png");
        antonyms.populate();
    }
    //populate questions from json
antonyms.populate = function() {
        for (var j = 0; j < 20; j++) {
            var randomNumber = Math.floor(Math.random() * antonyms.data.length);
            while (antonyms.QuetionArray.includes(antonyms.data[randomNumber])) {
                randomNumber = Math.floor(Math.random() * antonyms.data.length);
            }
            antonyms.QuetionArray.push(antonyms.data[randomNumber]);
        }
        shuffleArray(antonyms.QuetionArray);
        antonyms.displayQuestions();
    }
    //suffle the array
function shuffleArray(array) {
    var i, j, temp;
    for (i = array.length - 1; i > 0; i--) {
        j = Math.floor(Math.random() * (i + 1));
        temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
    return array;
}
//used to display Question
antonyms.displayQuestions = function() {
        $("#checkBtn").css("visibility", "hidden");
        $("#answerImg").prop("src", Utils.path + "images/emoticons.png");
        scoreFlag = false;
        totalMarks = 4;
        var droppableCounter = 0;
        d = new Date(); //date object
        startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(); //to get the current date
        for (var i = 0; i < 4; i++) {
            questionDiv = document.createElement("div");
            questionDiv.setAttribute("id", "quediv" + (i + 1));
            questionDiv.setAttribute("class", " nopadding quetions col-sm-6 col-lg-6 col-md-6 col-xs-6");
            document.getElementById('questions').appendChild(questionDiv);
            document.getElementById("quediv" + (i + 1)).style.backgroundImage = Utils.path + "url('images/Small-Band.png')";
            for (var j = 0; j < 3; j++) {
                questionspan = document.createElement("span");
                questionspan.setAttribute("id", "quespan" + (i) + (j + 1));
                questionspan.setAttribute("class", " nopadding");
                questionDiv.appendChild(questionspan);
                if (j == 0)
                    questionspan.innerHTML = antonyms.QuetionArray[antonyms.Counter].Words;
                else if (j == 1) {
                    questionspan.innerHTML = "X";
                    $("#quespan" + (i) + (j + 1)).css("marginLeft", "2vw");
                } else {
                    questionspan.innerHTML = antonyms.QuetionArray[antonyms.Counter].Antonym;
                    questionspan.setAttribute("data", antonyms.QuetionArray[antonyms.Counter].Antonym);
                    questionspan.style.borderBottom = "2px dashed black";
                    questionspan.style.color = "transparent";
                    questionspan.setAttribute("onclick", "antonyms.setData(this)");
                    $("#quespan" + (i) + (j + 1)).css("marginLeft", "2vw");
                    $("#quespan" + (i) + (j + 1)).data("value", "off");
                }
            }
            antonyms.optionsArray.push(antonyms.QuetionArray[antonyms.Counter].Antonym);
            antonyms.Counter++;
        }
        antonyms.options();
        antonyms.displayOptions();
    }
    //choose the options
antonyms.options = function() {
        for (var j = 0; j < 2;) {
            var randomNumber = Math.floor(Math.random() * antonyms.data.length);
            while (antonyms.optionsArray.includes(antonyms.data[randomNumber])) {
                randomNumber = Math.floor(Math.random() * antonyms.data.length);
            }
            if (!antonyms.optionsArray.includes(antonyms.data[randomNumber].Antonym)) {
                antonyms.optionsArray.push(antonyms.data[randomNumber].Antonym);
                j++;
            }
        }
        shuffleArray(antonyms.optionsArray);
    }
    //to display options
antonyms.displayOptions = function() {
    document.getElementById("ansid").style.backgroundImage = Utils.path + "url('images/Long-Band.png')";
    for (var i = 0; i < antonyms.optionsArray.length; i++) {
        answerDiv = document.createElement("span");
        answerDiv.setAttribute("id", "ansdiv" + (i + 1));
        answerDiv.setAttribute("class", " nopadding words ");
        document.getElementById('ansid').appendChild(answerDiv);
        if (i == antonyms.optionsArray.length - 1)
            answerDiv.innerHTML = antonyms.optionsArray[i];
        else
            answerDiv.innerHTML = antonyms.optionsArray[i] + ",";
        answerDiv.setAttribute("data", antonyms.optionsArray[i]);
        answerDiv.setAttribute("onclick", "antonyms.getData(this)");
    }
}
antonyms.playInstructions = function() {
        document.getElementById("playSound").pause();
        document.getElementById('playSound').src = Utils.path + "sounds/Game_Instruction.mp3";
        document.getElementById("playSound").play();
    }
    //get selected word
antonyms.getData = function(object) {
        document.getElementById("playSound").pause();
        $(object).siblings().css("color", "black");
        $(object).siblings().css("fontWeight", "normal");
        antonyms.getDataFlag = true;
        antonyms.selectdWord = document.getElementById(object.id).getAttribute("data");
        document.getElementById(object.id).style.color = 'rgb(255 106 0)';
        document.getElementById(object.id).style.fontWeight = 'bold';
    }
    //set selected word
antonyms.setData = function(object) {
        var counter = 0;
        if (antonyms.getDataFlag == true) {
            document.getElementById(object.id).innerHTML = antonyms.selectdWord;
            document.getElementById(object.id).style.color = "black";
            $("#" + object.id).data("value", "on");
        }
        for (var i = 0; i < 4; i++) {
            for (var j = 0; j < 3; j++) {
                if (j == 2) {
                    if ($("#quespan" + (i) + (j + 1)).data("value") == "on")
                        counter++;
                }
            }
        }
        if (counter == 4)
            $("#checkBtn").css("visibility", "visible");
    }
    //to check sentence are correct format or not
antonyms.checkAnswer = function() {
        antonyms.rightAnsCount = 0;
        antonyms.correctWordArray = [];
        scoredMarks = 0;
        var flag = true;
        scoreFlag = true;
        helpFlag = false;
        antonyms.Attempt++;
        //document.getElementById("playSound").pause();
        $("#MainDiv").css("pointer-Events", "auto");
        for (var i = 0; i < antonyms.optionsArray.length; i++) {
            $("#ansdiv" + (i + 1)).css("color", "black");
        }
        for (var i = 0; i < 4; i++) {
            for (var j = 0; j < 3; j++) {
                if (j == 2) {
                    var sentence = document.getElementById("quespan" + (i) + (j + 1)).textContent; // get the span text
                    var data = document.getElementById("quespan" + (i) + (j + 1)).getAttribute("data");
                    if (sentence == data && $("#quespan" + (i) + (j + 1)).data("value") == "on") {
                        document.getElementById("quespan" + (i) + (j + 1)).style.color = "green";
                        antonyms.rightAnsCount++;
                        antonyms.correctWordArray.push(sentence);
                        scoredMarks++;
                    } else if ($("#quespan" + (i) + (j + 1)).data("value") == "on") {
                        flag = false;
                        document.getElementById("quespan" + (i) + (j + 1)).style.color = "red";
                        $("#answerImg").prop("src", Utils.path + "images/emoticons_wrong-ANS.png");
                        $("#MainDiv").css("pointer-Events", "none");
                        setTimeout(function() {
                            $("#answerImg").prop("src", Utils.path + "images/emoticons.png");
                            if (antonyms.Attempt == 1)
                                $("#MainDiv").css("pointer-Events", "auto");
                        }, 2000);
                        if (antonyms.helpClick == false)
                            $("#MainDiv").css("pointer-Events", "auto");
                    }
                }
            }
        }
        if (flag == false) {
            if (antonyms.Attempt == 1) {
                $("#playSound").prop("src", Utils.path + 'sounds/Try_again.mp3');
                document.getElementById("playSound").play();
            } else {
                $("#playSound").prop("src", Utils.path + 'sounds/Better_luck_next_time.mp3');
                document.getElementById("playSound").play();
            }
        }
        if (antonyms.Attempt == 2 && antonyms.helpClick == true && antonyms.helpCounter == 0) {
            $("#MainDiv,#checkBtn,#soundBtn").css("pointer-Events", "none");
            $("#nextDiv").addClass("blink_me");
        }
        if (antonyms.rightAnsCount == 4) {
            $("#nextDiv").addClass("blink_me");
            $("#playSound").prop("src", Utils.path + 'sounds/Brilent.mp3');
            document.getElementById("playSound").play();
            $("#MainDiv ,#helpBtn,#checkBtn,#soundBtn").css("pointer-Events", "none");
            $("#answerImg").prop("src", Utils.path + "images/emoticons_correct-ANS.png");
        }
    }
    //show the answer 
antonyms.help = function() {
        /* if(antonyms.helpCounter==0)
        	$("#nextDiv").addClass("blink_me");
        else
        	$("#nextDiv").removeClass("blink_me"); */
        document.getElementById("playSound").pause();
        scoreFlag = true;
        helpFlag = true;
        if (antonyms.helpClick == true) {
            $("#helpBtn").prop("src", Utils.path + "images/Hide-Help.png");
            $("#nextDiv").addClass("blink_me");
            if (antonyms.helpCounter == 0 && scoreFlag == true)
                antonyms.scoreFunction();
            antonyms.helpCounter++;
            $("#checkBtn,#answerImg").css("visibility", "hidden");
            $("#MainDiv").css("pointer-Events", "none");
            antonyms.helpClick = false;
            for (var i = 0; i < 4; i++) {
                for (var j = 0; j < 3; j++) {
                    if (j == 2) {
                        document.getElementById("quespan" + (i) + (j + 1)).style.color = "green";
                        var data = document.getElementById("quespan" + (i) + (j + 1)).getAttribute("data");
                        document.getElementById("quespan" + (i) + (j + 1)).innerHTML = data;
                    }
                }
            }
            for (var i = 0; i < antonyms.optionsArray.length; i++) {
                $("#ansdiv" + (i + 1)).css("color", "black");
            }
        } else {
            antonyms.helpClick = true;
            $("#helpBtn").prop("src", Utils.path + "images/Show-Help.png");
            $("#nextDiv").removeClass("blink_me");
            $("#MainDiv,#checkBtn").css("pointer-Events", "auto");
            $("#answerImg").css("visibility", "visible");
            for (var i = 0; i < antonyms.optionsArray.length; i++) {
                $("#ansdiv" + (i + 1)).css("color", "black");
            }
            for (var i = 0; i < 4; i++) {
                for (var j = 0; j < 3; j++) {
                    if (j == 2) {
                        $("#quespan" + (i) + (j + 1)).css("color", "transparent");
                        $("#quespan" + (i) + (j + 1)).css("borderBottom", "2px dashed black");
                    }
                }
            }
        }
    }
    //score Function
antonyms.scoreFunction = function() {
        if (antonyms.correctWordArray.length > 0) {
            for (var i = 0; i < antonyms.correctWordArray.length; i++) {
                console.log(antonyms.correctWordArray[i], antonyms.data.length, "word");
            }
        }
        console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
        if (Utils.mobileDeviceFlag) {
            Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
            if (antonyms.correctWordArray.length > 0) {
                for (var i = 0; i < antonyms.correctWordArray.length; i++) {
                    Android.addLearntData(antonyms.correctWordArray[i], antonyms.data.length, "word");
                }
            }
        }
    }
    //used to display next question
antonyms.nextQue = function() {
        $("#nextDiv").removeClass("blink_me");
        $("#helpBtn").prop("src", Utils.path + "images/Show-Help.png");
        $("#answerImg,#soundBtn").css("visibility", "visible");
        document.getElementById("playSound").pause();
        if (scoreFlag == true && helpFlag == false && antonyms.helpCounter == 0) {
            antonyms.scoreFunction();
        }
        if (scoreFlag == false) {
            scoredMarks = -1;
            antonyms.scoreFunction();
        }
        antonyms.helpCounter = 0;
        antonyms.correctWordArray = [];
        scoredMarks = 0;
        antonyms.helpClick = true;
        antonyms.Attempt = 0;
        scoreFlag = false;
        helpFlag = false;
        antonyms.getDataFlag = false;
        antonyms.flag = false;
        antonyms.optionsArray = [];
        antonyms.QueCounter++;
        $("#MainDiv ,#helpBtn,#soundBtn,#checkBtn").css("pointer-Events", "auto");
        document.getElementById('ansid').innerHTML = "";
        document.getElementById('questions').innerHTML = "";
        if (antonyms.QueCounter == 5)
            antonyms.gameOver();
        else
            antonyms.displayQuestions();
    }
    //go return back to cover page
antonyms.callHome = function() {
        if (scoreFlag == true) {
            antonyms.scoreFunction();
        }
        document.getElementById("playSound").pause();
        location.reload(true);
    }
    //credit 
antonyms.showCredits = function() {
        document.getElementById("acknowledgementDiv").style.display = "block";
    }
    //display game over page
antonyms.gameOver = function() {
    document.getElementById("playSound").pause();
    document.getElementById("coverpage").style.display = "none";
    document.getElementById("gamepage").style.display = "none";
    document.getElementById("endpage").style.display = "block";
    document.getElementById("acknowledgementDiv").style.display = "none";
    document.getElementById("resetbtnid").src = Utils.path + "images/Replay.png";
    document.getElementById("creditbtnid").src = Utils.path + "images/Credit.png";
    document.getElementById("gameover").style.backgroundImage = Utils.path + "url('images/Long-Band.png')";
    document.getElementById("acknowledgementDiv").style.backgroundImage = "url('images/Small-Band.png')";
    document.getElementById("gameoverdiv").innerHTML = antonyms.hardcodedData.message;
    for (i = 0; i < Object.keys(antonyms.AcknowledgementPageData).length; i++) {
        div = document.createElement('div');
        div.className = "fontClass nopadding col-xs-12 col-sm-12 col-md-12 col-lg-12";
        div.innerHTML = Object.keys(antonyms.AcknowledgementPageData)[i] + " : " + Object.values(antonyms.AcknowledgementPageData)[i];
        acknowledgementDiv.appendChild(div);
    }
    div = document.createElement('div');
    div.className = "fontClass col-xs-12 col-sm-12 col-md-12 col-lg-12";
    div.innerHTML = antonyms.hardcodedData['createdOn'];
    acknowledgementDiv.appendChild(div);
}