var EstimationGrain={};
var Utils={};
Utils.Path='';
Utils.mobileDeviceFlag=false;

EstimationGrain.calibrationValue=100,EstimationGrain.temp=0;
var canvas=document.getElementById('weightCanvas');
var context=canvas.getContext('2d');
var countWT=110,tempVar=0;
var time=3000;
EstimationGrain.randomIngredient=0;
EstimationGrain.randomQue=0;
EstimationGrain.lineIntreval=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("TikTok"); 
	setTimeout(function()
	{
		document.body.style.backgroundImage="url("+Utils.Path+"images/scene1_background.png"+")";
		document.getElementById('grainScreen1').classList.add('fadeInRight');
		EstimationGrain.setImages();
		EstimationPayment.setNoteForReturn();
		if(EstimationGrain.hardCodedData['languageFont']!=null)
			setFontFamilyForLang("fonts/"+EstimationGrain.hardCodedData['languageFont']);		
	},50);
});

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");            
}

EstimationGrain.setImages=function()
{
	document.getElementById('grainScreen1').style.backgroundImage="url("+Utils.Path+"images/general_store_close_up.png"+")";
	document.getElementById('customer').src=Utils.Path+"images/custStanding.png";
	document.getElementById('check').src=Utils.Path+"images/startButton.png";
	document.getElementById('reset').src=Utils.Path+"images/resetButton.png";
	setTimeout(function()
	{
		EstimationGrain.setQuestion();
		EstimationGrain.nextScreen();
	},4000);
}

EstimationGrain.setQuestion=function()
{
	EstimationGrain.randomIngredient=Math.floor(Math.random()*EstimationGrain.ingredient.length);
	EstimationGrain.randomQue=Math.floor(Math.random()*EstimationGrain.Question.length);
	document.getElementById('bag').src=Utils.Path+"images/"+EstimationGrain.ingredient[EstimationGrain.randomIngredient].ingredientBag;
	document.getElementById('ingredient').innerHTML=EstimationGrain.ingredient[EstimationGrain.randomIngredient].ingrdientName;
	document.getElementById('quantity').innerHTML=EstimationGrain.Question[EstimationGrain.randomQue].question;
}

EstimationGrain.nextScreen=function()
{
	document.getElementById('grainScreen1').style.display="none";
	document.getElementById('grainScreen2').style.display="block";
	document.getElementById('grainScreen2').style.backgroundImage="url("+Utils.Path+"images/generalShopBack.png"+")";
	document.getElementById('orderBoard').style.backgroundImage="url("+Utils.Path+"images/orderBoard.png"+")";
	document.getElementById('customerScreen2').src=Utils.Path+"images/customer1lookingright.png";
	document.getElementById('weightImg').src=Utils.Path+"images/weighingScale.png";
	document.getElementById('handImg').src=Utils.Path+"images/"+EstimationGrain.ingredient[EstimationGrain.randomIngredient].ingredientHoldingScoop;
	document.getElementById('assistantScreen2').src=Utils.Path+"images/assistant_looking_up.png";
	document.getElementById('bagofdal').src=Utils.Path+"images/emptygrainbag.png";
	document.getElementById('startStop').src=Utils.Path+"images/startButton.png";
	tempVar=0;
	EstimationGrain.weightCount();
}
	
EstimationGrain.weightCount=function()
{
	var value=20,weight=0,count=0,linePoint=0;
	context.fillStyle = "black";
	context.fillRect(100,50,1300,100);
	canvas.style.borderRadius="20px";
	context.lineWidth=15;
	if(EstimationGrain.temp>2)
	{
		EstimationGrain.calibrationValue=EstimationGrain.weightData[1].calibrationValue;
	}
	if(EstimationGrain.temp>4)
		EstimationGrain.calibrationValue=EstimationGrain.weightData[2].calibrationValue;
	
	if(EstimationGrain.lineIntreval)
	{
		EstimationGrain.lineIntreval=null;
		clearInterval(EstimationGrain.lineIntreval);
	}
	 EstimationGrain.lineIntreval=setInterval(function ()
	{
		count+=100;
		linePoint+=120;
		if(count%EstimationGrain.calibrationValue==0)
		{
			context.font ="bold 4vw Arial";
			context.fillText(count,linePoint+60,210);
			context.font ="bold 3.2vw Arial";
			context.fillText("in gms",650,25);
		}	
			context.moveTo(linePoint+100,58);
			context.lineTo(linePoint+100,100);
			context.strokeStyle="white";
			context.stroke();
		if(count>=1000)
		{
			clearInterval(EstimationGrain.lineIntreval);
			context.beginPath();
			context.moveTo(110,51);
			context.lineTo(110,100);
			context.strokeStyle="red";
			context.stroke();
		}				
	},5);
}

