"use strict";

var Meters = function(){
	var metersImg = SDS_Load_Image("art/sheets/interface/meters.png");
	this.tileSheet = new TileSheet(3, 3, 4, 2, metersImg);

	// we'd ~like~ to init from diary, but we may load before it...
	var diary = (typeof(GLOBAL_DIARY)!='undefined' && GLOBAL_DIARY) ? GLOBAL_DIARY : null;
	this.displayed_life = diary ? diary.life : 0;
	this.displayed_lmax = diary ? diary.max_life : 0;
	this.displayed_chi = diary ? diary.chi : 0;
	this.displayed_cmax = diary ? diary.max_chi : 0;

	this.meterCanvas = document.createElement('canvas');
	this.meterCanvas.width = 16<<3;
	this.meterCanvas.height = 2<<3;
	this.meterCanvas.getContext('2d').clearRect(0,0,this.meterCanvas.width,this.meterCanvas.height);

	this.timerCanvas = document.createElement('canvas');
	this.timerCanvas.width = 20<<3;
	this.timerCanvas.height = 1<<3;
	this.timerCanvas.getContext('2d').clearRect(0,0,this.timerCanvas.width,this.timerCanvas.height);

	this.timerBlinkFrame = 0;

	this.visible = true;
};

	Meters.prototype._redrawLife = function(){
		var ctx = this.meterCanvas.getContext('2d');
		ctx.clearRect(0,0,this.meterCanvas.width,8);
		this.tileSheet.drawIntoCanvas(this.meterCanvas,15,0,0);
		this.tileSheet.drawIntoCanvas(this.meterCanvas,10,4<<3,0);
		var x=3;
		var remaining = this.displayed_life;
		while (remaining>0){
			var digit = remaining%10;
			this.tileSheet.drawIntoCanvas(
				this.meterCanvas,
				digit,
				x<<3,0);
			remaining = (remaining-digit)/10;
			x--;
		}
		x=5;
		remaining = this.displayed_lmax;
		while (remaining>=10) {
			++x;
			remaining /= 10;
		}
		remaining = this.displayed_lmax;
		while (remaining>0){
			var digit = remaining%10;
			this.tileSheet.drawIntoCanvas(
				this.meterCanvas,
				digit,
				x<<3,0);
			remaining = (remaining-digit)/10;
			x--;
		}
	};

	Meters.prototype._redrawChi = function(){
		var ctx = this.meterCanvas.getContext('2d');
		ctx.clearRect(0,8,this.meterCanvas.width,8);

		var x=0;
		var remaining = this.displayed_cmax;
		while (remaining>0){
			this.tileSheet.drawIntoCanvas(
				this.meterCanvas,
				16 + (remaining>8 ? 8 : remaining),
				x<<3,8);
			remaining = remaining-8;
			x++;
		}

		x=0;
		remaining = GLOBAL_DIARY.blessingActive() ? this.displayed_cmax : this.displayed_chi;
		while (remaining>0){
			this.tileSheet.drawIntoCanvas(
				this.meterCanvas,
				32 + (remaining>8 ? 8 : remaining),
				x<<3,8);
			remaining = remaining-8;
			x++;
		}

		if (GLOBAL_DIARY.blessingActive()){
			x=0;
			remaining = this.displayed_chi;
			while (remaining>0){
				this.tileSheet.drawIntoCanvas(
					this.meterCanvas,
					48 + (remaining>8 ? 8 : remaining),
					x<<3,8);
				remaining = remaining-8;
				x++;
			}
		}
	};

	Meters.prototype._redrawTime = function(){
		var ctx = this.timerCanvas.getContext('2d');
		ctx.clearRect(0,0,this.timerCanvas.width,8);

		if (GLOBAL_TIME_IN){
			this.timerBlinkFrame++;
			this.timerBlinkFrame&=15;
			if (!(this.timerBlinkFrame&8)) return;
		}

		var position = 19;
		var remainder = GLOBAL_TIME_FRAMES;
		var digit = remainder%10;
		remainder = (remainder-digit)/10;

		this.tileSheet.drawIntoCanvas(
			this.timerCanvas,
			digit,
			position<<3,0);
		position--;
		digit = remainder;
		this.tileSheet.drawIntoCanvas(
			this.timerCanvas,
			digit,
			position<<3,0);
		position--;
		digit = 11;
		this.tileSheet.drawIntoCanvas(
			this.timerCanvas,
			digit,
			position<<3,0);
		position--;


		remainder = GLOBAL_TIME_SECONDS;
		digit = remainder%10;
		remainder = (remainder-digit)/10;

		this.tileSheet.drawIntoCanvas(
			this.timerCanvas,
			digit,
			position<<3,0);
		position--;
		digit = remainder;
		this.tileSheet.drawIntoCanvas(
			this.timerCanvas,
			digit,
			position<<3,0);
		position--;
		digit = 11;
		this.tileSheet.drawIntoCanvas(
			this.timerCanvas,
			digit,
			position<<3,0);
		position--;

		remainder = GLOBAL_TIME_MINUTES;
		digit = remainder%10;
		remainder = (remainder-digit)/10;

		this.tileSheet.drawIntoCanvas(
			this.timerCanvas,
			digit,
			position<<3,0);
		position--;
		digit = remainder;
		this.tileSheet.drawIntoCanvas(
			this.timerCanvas,
			digit,
			position<<3,0);
		position--;
		digit = 11;
		this.tileSheet.drawIntoCanvas(
			this.timerCanvas,
			digit,
			position<<3,0);
		position--;

		remainder = GLOBAL_TIME_HOURS;
		digit = remainder%10;
		do {
			remainder = (remainder-digit)/10;

			this.tileSheet.drawIntoCanvas(
				this.timerCanvas,
				digit,
				position<<3,0);
			position--;
			digit = remainder;
		} while (remainder>0)

		if (GLOBAL_TIME_IN){
			position--;
			digit = 12;
			this.tileSheet.drawIntoCanvas(
				this.timerCanvas,
				digit,
				position<<3,0);
			position--;

			remainder = GLOBAL_COMPLETION;
			digit = remainder%10;
			do {
				remainder = (remainder-digit)/10;

				this.tileSheet.drawIntoCanvas(
					this.timerCanvas,
					digit,
					position<<3,0);
				position--;
				digit = remainder;
			} while (remainder>0)
		}

	};

	Meters.prototype.forceRedraw = function(){
		this._redrawLife();
		this._redrawChi();

		if (GLOBAL_SPEEDRUN){
			this._redrawTime();
		}
	};

	Meters.prototype.Update = function(){
		if (GLOBAL_DIARY.life != this.displayed_life || GLOBAL_DIARY.max_life != this.displayed_lmax){
			this.displayed_life = GLOBAL_DIARY.life;
			this.displayed_lmax = GLOBAL_DIARY.max_life;
			this._redrawLife();
		}
		if (GLOBAL_DIARY.chi != this.displayed_chi || GLOBAL_DIARY.max_chi != this.displayed_cmax){
			this.displayed_chi = GLOBAL_DIARY.chi;
			this.displayed_cmax = GLOBAL_DIARY.max_chi;
			this._redrawChi();
		}
		if (GLOBAL_SPEEDRUN){
			this._redrawTime();
		}
	};

	Meters.prototype.drawIntoCanvas = function(canvas){
		var ctx = canvas.getContext('2d');
		if (this.visible) ctx.drawImage(this.meterCanvas,8,8);

		ctx.drawImage(this.timerCanvas,216,8);
	};
