var MultiplicationChart = {}; // namespace name
var Utils = {};
Utils.path = '';
Utils.mobileDeviceFlag = false;
var questionId = 0,
    startTime = 0,
    scoredMarks = 0,
    totalMarks = 0,
    resId = 0,
    label = "",
    level = 1;
$(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() {
        MultiplicationChart.setImages();
    }, 100);
}, 200);
MultiplicationChart.globalVariables = function() {
    MultiplicationChart.uniqueNumber = []; // generate unique number
    MultiplicationChart.fieldId; // store   id
    MultiplicationChart.timeOut;
    MultiplicationChart.currentId;
    MultiplicationChart.rowPosition; //previous row id	
    MultiplicationChart.colPosition; //previous column id
    MultiplicationChart.randomNum; //unique random number
    MultiplicationChart.level = 1;
    MultiplicationChart.operant1; //store previous id
    MultiplicationChart.operant2; //store previous id
    MultiplicationChart.time = "00:00";
    MultiplicationChart.checkNumber = [];
    MultiplicationChart.timer;
}
MultiplicationChart.setImages = function() {
        MultiplicationChart.globalVariables();
        document.body.style.backgroundImage = "url(" + Utils.path + MultiplicationChart.coverImage + ")";
        document.getElementById("homeid").style.visibility = "hidden";
        document.getElementById("nextbtnid").style.display = "none";
        document.getElementById("timerDiv").style.visibility = "hidden";
        $("#playbtnid").prop("src", "images/Play.png");
        $("#homeid").prop("src", "images/Home.png");
        $("#logoid").prop("src", "images/logo.png");
        $("#nextbtnid").prop("src", "images/Next.png");
    }
    //create options
MultiplicationChart.selectMatrix = function() {
    MultiplicationChart.playInstructions();
    var d = new Date(); //date object
    startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(); //to get the current date
    if (MultiplicationChart.level == 1) {
        MultiplicationChart.time = parseInt(MultiplicationChart.time);
        var Minutes = 60 * MultiplicationChart.time;
        display = document.querySelector('#timerDiv');
        document.getElementById("timerDiv").innerHTML = MultiplicationChart.time;
        startTimer(Minutes, display);
    }
    document.body.style.backgroundImage = "url('images/BG.jpg')";
    document.getElementById("coverpage").style.display = "none";
    document.getElementById("gamePage").style.display = "block";
    document.getElementById("endpage").style.display = "none";
    document.getElementById("nextbtnid").style.display = "block";
    document.getElementById("timerDiv").style.visibility = "visible";
    document.body.style.backgroundImage = "url('images/BG.jpg')";
    document.getElementById("homeid").style.visibility = "visible";
    var counter1 = 1;
    var counter2 = 4;
    for (j = 0; j < 11; j++) {
        div = document.createElement("div");
        div.setAttribute("id", "outerDiv" + (j + 1));
        div.setAttribute("class", " nopadding col-sm-12 col-lg-12 col-md-12 col-xs-12");
        document.getElementById('questions').appendChild(div);
        for (var i = 0; i < 11; i++) {
            Div = document.createElement("div");
            Div.setAttribute("id", "div" + (j) + (i));
            Div.setAttribute("class", " nopadding quetions text-center col-sm-1 col-lg-1 col-md-1 col-xs-1");
            div.appendChild(Div);
            Div.style.height = "6.7vh";
            Div.style.width = "4vw";
            var div1 = document.getElementById("div" + (j) + (i));
            if (i == 0 && j == 0) {
                div1.innerHTML = "X";
                div1.style.backgroundColor = "rgb(139, 216, 84)";
            }
            if (i == 0 && j > 0) {
                div1.innerHTML = counter1;
                div1.setAttribute("onclick", "MultiplicationChart.getData(this)");
                div1.style.backgroundColor = "rgb(230, 227, 182)";
                div1.setAttribute("data", "Left");
            }
            if (i == 1 && j > 0) { /////
                div1.innerHTML = counter1;
                div1.style.backgroundColor = "rgb(230, 227, 182)";
                MultiplicationChart.checkNumber.push(counter1);
                counter1++;
            }
            if (j == 0 && i > 0) {
                div1.innerHTML = i;
                div1.setAttribute("onclick", "MultiplicationChart.getData(this)");
                div1.style.backgroundColor = "rgb(230, 227, 182)";
            }
            if (j == 1 && i > 0) { ////
                div1.innerHTML = i;
                div1.style.backgroundColor = "rgb(230, 227, 182)";
                MultiplicationChart.checkNumber.push(i);
            }
            if (j > 1 && i > 1) { ////
                div1.innerHTML = counter2;
                div1.style.backgroundColor = "rgb(230, 227, 182)";
                MultiplicationChart.checkNumber.push(counter2);
                counter2 = counter2 + j;
            }
        }
        counter2 = 2 * (j + 1);
    }
    MultiplicationChart.selectMatrix1();
}
MultiplicationChart.playInstructions = function() {
    document.getElementById("playSound").pause();
    document.getElementById('playSound').src = Utils.path + "sounds/MultiplicationchartInstruction2.mp3";
    document.getElementById("playSound").play();
}

