var synonyms = {}; // 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() {
        synonyms.setimage();
    }, 100);
    if (synonyms.hardcodedData.languageFont != null)
        setFontFamilyForLang(synonyms.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");
}
synonyms.globalvariables = function() {
        synonyms.QuetionArray = []; //to store the questions
        synonyms.flag = false; //used to call home
        synonyms.QueCounter = 0; //question counter 
        synonyms.getDataFlag = false; //used for get data 
        synonyms.rightAnsCount = 0; //used to count right answer
        synonyms.optionsArray = []; //used to store options
        synonyms.Counter = 0;
        synonyms.selectdWord;
        synonyms.practice = false;
        synonyms.Attempt = 0;
        synonyms.helpClick = true;
        synonyms.helpCounter = 0;
        synonyms.correctWordArray = [];
    }
    //set the all images
synonyms.setimage = function() {
    document.body.style.backgroundImage = "url(" + Utils.path + synonyms.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");
}
synonyms.gamePage = function() {
        synonyms.globalvariables();
        if (Utils.mobileDeviceFlag == true)
            synonyms.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 (synonyms.practice == true)
            document.getElementById("helpBtn").style.visibility = "hidden";
        else
            document.getElementById("helpBtn").style.visibility = "visible";
        synonyms.playInstructions();
        $("#helpBtn").prop("src", Utils.path + "images/Show-Help.png");
        synonyms.populate();
    }
    //populate questions from json
synonyms.populate = function() {
        for (var j = 0; j < 20; j++) {
            var randomNumber = Math.floor(Math.random() * synonyms.data.length);
            while (synonyms.QuetionArray.includes(synonyms.data[randomNumber])) {
                randomNumber = Math.floor(Math.random() * synonyms.data.length);
            }
            synonyms.QuetionArray.push(synonyms.data[randomNumber]);
        }
        shuffleArray(synonyms.QuetionArray);
        synonyms.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
synonyms.displayQuestions = function() {
        $("#checkBtn").css("visibility", "hidden");
        $("#answerImg").prop("src", Utils.path + "images/emoticons.png");
        scoreFlag = false;
        totalMarks = 4;
        d = new Date(); //date object
        startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(); //to get the current date
        synonyms.optionsArray = [];
        var droppableCounter = 0;
        for (var i = 0; i < 4; i++) {
            questionDiv = document.createElement("div");
            questionDiv.setAttribute("id", "quediv" + (i + 1));
            questionDiv.setAttribute("class", " nopadding quetions col-sm-5 col-lg-5 col-md-5 col-xs-5");
            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 = synonyms.QuetionArray[synonyms.Counter].word;
                else if (j == 1) {
                    questionspan.innerHTML = "=";
                    $("#quespan" + (i) + (j + 1)).css("marginLeft", "2vw");
                } else {
                    var word = synonyms.QuetionArray[synonyms.Counter].synonym;
                    var arr = word.split(",", 1);
                    questionspan.innerHTML = arr[0];
                    questionspan.setAttribute("data", arr[0]);
                    questionspan.style.borderBottom = "2px dashed black";
                    questionspan.style.color = "transparent";
                    questionspan.setAttribute("onclick", "synonyms.setData(this)");
                    $("#quespan" + (i) + (j + 1)).css("marginLeft", "2vw");
                    $("#quespan" + (i) + (j + 1)).data("value", "off");
                }
            }
            //var word=synonyms.QuetionArray[synonyms.Counter].Synonym;
            //var arr=word.split(",",1); 
            synonyms.optionsArray.push(arr[0]);
            synonyms.Counter++;
        }
        synonyms.options();
        synonyms.displayOptions();
    }
    //choose the options
synonyms.options = function() {
        for (var j = 0; j < 2;) {
            var randomNumber = Math.floor(Math.random() * synonyms.data.length);
            while (synonyms.optionsArray.includes(synonyms.data[randomNumber])) {
                randomNumber = Math.floor(Math.random() * synonyms.data.length);
            }
            var word = synonyms.data[randomNumber].synonym;
            var arr = word.split(",", 1);
            if (!synonyms.optionsArray.includes(arr[0])) {
                synonyms.optionsArray.push(arr[0]);
                j++;
            }
        }
        shuffleArray(synonyms.optionsArray);
    }
    //to display options
synonyms.displayOptions = function() {
    document.getElementById("ansid").style.backgroundImage = Utils.path + "url('images/Long-Band.png')";
    for (var i = 0; i < synonyms.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 == synonyms.optionsArray.length - 1)
            answerDiv.innerHTML = synonyms.optionsArray[i];
        else
            answerDiv.innerHTML = synonyms.optionsArray[i] + ",";
        answerDiv.setAttribute("data", synonyms.optionsArray[i]);
        answerDiv.setAttribute("onclick", "synonyms.getData(this)");
    }
}
synonyms.playInstructions = function() {
        document.getElementById("playSound").pause();
        document.getElementById('playSound').src = Utils.path + "sounds/Game_Instruction.mp3";
        document.getElementById("playSound").play();
    }
    //get selected word
synonyms.getData = function(object) {
        document.getElementById("playSound").pause();
        $(object).siblings().css("color", "black");
        synonyms.getDataFlag = true;
        synonyms.selectdWord = document.getElementById(object.id).getAttribute("data");
        document.getElementById(object.id).style.color = '#ff9f00';
    }
    //set selected word
