//startingDate:30-08-2019
var Singular = {};
var Utils = {};
Utils.Path = '';
Utils.mobileDeviceFlag = false;
var questionId = 0, startTime = 0, scoredMarks = 0, totalMarks = 40, resId = 0, level = 0;

$(document).ready(function () {
    $(".se-pre-con").fadeOut("slow");
    if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i))
        Utils.mobileDeviceFlag = true;
	/* if(Utils.mobileDeviceFlag) 
		Utils.Path=Android.getMediaPath("Singular");  */
    setTimeout(function () {
        Singular.init();
    }, 100);
});

Singular.init = function () {
    $("#coverPage").show();
    $("#gamePage").hide();
    $(".header").not("#logo").css("visibility", "hidden");
    document.body.style.backgroundImage = "url(" + Utils.Path + "images/Bg.png";
    Singular.initializeVariables();
    Singular.setAllImages();
    Singular.createDivs();
}

Singular.initializeVariables = function () {
    Singular.uniqueArr = [];
    Singular.counter = 0;
    Singular.firstFlag = true;
}
Singular.setAllImages = function () {
    document.getElementById("homeImage").src = Utils.Path + "images/Home.png";
    document.getElementById("logoImage").src = Utils.Path + "images/logo.png";
    document.getElementById("playImg").src = Utils.Path + "images/playButton.png";
    document.getElementById("oneText").innerHTML = "One";
    document.getElementById("manyText").innerHTML = "Many";
    document.getElementById("skipImg").src = Utils.Path + "images/Next.png";
    //document.getElementById("homeLogo").src=Utils.Path+"images/Home_button.png";
}

Singular.createDivs = function () {
    var parentDiv, i, outerDiv, wordDiv, buttonDiv, onDiv, offDiv, container;
    parentDiv = document.getElementById("questionDiv");
    for (i = 1; i <= 6; i++) {

        container = document.createElement("DIV");
        container.setAttribute("id", "container" + i);
        container.setAttribute("class", "containers col-xs-12 col-md-12 col-lg-12 col-sm-12");
        parentDiv.appendChild(container);

        outerDiv = document.createElement("DIV");
        outerDiv.setAttribute("id", "outerDiv" + i);
        outerDiv.setAttribute("class", "outerDivs col-xs-6 col-md-6 col-lg-6 col-sm-6 col-xs-offset-3 col-md-offset-3 col-lg-offset-3 col-sm-offset-3");
        container.appendChild(outerDiv);

        buttonDiv = document.createElement("DIV");
        buttonDiv.setAttribute("id", "buttonDiv" + i);
        buttonDiv.setAttribute("class", "buttonDivs col-xs-6 col-md-6 col-lg-6 col-sm-6");
        outerDiv.appendChild(buttonDiv);

        label = document.createElement("LABEL");
        label.setAttribute("id", "label" + i);
        label.setAttribute("class", "switch");
        buttonDiv.appendChild(label);

        input = document.createElement("INPUT");
        input.setAttribute("id", "togBtn" + i);
        input.setAttribute("type", "checkbox");
        label.appendChild(input);

        div = document.createElement("DIV");
        div.setAttribute("class", "slider round");
        label.appendChild(div);

        span1 = document.createElement("SPAN");
        span1.setAttribute("id", "firstSpan" + i);
        span1.setAttribute("class", "one");
        $(span1).text("ONE");
        span1.setAttribute("onclick", "Singular.toggle();");
        div.appendChild(span1);

        // span2 = document.createElement("SPAN");
        // span2.setAttribute("id", "secondSpan" + i);
        // span2.setAttribute("class", "many");
        // $(span2).text("MANY");
        // span2.setAttribute("onclick", "Singular.toggle();");
        // div.appendChild(span2);

        wordDiv = document.createElement("DIV");
        wordDiv.setAttribute("id", "wordDiv" + i);
        wordDiv.setAttribute("class", "wordDivs col-xs-6 col-md-6 col-lg-6 col-sm-6");
        outerDiv.appendChild(wordDiv);

        // 	onDiv = document.createElement("DIV");
        // 	onDiv.setAttribute("id", "onDiv" + i);
        // 	onDiv.setAttribute("class", "onDivs col-xs-5 col-md-5 col-lg-5 col-sm-5");
        // 	buttonDiv.appendChild(onDiv);

        // 	offDiv = document.createElement("DIV");
        // 	offDiv.setAttribute("id", "offDiv" + i);
        // 	offDiv.setAttribute("class", "offDivs col-xs-5 col-md-5 col-lg-5 col-sm-5 col-xs-offset-1 col-md-offset-1 col-lg-offset-1 col-sm-offset-1");
        // 	buttonDiv.appendChild(offDiv);
    }
}