function startTimer(duration, display) {
    var timer = duration,
        minutes, seconds, mili;
    MultiplicationChart.timer = setInterval(function() {
        minutes = parseInt(timer / 60, 10);
        seconds = parseInt(timer % 60, 10);
        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;
        display.textContent = minutes + ":" + seconds;
        timer++;
        MultiplicationChart.time = minutes + ":" + seconds;
    }, 500);
}
//create table
MultiplicationChart.selectMatrix1 = function() {
        var counter = 1;
        for (var j = 0; j < 10; j++) {
            div = document.createElement("div");
            div.setAttribute("id", "outerDiv1" + (j + 1));
            div.setAttribute("class", " nopadding col-sm-12 col-lg-12 col-md-12 col-xs-12");
            document.getElementById('table').appendChild(div);
            counter = j + 1;
            for (var i = 0; i < 10; i++) {
                Div = document.createElement("div");
                Div.setAttribute("id", "textdiv" + (j) + (i));
                Div.setAttribute("class", " nopadding quetions text-center col-sm-1 col-lg-1 col-md-1 col-xs-1");
                div.appendChild(Div);
                Div.style.height = "6.7vh";
                Div.style.width = "4vw";
                document.getElementById("textdiv" + (j) + (i)).innerHTML = counter;
                document.getElementById("textdiv" + (j) + (i)).style.backgroundColor = "white";
                document.getElementById("textdiv" + (j) + (i)).setAttribute("onclick", "MultiplicationChart.getNumber(this.id)");
                counter = counter + 10;
            }
        }
        MultiplicationChart.createBlankFields();
        row = 1;
        col = 1;
        MultiplicationChart.fillBlankField(row, col);
    }
    // hide the random any grid
