Number.prototype.toStringDayIndex ??= function(){
	switch(this){
		case 0:	return 'Today';
		case 1: return 'Tomorrow';
		case -1:return 'Yesterday';
	}
	if(this > 1)
		return 'In '+this+'days';
	if(this < 1)
		return -1*this+' days ago'
}

window.energyHistoryDraw = function(domElementId){
	jUIPromise.then(function () {
			google.charts.load('current', {'packages':['corechart']});

			google.charts.setOnLoadCallback(drawEnergyChart);


			function drawEnergyChart() {
				let pc = State.variables.pc;
				let rawdata = [['Day', 'Energy consumed (kcal)', 'EnergyRequired (kcal)']];
				let energyHistory = pc.energyHistory;

				let kcalFactor = 24;

				let i = -1 * energyHistory.length;
				for(let energyHistoryStep of energyHistory){
					rawdata.push([(i++).toStringDayIndex(),energyHistoryStep.energy*kcalFactor,energyHistoryStep.energyRequirement*kcalFactor]);
				}

				rawdata.push([(0).toStringDayIndex(),pc.energy*kcalFactor,pc.energyRequirement*kcalFactor]);
				

				var data = google.visualization.arrayToDataTable(rawdata);

				var options = {
					title: 'Energy',
					hAxis: {title: 'Day'},
					vAxis: {title: 'Energy (kcal)'},
					legend: {position: 'top'},
					series: {
						0: { pointShape: 'diamond',pointSize: 10, },
						1: { pointShape: 'diamond',pointSize: 10, }
					}

				};

				var chart = new google.visualization.LineChart(document.getElementById(domElementId));
				chart.draw(data, options);
			}
	});
}

window.energyBalanceHistoryDraw = function(domElementId){
	jUIPromise.then(function () {
			google.charts.load('current', {'packages':['corechart']});

			google.charts.setOnLoadCallback(drawEnergyHistoryChart);


			function drawEnergyHistoryChart() {
				let pc = State.variables.pc;
				let rawdata = [['Day', 'Change', 'New Balance']];
				let energyHistory = pc.energyHistory;

				let i = -1 * energyHistory.length;
				for(let energyHistoryStep of energyHistory){
					rawdata.push([(i++).toStringDayIndex(),energyHistoryStep.balanceChange,energyHistoryStep.balanceNew]);
				}

				rawdata.push([(0).toStringDayIndex(),undefined,undefined]);

				

				var data = google.visualization.arrayToDataTable(rawdata);

				var options = {
					title: 'Energy Balance',
					hAxis: {title: 'Day'},
					vAxis: {title: 'Energy Balance'},
					legend: {position: 'top'},
					series: {
						0: { pointShape: 'diamond',pointSize: 10, },
						1: { pointShape: 'diamond',pointSize: 10, }
					}

				};

				var chart = new google.visualization.LineChart(document.getElementById(domElementId));
				chart.draw(data, options);
			}
	});
}

window.energyBMIHistoryDraw = function(domElementId){
	jUIPromise.then(function () {
			google.charts.load('current', {'packages':['corechart']});

			google.charts.setOnLoadCallback(drawEnergyBMIChart);


			function drawEnergyBMIChart() {
				let pc = State.variables.pc;
				let rawdata = [['Day', 'Change', 'New BMI']];
				let energyHistory = pc.energyHistory;

				let i = -1 * energyHistory.length;
				for(let energyHistoryStep of energyHistory){
					rawdata.push([(i++).toStringDayIndex(),energyHistoryStep.bmiChange,energyHistoryStep.bmiNew]);
				}

				rawdata.push([(0).toStringDayIndex(),undefined,undefined]);

				

				var data = google.visualization.arrayToDataTable(rawdata);

				var options = {
					title: 'BMI',
					hAxis: {title: 'Day'},
					vAxes: {
						0: {label: 'Change'},
						1: {label: 'New Value'}
					},
					legend: {position: 'top'},
					series: {
						0: { pointShape: 'diamond',pointSize: 10, targetAxisIndex: 0 },
						1: { pointShape: 'diamond',pointSize: 10, targetAxisIndex: 1 }
					},
					


				};

				var chart = new google.visualization.LineChart(document.getElementById(domElementId));
				chart.draw(data, options);
			}
	});
}

window.energyWeightHistoryDraw = function(domElementId){
	jUIPromise.then(function () {
			google.charts.load('current', {'packages':['corechart']});

			google.charts.setOnLoadCallback(drawEnergyWeightChart);


			function drawEnergyWeightChart() {
				let pc = State.variables.pc;
				let weightFactor = pc.pcs_hgt*pc.pcs_hgt/10000;
				let rawdata = [['Day', 'Weight']];
				let energyHistory = pc.energyHistory;

				let i = -1 * energyHistory.length;
				for(let energyHistoryStep of energyHistory){
					rawdata.push([(i++).toStringDayIndex(),energyHistoryStep.bmiNew*weightFactor]);
				}

				rawdata.push([(0).toStringDayIndex(),pc.bmi*weightFactor]);

				

				var data = google.visualization.arrayToDataTable(rawdata);

				var options = {
					title: 'Weight',
					hAxis: {title: 'Day'},
					vAxis: {title: 'Weight (kg)'},
					legend: {position: 'top'},

					series: {
						0: { pointShape: 'diamond',pointSize: 10, targetAxisIndex: 0 }
					},
					


				};

				var chart = new google.visualization.LineChart(document.getElementById(domElementId));
				chart.draw(data, options);
			}
	});
}