
var waterFilling = {};
var Utils = {};
Utils.Path = "";
var starttime = '0', scoredmarks = '0', label ='';
var ArrStepData = [], smallGlassWaterArr = [], bigGlassWaterArr = [];

$(document).ready(function () {
    /* if(navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i))
		Utils.mobileDeviceFlag=true; */
    if((navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) && !(window.location.href.includes('http')))
        Utils.mobileDeviceFlag = true;
	else
        Utils.mobileDeviceFlag=false;

    setTimeout(function () {
        waterFilling.init();
    });
}, 200);

waterFilling.init = function () {
    $("#gamePage").show();
    waterFilling.setImages();
    waterFilling.initVariables();
    waterFilling.play();
    showQues();
}

waterFilling.setImages = function()
{
    $("#bottleDiv").css('backgroundImage', Utils.Path + "url('images/ques2/bottleEmpty.png')");
    $("#counter1Div").css('backgroundImage', Utils.Path + "url('images/ques2/button.png')");
    $("#counter2Div").css('backgroundImage', Utils.Path + "url('images/ques2/button.png')");
    $("#smallGlassDiv").css('backgroundImage', Utils.Path + "url('images/ques2/smallGlassEmpty.png')");
    $("#smallGlassWaterDiv").css('backgroundImage', Utils.Path + "url('images/ques2/smallGlass_water.png')");
    $("#bigGlassWaterDiv").css('backgroundImage', Utils.Path + "url('images/ques2/bigGlass_water.png')");
    $("#bigGlassDiv").css('backgroundImage', Utils.Path + "url('images/ques2/bigGlassEmpty.png')");
    $("#bottleWaterDiv").css('backgroundImage', Utils.Path + "url('images/ques2/bottle_waterFill.png')");
    $("#smallWaterDropImg").prop("src", Utils.Path + "images/ques2/waterDrop.png");
    $("#bigWaterDropImg").prop("src", Utils.Path + "images/ques2/waterDrop.png");
    $("#smallCupImg").prop("src", Utils.Path + "images/ques2/cup.png");
    $("#bigCupImg").prop("src", Utils.Path + "images/ques2/cup.png");
    $("#counterCup1Img").prop("src", Utils.Path + "images/ques2/cup.png");
    $("#counterCup2Img").prop("src", Utils.Path + "images/ques2/cup.png");
    $("#cupImg").prop("src", Utils.Path + "images/ques2/cup.png");
    $("#counter1Span").text("0");
    $("#counter2Span").text("0");
}

waterFilling.initVariables = function () 
{
    waterFilling.randQue = 0;
    waterFilling.waterCupCnt = 0;
    waterFilling.waterCup1Cnt = 0;
    waterFilling.waterCup2Cnt = 0;
    waterFilling.waterCup2SpanCnt = 0;
    waterFilling.attemptFlag = false;
	waterFilling.currentGame = null;
}

function showQues(){
    $('#questioMark').hide();
    $('#myModal').css('display','block');
}
function hideQuestion(){
    $('#questioMark').show();
    $('#myModal').css('display','none');
}

waterFilling.play = function()
{
    //Utils.mobileDeviceFlag = true;
    if(Utils.mobileDeviceFlag){					//For mobile device.
		/* randLang = Android.getGameLang();
		//randLang = 'Bangla-Bangladesh';
		lang = waterFilling.patternData.findIndex(obj => obj.language == randLang);
        var gameNo = Android.getGameID();
        //var gameCode = "L7E.5";
        var gameCodeArray = gameNo.split(".");
        var langRandQue = waterFilling.patternData[lang].gameArray.findIndex(obj => obj.gameName == gameCodeArray[0]);

        waterFilling.randQue = GData.findIndex(obj => obj.gameCode == gameNo); */
    }
    else{ 
        waterFilling.randQue = Math.floor(Math.random() * GData.length);        //For browser
        //waterFilling.randQue = 0;
    }

    $("#heading").css('font-size', GData[waterFilling.randQue].fontSize);               
	waterFilling.currentGame = GData[waterFilling.randQue];
    document.title = waterFilling.currentGame.title;   
    $('#queNo').text(waterFilling.currentGame.gameCode);
    document.getElementById("heading").innerHTML = waterFilling.currentGame.questionText;
    d = new Date();
	starttime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
    questionId = waterFilling.currentGame.gameCode;
    waterFilling.findRandomLevelNumber();
}