synonyms.setData = function(object) {
        var counter = 0;
        if (synonyms.getDataFlag == true) {
            document.getElementById(object.id).innerHTML = synonyms.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
synonyms.checkAnswer = function() {
        synonyms.rightAnsCount = 0;
        scoredMarks = 0;
        synonyms.correctWordArray = [];
        var flag = true;
        scoreFlag = true;
        helpFlag = false;
        synonyms.Attempt++;
        document.getElementById("playSound").pause();
        $("#MainDiv").css("pointer-Events", "auto");
        for (var i = 0; i < synonyms.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";
                        synonyms.rightAnsCount++;
                        synonyms.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() {
                            if (synonyms.Attempt == 1)
                                $("#MainDiv").css("pointer-Events", "auto");
                            $("#answerImg").prop("src", Utils.path + "images/emoticons.png");
                        }, 2000);
                        if (synonyms.helpClick == false)
                            $("#MainDiv").css("pointer-Events", "auto");
                    }
                }
            }
        }
        if (flag == false) {
            if (synonyms.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 (synonyms.Attempt == 2 && synonyms.helpClick == true && synonyms.helpCounter == 0) {
            $("#MainDiv,#checkBtn,#soundBtn").css("pointer-Events", "none");
            $("#nextDiv").addClass("blink_me");
        }
		if (synonyms.Attempt >2 && synonyms.helpClick == false && synonyms.helpCounter >0) {
            $("#MainDiv,#checkBtn,#soundBtn").css("pointer-Events", "auto");
            $("#nextDiv").addClass("blink_me");
        }
        if (synonyms.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 
synonyms.help = function() {
        /* if(synonyms.helpCounter==0)
        	$("#nextDiv").addClass("blink_me");
        else
        	$("#nextDiv").removeClass("blink_me"); */
        document.getElementById("playSound").pause();
        scoreFlag = true;
        helpFlag = true;
        if (synonyms.helpClick == true) {
            $("#helpBtn").prop("src", Utils.path + "images/Hide-Help.png");
            $("#nextDiv").addClass("blink_me");
            if (synonyms.helpCounter == 0 && scoreFlag == true)
                synonyms.scoreFunction();
            synonyms.helpCounter++;
            $("#checkBtn,#answerImg").css("visibility", "hidden");
            $("#MainDiv").css("pointer-Events", "none");
            synonyms.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 < synonyms.optionsArray.length; i++) {
                $("#ansdiv" + (i + 1)).css("color", "black");
            }
        } else {
            synonyms.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 < synonyms.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
synonyms.scoreFunction = function() {
        if (synonyms.correctWordArray.length > 0) {
            for (var i = 0; i < synonyms.correctWordArray.length; i++) {
                console.log(synonyms.correctWordArray[i], synonyms.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 (synonyms.correctWordArray.length > 0) {
                for (var i = 0; i < synonyms.correctWordArray.length; i++) {
                    Android.addLearntData(synonyms.correctWordArray[i], synonyms.data.length, "word");
                }
            }
        }
    }
    //used to display next question
synonyms.nextQue = function() {
        $("#helpBtn").prop("src", Utils.path + "images/Show-Help.png");
        $("#nextDiv").removeClass("blink_me");
        $("#answerImg,#soundBtn").css("visibility", "visible");
        document.getElementById("playSound").pause();
        if (scoreFlag == true && helpFlag == false && synonyms.helpCounter == 0) {
            synonyms.scoreFunction();
        }
        if (scoreFlag == false) {
            scoredMarks = -1;
            synonyms.scoreFunction();
        }
        synonyms.helpCounter = 0;
        scoredMarks = 0;
        synonyms.helpClick = true;
        synonyms.Attempt = 0;
        scoreFlag = false;
        helpFlag = false;
        synonyms.getDataFlag = false;
        synonyms.flag = false;
        synonyms.optionsArray = [];
        synonyms.correctWordArray = [];
        //synonyms.QuetionArray=[];
        synonyms.QueCounter++;
        $("#MainDiv ,#helpBtn,#checkBtn,#soundBtn").css("pointer-Events", "auto");
        $("#checkBtn").css("visibility", "visible");
        document.getElementById('ansid').innerHTML = "";
        document.getElementById('questions').innerHTML = "";
        if (synonyms.QueCounter == 5)
            synonyms.gameOver();
        else
            synonyms.displayQuestions();
    }
    //go return back to cover page
synonyms.callHome = function() {
        if (scoreFlag == true && helpFlag == false) {
            synonyms.scoreFunction();
        }
        document.getElementById("playSound").pause();
        location.reload(true);
    }
    //credit 
synonyms.showCredits = function() {
        document.getElementById("acknowledgementDiv").style.display = "block";
    }
    //display game over page
synonyms.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("gameoverdiv").innerHTML = synonyms.hardcodedData.message;
    document.getElementById("gameover").style.backgroundImage = Utils.path + "url('images/Long-Band.png')";
    document.getElementById("acknowledgementDiv").style.backgroundImage = "url('images/Small-Band.png')";
    for (i = 0; i < Object.keys(synonyms.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(synonyms.AcknowledgementPageData)[i] + " : " + Object.values(synonyms.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 = synonyms.hardcodedData['createdOn'];
    acknowledgementDiv.appendChild(div);
}