Singular.playGame = function () {
    $("#coverPage").hide();
    $("#gamePage").show();
    $(".header").css("visibility", "visible");
    Singular.showPairs();
}

Singular.showPairs = function () {
    var i, rno;
    $("#learnDiv").show();
    $("#questionDiv").hide();

    for (i = 0; i < 3; i++) {
        rno = Math.floor(Math.random() * Singular.data.length);
        while (Singular.uniqueArr.includes(rno))
            rno = Math.floor(Math.random() * Singular.data.length);
        Singular.uniqueArr.push(rno);
    }
    Singular.playWords(Singular.uniqueArr[Singular.counter]);
}
Singular.playWords = function (rno) {

    if (Singular.firstFlag) {
        document.getElementById("oneWord").innerHTML = Singular.data[rno].one;
        document.getElementById("playSound").src = Utils.Path + "sounds/one.mp3";
    }
    else {
        document.getElementById("manyWord").innerHTML = Singular.data[rno].many;
        document.getElementById("playSound").src = Utils.Path + "sounds/many.mp3";
    }
    document.getElementById("playSound").play();
    Singular.playOneTime = setTimeout(function () {
        if (Singular.firstFlag)
            document.getElementById("playSound").src = Utils.Path + "sounds/" + Singular.data[rno].one + ".mp3";
        else
            document.getElementById("playSound").src = Utils.Path + "sounds/" + Singular.data[rno].many + ".mp3";
        document.getElementById("playSound").play();
        if (Singular.firstFlag) {
            Singular.firstFlag = false;
            Singular.playManyTime = setTimeout(function () {
                Singular.playWords(rno);
            }, 1000);
        }
        else {
            if (Singular.counter < 2) {
                Singular.showPairTime = setTimeout(function () {
                    document.getElementById("oneWord").innerHTML = "";
                    document.getElementById("manyWord").innerHTML = "";
                    Singular.firstFlag = true;
                    Singular.counter++;
                    Singular.playWords(Singular.uniqueArr[Singular.counter]);
                }, 2000);

            }
            else {
                setTimeout(function () {
                    Singular.showQuestion();
                }, 2000);
            }
        }
    }, 800);

}

Singular.showQuestion = function () {

    var i, counter = 1, arr = [];
    clearTimeout(Singular.showPairTime);
    clearTimeout(Singular.playOneTime);
    clearTimeout(Singular.playManyTime);
    document.getElementById("playSound").pause();
    // console.log(Singular.uniqueArr);
    // Singular.shuffle(Singular.uniqueArr);
    // console.log(Singular.uniqueArr);
    $("#learnDiv").hide();
    $("#questionDiv").show();
    for (i = 0; i < 3; i++) {
        Singular.wordArr.push(Singular.data[Singular.uniqueArr[i]].one);
        Singular.wordArr.push(Singular.data[Singular.uniqueArr[i]].many);
    }
    for (i = 0; i < 4; i++) {
        rno = Math.floor(Math.random() * Singular.wordArr.length)
        document.getElementById("wordDiv" + i).innerHTML = Singular.wordArr[rno];
        if (rno % 2 == 0)

            document.getElementById("wordDiv" + counter).setAttribute("word", "one");
        counter++;
        document.getElementById("wordDiv" + counter).innerHTML = Singular.data[Singular.uniqueArr[i]].many;
        document.getElementById("wordDiv" + counter).setAttribute("word", "many");
        counter++;
    }
    // $(".onDivs").text("one");
    // $(".offDivs").text("many");

    //Singular.placeImageInRandomPlaces();
}
//called from Countinggame.createDiv function. places the created divs @ random positions. 
Singular.placeImageInRandomPlaces = function () {

    var containerW = $("#questionDiv").width();
    var containerH = $("#questionDiv").height();
    var positions = [];
    $(".outerDivs").each(function () {
        var coords = {
            w: $(this).outerWidth(true),
            h: $(this).outerHeight(true)
        };

        var success = false;
        while (!success) {
            coords.x = parseInt(Math.random() * (containerW - coords.w));
            coords.y = parseInt(Math.random() * (containerH - coords.h));
            var success = true;
            $.each(positions, function () {
                if (coords.x <= (this.x + this.w) &&
                    (coords.x + coords.w) >= this.x &&
                    coords.y <= (this.y + this.h) &&
                    (coords.y + coords.h) >= this.y) {
                    success = false;
                }
            });
        }
        positions.push(coords);
        $(this).css({ 'top': coords.y + 'px', 'left': coords.x + 'px' });
    });
}

Singular.shuffle = function (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;
}