waterFilling.findRandomLevelNumber = function()
{
    var jsonObj = {}, glassLevel = 0, bigGlassRandomLevel = 0, smallGlassRandomLevel = 0;
    var glassWaterArray = [], smallGlassHeightArr = [], bigGlassHeightArr = [];
    /* ArrStepData = [
        {"BottleCover": 13, "GlassCover":19},
        {"BottleCover": 26, "GlassCover":0},
        {"BottleCover": 39, "GlassCover":32},
        {"BottleCover": 52, "GlassCover":16},
        {"BottleCover": 64, "GlassCover":0},    
    ]; */

    bottleHeight = 65;          //in vh
    smallGlassHeight = 40;      //in vh
    for(i=3; i<smallGlassHeight/2; i++){
        if(smallGlassHeight % i == 0)           //Find factors of small glass height
            smallGlassHeightArr.push(i);
    }
    
    bigGlassHeight = 50;        //in vh
    for(i=3; i<bigGlassHeight/2; i++){
        if(bigGlassHeight % i == 0)             //Find factors of big glass height
            bigGlassHeightArr.push(i);
    }

    smallGlassRandomLevel = Math.floor(Math.random() * smallGlassHeightArr.length);
    for(j=0; glassLevel <= smallGlassHeight; j++){      
        glassLevel = smallGlassHeightArr[smallGlassRandomLevel] * j;
        if(glassLevel < 40)                     //40 = height of small glass
            smallGlassWaterArr.push(glassLevel);
        else
            break;
    }

    bigGlassRandomLevel = Math.floor(Math.random() * bigGlassHeightArr.length);
    for(j=0; glassLevel <= bigGlassHeight; j++){      
        glassLevel = bigGlassHeightArr[bigGlassRandomLevel] * j;
        if(glassLevel < 50)                     //50 = height of big glass
            bigGlassWaterArr.push(glassLevel);
        else
            break;
    }       
    console.log('smallGlassWaterArr');
    console.log(smallGlassWaterArr);
    console.log('bigGlassWaterArr');
    console.log(bigGlassWaterArr); 
    
    for(i=smallGlassWaterArr.length-1; i>=0; i--){        //For Small Glass
        glassWaterArray.push(smallGlassWaterArr[i]);
    }
    for(i=bigGlassWaterArr.length-1; i>=0; i--){          //For Big Glass    
        glassWaterArray.push(bigGlassWaterArr[i]);
    }
    bottleTotalHeight = smallGlassWaterArr.length + bigGlassWaterArr.length;
    bottleHeightLevel = Math.floor(65 / bottleTotalHeight);             //Height of Bottle
    for(i=1; i<=bottleTotalHeight; i++){                    //For Bottle
        jsonObj["BottleCover"] = bottleHeightLevel * i;
        jsonObj["GlassCover"] = glassWaterArray[i-1];
        ArrStepData.push(jsonObj);
        jsonObj = {};
    } 
    console.log(ArrStepData);
    waterFilling.waterCup2Cnt = smallGlassWaterArr.length;
}


waterFilling.pourWater = function(cupId)      //Pour water into cup
{
    $("#cupDiv").css('visibility','hidden');
    $("#bottleNoWaterDiv").animate({height: ArrStepData[waterFilling.waterCupCnt].BottleCover + "vh"}, 1000, function(){
   if(cupId == "counterCup1Div"){
        $("#smallWaterCupDiv").css('visibility','visible');
        $("#bigWaterCupDiv").css('visibility','hidden');
        $( "#smallGlassNoWaterDiv" ).animate({height: ArrStepData[waterFilling.waterCup1Cnt].GlassCover + "vh"}, 1000, function(){
        if(waterFilling.smallGlassTimer){
            clearTimeout(waterFilling.smallGlassTimer);
            waterFilling.smallGlassTimer = null;
        }
        waterFilling.smallGlassTimer = setTimeout(function(){
            $("#cupDiv").css('visibility','visible');
            $("#smallWaterCupDiv").css('visibility','hidden');
        },700); 

        if(waterFilling.waterCup1Cnt == smallGlassWaterArr.length-1){
            $("#cupDiv").css('visibility','visible');
            $("#smallWaterCupDiv").css('visibility','hidden');
            $("#counterCup1Div").css('pointer-events','none');
        }
        waterFilling.waterCup1Cnt++;
        $("#counter1Span").text(waterFilling.waterCup1Cnt);
    });
   }
   else if(cupId == "counterCup2Div"){
        waterFilling.waterCup2SpanCnt++;
        $("#bigWaterCupDiv").css('visibility','visible');
        $("#smallWaterCupDiv").css('visibility','hidden');
        $( "#bigGlassNoWaterDiv" ).animate({height: ArrStepData[waterFilling.waterCup2Cnt].GlassCover + "vh"}, 1000, function(){
        
        if(waterFilling.bigGlassTimer){
            clearTimeout(waterFilling.bigGlassTimer);
            waterFilling.bigGlassTimer = null;
        }
        waterFilling.bigGlassTimer = setTimeout(function(){
            $("#cupDiv").css('visibility','visible');
            $("#bigWaterCupDiv").css('visibility','hidden');
        },700);  

        totalWaterCup2Cnt = (bigGlassWaterArr.length-1) + smallGlassWaterArr.length;
        if(waterFilling.waterCup2Cnt == totalWaterCup2Cnt){
            $("#cupDiv").css('visibility','visible');
            $("#bigWaterCupDiv").css('visibility','hidden');
            $("#counterCup2Div").css('pointer-events','none');
        }
    
        $("#counter2Span").text(waterFilling.waterCup2SpanCnt);
        waterFilling.waterCup2Cnt++;
    });    
   }
   waterFilling.waterCupCnt++;
    });    
}

waterFilling.nextQue = function()
{    
    if(waterFilling.attemptFlag == false){
        label = "null";
        scoredmarks = '0';
    }
    else{ 
        if(waterFilling.clickedOptionData == 'Correct'){     
            //console.log('Correct');
            scoredmarks = '2';
        }
        else{                                                   
            //console.log('Wrong');
            scoredmarks = '1';
        }
    }
    console.log(scoredmarks, starttime, label);
	if (Utils.mobileDeviceFlag)
        Android.gotoNextGame(scoredmarks, starttime, label);
}

waterFilling.reset = function()
{
    waterFilling.initVariables();
    $("#counter1Span").text("0");
    $("#counter2Span").text("0");
    $("#bottleNoWaterDiv").css('height','0vh');
    $("#smallGlassNoWaterDiv").css('height','40vh');
    $("#bigGlassNoWaterDiv").css('height','50vh');
    $("#counterCup1Div").css('pointer-events','auto');
    $("#counterCup2Div").css('pointer-events','auto');
}

function shuffle(randomArray) {
    var arrayLength = randomArray.length;
    var temp, random_index;
    while (arrayLength > 0) {
        random_index = Math.floor(Math.random() * arrayLength);
        arrayLength--;
        temp = randomArray[arrayLength];
        randomArray[arrayLength] = randomArray[random_index];
        randomArray[random_index] = temp;
    }
    return (randomArray);
}