EstimationGrain.startPouring=function()
{
	document.getElementById('handImg').src=Utils.Path+"images/"+EstimationGrain.ingredient[EstimationGrain.randomIngredient].ingredientFallingScoop;
	document.getElementById('bagofdal').src=Utils.Path+"images/"+EstimationGrain.ingredient[EstimationGrain.randomIngredient].ingredientBag;
	document.getElementById('startStop').src=Utils.Path+"images/stopButton.png";
	EstimationGrain.weightMover();
}

EstimationGrain.weightMover=function()
{
	if(EstimationGrain.weightInterval!=null)
	{
		clearInterval(EstimationGrain.weightInterval);
		EstimationGrain.weightInterval=null;
		EstimationGrain.checkAnswer();
	}
	else
	{
		EstimationGrain.weightInterval=setInterval(function ()
		{
			context.beginPath();
			context.moveTo(countWT,51);
			context.lineTo(countWT,100);
			context.strokeStyle="red";
			context.stroke();
			tempVar=tempVar+0.18;
			countWT+=1;
			if(countWT>1350)
			{
				clearInterval(EstimationGrain.weightInterval);
				setTimeout(function()
				{
					EstimationGrain.nextQuestion();
				},1000);
			}
		},20);
	}
}

EstimationGrain.checkAnswer=function()
{
	clearInterval(EstimationGrain.weightInterval);
	countWT=countWT-(100+tempVar);
	if((countWT)>=EstimationGrain.Question[EstimationGrain.randomQue].answer-15 && (countWT)<=EstimationGrain.Question[EstimationGrain.randomQue].answer+15)
	{
		EstimationGrain.answerWiseScreen("customerHappy.png",EstimationGrain.ingredient[EstimationGrain.randomIngredient].ingredientBag,"assisatantHappy.png");	
		setTimeout(function()
		{
			document.getElementById('paymentPageScreen3').style.display="block";
			EstimationGrain.showRateCardPage();
		},2000);
	}	
	else if((countWT)<EstimationGrain.Question[EstimationGrain.randomQue].answer)
	{
		EstimationGrain.answerWiseScreen("customerSad.png",EstimationGrain.ingredient[EstimationGrain.randomIngredient].ingredientLowQty,"assistantSad.png");
		setTimeout(function()
		{
			EstimationGrain.nextQuestion();
		},2000);
	}
	
	else if((countWT)>EstimationGrain.Question[EstimationGrain.randomQue].answer)
	{
		EstimationGrain.answerWiseScreen("customerSad.png",EstimationGrain.ingredient[EstimationGrain.randomIngredient].ingredientOverFlow,"assistantSad.png");
		setTimeout(function()
		{
			EstimationGrain.nextQuestion();
		},2000);
	}
}

EstimationGrain.answerWiseScreen=function(customer,ingredient,assistant)
{
	document.getElementById('bagDiv').style.display="none";
	document.getElementById('pouringHand').style.display="none";
	document.getElementById('startStopDiv').style.display="none";
	document.getElementById('bagDivScreen3').style.display="block";
	document.getElementById('bagScreen3').src=Utils.Path+"images/"+ingredient;
	document.getElementById('customerScreen2').src=Utils.Path+"images/"+customer;
	document.getElementById('assistantScreen2').src=Utils.Path+"images/"+assistant;
	document.getElementById('orderBoard').setAttribute("class","col-xs-5 col-sm-5 col-lg-5 col-md-5 col-xs-offset-4 col-sm-offset-4 col-lg-offset-4 col-md-offset-4");
	document.getElementById('weightDiv').setAttribute("class","col-xs-4 col-sm-4 col-lg-4 col-md-4 col-xs-offset-5 col-sm-offset-5 col-lg-offset-5 col-md-offset-5");
	document.getElementById('container').setAttribute("class","col-xs-4 col-sm-4 col-lg-4 col-md-4 col-xs-offset-5 col-sm-offset-5 col-lg-offset-5 col-md-offset-5");
	document.getElementById('customerImgDivScreen2').setAttribute("class","col-xs-2 col-sm-2 col-lg-2 col-md-2 col-xs-offset-2 col-sm-offset-2 col-lg-offset-2 col-md-offset-2");
	document.getElementById('assistantImgDivScreen2').style.right='15vw';
}