MultiplicationChart.createBlankFields = function() {
        if (MultiplicationChart.level == 1) {
            for (j = 1; j < 11; j++) {
                MultiplicationChart.uniqueNumber = [];
                var randomNumber = Math.floor(Math.random() * (3 - 2) + 2);
                for (var i = 0; i < 1; i++) {
                    MultiplicationChart.createUniqueNumber();
                    var data = document.getElementById("div" + (j) + (randomNum)).innerHTML;
                    document.getElementById("div" + (j) + (randomNum)).setAttribute("setdata", data);
                    document.getElementById("div" + (j) + (randomNum)).setAttribute("data", "ON");
                    document.getElementById("div" + (j) + (randomNum)).innerHTML = "";
                    document.getElementById("div" + (j) + (randomNum)).style.backgroundColor = "white";
                }
            }
        } else if (MultiplicationChart.level == 2) {
            for (j = 1; j < 11; j++) {
                MultiplicationChart.uniqueNumber = [];
                var randomNumber = Math.floor(Math.random() * (3 - 2) + 2);
                for (var i = 0; i < 11; i++) {
                    if (i == j) {
                        var data = document.getElementById("div" + (j) + (i)).innerHTML;
                        document.getElementById("div" + (j) + (i)).setAttribute("setdata", data);
                        document.getElementById("div" + (j) + (i)).setAttribute("data", "ON");
                        document.getElementById("div" + (j) + (i)).innerHTML = "";
                        document.getElementById("div" + (j) + (i)).style.backgroundColor = "white";
                    }
                }
            }
        } else if (MultiplicationChart.level == 3) {
            var randomNumber = Math.floor(Math.random() * (10) + 1);
            var ranNo = Math.floor(Math.random() * (2) + 0),
                p, q;
            for (var i = 1; i < 11; i++) {
                if (ranNo == 0) { p = i, q = randomNumber; } else { p = randomNumber, q = i; }
                var data = document.getElementById("div" + (p) + (q)).innerHTML;
                document.getElementById("div" + (p) + (q)).setAttribute("setdata", data);
                document.getElementById("div" + (p) + (q)).setAttribute("data", "ON");
                document.getElementById("div" + (p) + (q)).innerHTML = "";
                document.getElementById("div" + (p) + (q)).style.backgroundColor = "white";
            }

        } else if (MultiplicationChart.level == 4) {
            var randomNumber = Math.floor(Math.random() * MultiplicationChart.checkNumber.length);
            var number = MultiplicationChart.checkNumber[randomNumber];
            for (j = 0; j < 11; j++) {
                for (var i = 0; i < 11; i++) {
                    if (i >= 1 && j >= 1) {
                        var data = document.getElementById("div" + (j) + (i)).innerHTML;
                        if (number == data) {
                            document.getElementById("div" + (j) + (i)).setAttribute("setdata", data);
                            document.getElementById("div" + (j) + (i)).setAttribute("data", "ON");
                            document.getElementById("div" + (j) + (i)).innerHTML = "";
                            document.getElementById("div" + (j) + (i)).style.backgroundColor = "white";
                        }
                    }
                }
            }

        } else {
            for (j = 0; j < 11; j++) {
                for (var i = 0; i < 11; i++) {
                    if (i >= 1 && j >= 1) {
                        var data = document.getElementById("div" + (j) + (i)).innerHTML;
                        document.getElementById("div" + (j) + (i)).setAttribute("setdata", data);
                        document.getElementById("div" + (j) + (i)).setAttribute("data", "ON");
                        document.getElementById("div" + (j) + (i)).innerHTML = "";
                        document.getElementById("div" + (j) + (i)).style.backgroundColor = "white";
                    }
                }
            }

        }
    }
    //create unique number
MultiplicationChart.createUniqueNumber = function() {
        for (var i = 0; i < 1; i++) {
            randomNum = Math.floor(Math.random() * (11 - 1) + 1);
            while (MultiplicationChart.uniqueNumber.includes(randomNum)) {
                randomNum = Math.floor(Math.random() * (11 - 1) + 1);
            }
            MultiplicationChart.uniqueNumber.push(randomNum);
        }
        return randomNum;
    }
    //fill the blank field
MultiplicationChart.fillBlankField = function(row, col) {
        $("#table").css("pointer-Events", "auto");
        loop1: for (var j = row; j < 11;) {
            loop2: for (var i = col; i < 11;) {
                if (document.getElementById("div" + (j) + (i)).getAttribute("data") == "ON") {
                    $("#" + "div" + (j) + (i)).css("background-color", "rgb(255,244,79)");
                    $("#" + "div" + (j) + (i)).addClass("blink_me");
                    break loop1;
                }
                i++;
            }
            col = 1;
            j++;
            if (j == 11) {
                $("#" + MultiplicationChart.fieldId).css("background-color", "white");
                return true;
            }
        }
        MultiplicationChart.currentId = "div" + (j) + (i);
        MultiplicationChart.rowPosition = j, MultiplicationChart.colPosition = i;
        $("#" + MultiplicationChart.operant1).css("background-color", "rgb(230, 227, 182)");
        $("#" + MultiplicationChart.operant2).css("background-color", "rgb(230, 227, 182)");
        $("#" + "div" + (0) + (i)).css("background-color", "rgb(97, 226, 204)");
        $("#" + "div" + (j) + (0)).css("background-color", "rgb(97, 226, 204)");
        MultiplicationChart.operant1 = "div" + (0) + (i);
        MultiplicationChart.operant2 = "div" + (j) + (0);
        if (MultiplicationChart.timeOut) {
            clearTimeout(MultiplicationChart.timeOut);
            MultiplicationChart.timeOut = null;
        }
        MultiplicationChart.timeOut = setTimeout(function() {
            $("#" + "div" + (j) + (i)).removeClass("blink_me");
            $("#" + "div" + (j) + (i)).css("background-color", "white");
            if ($("#" + "div" + (j) + (i)).text().length == 0) {
                document.getElementById("div" + (j) + (i)).innerHTML = document.getElementById("div" + (j) + (i)).getAttribute("setdata");
                document.getElementById("div" + (j) + (i)).style.color = "black";
            }
            if (i == 10) {
                i = 0;
                j++;
            }
            if (j == 11) {
                $("#" + MultiplicationChart.fieldId).css("background-color", "white");
                return true;
            }
            document.getElementById('playSound').src = Utils.path + "sounds/ting.mp3";
            document.getElementById("playSound").play();
            MultiplicationChart.fillBlankField(j, ++i);
        }, 6000);
    }
    //give the number to number chart