EstimationGrain.createRateCard=function()
{
	var row,column;
	document.getElementById('rateScreen3').innerHTML="Rate";
	document.getElementById('orderScreen3').innerHTML="Order";
	document.getElementById('ingredientScreen3').innerHTML=EstimationGrain.ingredient[EstimationGrain.randomIngredient].ingrdientName;
	document.getElementById('qtyScreen3').innerHTML=EstimationGrain.Question[EstimationGrain.randomQue].question;
	document.getElementById('moneyToPay').innerHTML="Rs. "+Math.floor(parseInt(EstimationGrain.ingredient[EstimationGrain.randomIngredient].price)/1000*parseInt(EstimationGrain.Question[EstimationGrain.randomQue].answer));
	for(var i=0;i<3;i++)
	{
		row=document.createElement('tr');
		row.setAttribute("id","row"+(i+1));
		row.setAttribute("class","ingredientsRows fontClass text-center ");
		for(var j=1;j<=2;j++)
		{
			column=document.createElement('td');
			column.setAttribute("id","column"+(i+1)+j);
			column.setAttribute("class","fontClass text-center");
			row.appendChild(column);
			if(j==1)
				column.innerHTML=EstimationGrain.ingredient[i].ingrdientName;
			else
				column.innerHTML="Rs. "+EstimationGrain.ingredient[i].price+"/Kg";
		}
		ratesIngredient.appendChild(row);
	}
}

EstimationGrain.showRateCardPage=function()
{
	document.getElementById('grainScreen2').style.display="none";
	document.body.style.backgroundImage="url("+Utils.Path+"images/general_store_cash_counter.png"+")";
	document.getElementById('customerScreen3').src=Utils.Path+"images/customer_standing.png";
	document.getElementById('rateBoardDiv').style.backgroundImage="url("+Utils.Path+"images/rate_display_board.png"+")";
	document.getElementById('rateBoardDiv').style.height="60vh";
	EstimationGrain.createRateCard();
}

EstimationGrain.nextQuestion=function()
{
	EstimationGrain.setQuestion();
	EstimationGrain.reset();
	EstimationGrain.nextScreen();
}

EstimationGrain.reset=function()
{
	clearInterval(EstimationGrain.weightInterval);
	clearInterval(EstimationGrain.lineIntreval);
	context.clearRect(0,0,1500,210);
	countWT=110;
	context.beginPath();
	context.moveTo(0,51);
	context.lineTo(0,100);
	context.strokeStyle="red";
	context.stroke();
	ratesIngredient.innerHTML="";
	document.getElementById('bagDiv').style.display="block";
	document.getElementById('pouringHand').style.display="block";
	document.getElementById('startStopDiv').style.display="block";
	document.getElementById('bagDivScreen3').style.display="none";
	document.getElementById('orderBoard').setAttribute("class","noPadding col-xs-5 col-sm-5 col-lg-5 col-md-5 col-xs-offset-1 col-sm-offset-1 col-lg-offset-1 col-md-offset-1");
	document.getElementById('weightDiv').setAttribute("class","noPadding col-xs-4 col-sm-4 col-lg-4 col-md-4");
	document.getElementById('container').setAttribute("class","noPadding col-xs-4 col-sm-4 col-lg-4 col-md-4");
	document.getElementById('customerImgDivScreen2').setAttribute("class","noPadding col-xs-2 col-sm-2 col-lg-2 col-md-2");
	document.getElementById('assistantImgDivScreen2').style.right=0;
}

EstimationGrain.paymentPage=function()
{
	EstimationGrain.temp+=1;
	var actualPayment=Math.floor(parseInt(EstimationGrain.ingredient[EstimationGrain.randomIngredient].price)/1000*parseInt(EstimationGrain.Question[EstimationGrain.randomQue].answer));
	document.getElementById('paymentPageScreen3').style.display="none";
	document.body.style.backgroundImage="";
	document.getElementById("paymentSide1").style.backgroundImage="url('images/general_store_cash_counter.png')";
	payment.style.display="block";
	EstimationPayment.setPayment(actualPayment);
}