MultiplicationChart.getNumber = function(obj) {
        $("#table").css("pointer-Events", "none");
        document.getElementById("playSound").pause();
        if (MultiplicationChart.timeOut) {
            clearTimeout(MultiplicationChart.timeOut);
            MultiplicationChart.timeOut = null;
        }
        $("#" + MultiplicationChart.fieldId).css("background-color", "white");
        MultiplicationChart.fieldId = obj;
        $("#" + MultiplicationChart.fieldId).css("background-color", "gray");
        var answer = document.getElementById(MultiplicationChart.currentId).innerHTML = document.getElementById(obj).innerHTML;
        if (answer == document.getElementById(MultiplicationChart.currentId).getAttribute("setdata")) {
            document.getElementById(MultiplicationChart.currentId).style.color = "green";
            $("#playSound").prop("src", Utils.path + 'sounds/right.mp3');
            document.getElementById("playSound").play();
            scoredMarks++;
        } else {
            $("#playSound").prop("src", Utils.path + 'sounds/Try_again.mp3');
            document.getElementById("playSound").play();
            document.getElementById(MultiplicationChart.currentId).style.color = "red";
        }
        MultiplicationChart.timer = setTimeout(function() {
            $("#" + MultiplicationChart.currentId).removeClass("blink_me");
            $("#" + MultiplicationChart.currentId).css("background-color", "white");
            $("#" + MultiplicationChart.fieldId).css("background-color", "white");
            if (answer != document.getElementById(MultiplicationChart.currentId).getAttribute("setdata")) {
                document.getElementById(MultiplicationChart.currentId).innerHTML = "";
                MultiplicationChart.fillBlankField(MultiplicationChart.rowPosition, MultiplicationChart.colPosition);
            } else {
                document.getElementById('playSound').src = Utils.path + "sounds/ting.mp3";
                document.getElementById("playSound").play();
                MultiplicationChart.fillBlankField(MultiplicationChart.rowPosition, ++MultiplicationChart.colPosition);
            }
        }, 1000);
    }
    //display next level
MultiplicationChart.nextLevel = function() {
        totalMarks = 10;
        console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
        if (Utils.mobileDeviceFlag) {
            Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
        }
        if (MultiplicationChart.level == 5) {
            MultiplicationChart.gameOver();
        } else {
            document.getElementById('questions').innerHTML = "";
            document.getElementById('table').innerHTML = "";
            scoredMarks = 0;
            MultiplicationChart.level++;
            MultiplicationChart.selectMatrix();
        }
    }
    //go to the cover page
MultiplicationChart.callHome = function() {
        location.reload(true);
    }
    //display game over page
MultiplicationChart.gameOver = function() {
    clearTimeout(MultiplicationChart.timer);
    clearTimeout(MultiplicationChart.timeOut);
    document.getElementById("playSound").pause();
    document.getElementById("coverpage").style.display = "none";
    document.getElementById("gamePage").style.display = "none";
    document.getElementById("endpage").style.display = "block";
    document.getElementById("gameover").innerHTML = MultiplicationChart.hardcodedData.message;
    document.getElementById("showTime").innerHTML = "समय:-    " + MultiplicationChart.time;
}