"use strict";

// Two figures from the Gargoyle Conflict, one oddly familiar
var GO_KittyMeter = function(){
	//this.sprite = new Sprite(GO_KittyMeter.spriteSheet,"KittyMeter");
	//this.sprite.collision_flags = 0;
	//this.sprite.setBoundingBox(0,0,7,7);
	//this.sprite.game_object = this; // no longer a generic GO

	this.name = "KittyMeter";

	this.frames = 0;
	this.seconds = 0;
	this.minutes = 0;
	this.blink = 0;

	this.meli = null;
	this.meliFind=false; // she probably won't be in the map when we init; hopefully when we update
	
	this.kitties = [];
	this.kittyFind = false;

	this.floof = null;
	this.floofFind = false;

	this.gate = null;
	this.gateFind = false;

	this.done = false;

	this.sprite.frame = 31;
	this.row1 = [];
	this.row2 = [];

	this._heap.petted = 0;
	this._heap.i = 0;
	this._heap.x = 0;
	this._heap.y = 0;
	this._heap.wasDone = false;

	for (this._heap.i=0; this._heap.i<4; ++this._heap.i){
		this._heap.sprite = new Sprite(GO_KittyMeter.spriteSheet,"KittyMeter");
		this._heap.sprite.collision_flags = 0;
		this._heap.sprite.setBoundingBox(0,0,7,7);
		this._heap.sprite.game_object = this; // no longer a generic GO
		this._heap.sprite.frame = 31;
		this.row1.push(this._heap.sprite);
	}
	for (this._heap.i=0; this._heap.i<8; ++this._heap.i){
		this._heap.sprite = new Sprite(GO_KittyMeter.spriteSheet,"KittyMeter");
		this._heap.sprite.collision_flags = 0;
		this._heap.sprite.setBoundingBox(0,0,7,7);
		this._heap.sprite.game_object = this; // no longer a generic GO
		this._heap.sprite.frame = 63;
		this.row2.push(this._heap.sprite);
	}
	this._heap.sprite = null;

	this.row1[2].frame = 10;
	this.row2[2].frame = 11;
	this.row2[5].frame = 11;

	this.evt = null;
};
GO_KittyMeter.spriteImg = SDS_Load_Image("art/sheets/interface/meters.png");
GO_KittyMeter.spriteSheet = new TileSheet(3, 3, 4, 2, GO_KittyMeter.spriteImg);

GO_KittyMeter.prototype = new GameObject(
	new Sprite(
		GO_KittyMeter.spriteSheet,
		"KittyMeter"
	),
	"KittyMeter"
	);

	GO_KittyMeter.prototype.interactions = 0;

	// not interacted normally,
	// but will be injecting ourselves as a checked object
	// to trigger alerts
	GO_KittyMeter.prototype.checkEvent = function(sourceGO){
		return this.evt;//"GD_ultimate_challenge_mid";
	};

	GO_KittyMeter.prototype.Update = function(){
		if (!this.meli && !this.meliFind){
			this.meli = this.manager.getNamedObject("Meli");
			this.meliFind = true;
		}
		if (this.kitties.length==0 && !this.kittyFind){
			this.manager.getAllNamedObjects("Kitty",this.kitties);
			this.kittyFind = true;
		}
		if (!this.floof && !this.floofFind){
			this.floof = this.manager.getNamedObject("TheGloriousFloof");
			this.floofFind = true;
		}
		if (!this.gate && !this.gateFind){
			this.gate = this.manager.getNamedObject("Sealwall_KITTY");
			this.gateFind = true;
		}

		this._heap.petted = 0;
		for (this._heap.i=0; this._heap.i<this.kitties.length; ++this._heap.i){
			this._heap.petted += this.kitties[this._heap.i].petted ? 1 : 0;
		}

		// split off old state so we can redraw completion if it just finished
		// but only tick time if still going
		this._heap.wasDone = this.done;
		this.done = (this._heap.petted >= this.kitties.length) && (!this.floof || this.floof.glomped);

		// not a great place for this, but better than making a dedicated watcher object
		if (this.gate!=null && this._heap.petted >= this.kitties.length && !this.gate.destructing){
			this.gate.Destruct();
			
			// hack to trigger an event/dialogue
			this.evt = "GD_ultimate_challenge_mid";
			if (this.meli) this.meli.checking = this;
		}

		if (!this.done){
			++this.frames;
			if (this.frames>=30){
				++this.seconds;
				this.frames=0;
			}
			if (this.seconds>=60){
				++this.minutes;
				this.seconds=0;
			}
		}

		if (!this._heap.wasDone){
			// There SHOULD be 10 kitties.
			if (this._heap.petted < this.kitties.length || !this.floof){
				this.row1[0].frame = 31;
				this.row1[1].frame = (
					this._heap.petted<10 ? this._heap.petted : 
					this._heap.petted==10 ? 14 : 12
					);
				
				this.row1[3].frame = (
					this.kitties.length<10 ? this.kitties.length : 
					this.kitties.length==10 ? 14 : 12
					);
			}
			else if (this.floof){
				this.row1[0].frame = 47;
				//this.row1[1].frame = (
				//	this.floof.glomped ? 15 :
				//	this.floof.glompage<10 ? this.floof.glompage : 
				//	this.floof.glompage==10 ? 14 : 12
				//	);
				this.row1[1].frame = (
					this.floof.glomped ? 15 :
					this.floof.glompage%10
					);
				this.row1[3].frame = 15;
			}

			this.row2[0].frame = (this.minutes<100 ? Math.floor(this.minutes/10) : 12);
			this.row2[1].frame = (this.minutes<100 ? this.minutes%10 : 12);
			
			this.row2[3].frame = (Math.floor(this.seconds/10));
			this.row2[4].frame = (this.seconds%10);
			
			this.row2[6].frame = (Math.floor(this.frames/10));
			this.row2[7].frame = (this.frames%10);
		}
		else{
			++this.blink;
			this.blink &= 15;
		}
		
		if (!this.meli) return;

		this._heap.y = this.meli.sprite.y - 24;
		this._heap.x = this.meli.sprite.x+this.meli.sprite.center.x - (this.row1.length<<2); // <<3 then /2
		for (this._heap.i=0; this._heap.i<this.row1.length; ++this._heap.i){
			this.row1[this._heap.i].x = this._heap.x;
			this.row1[this._heap.i].y = this._heap.y;
			this._heap.x += 8;
		}

		this._heap.y = this.meli.sprite.y - 16;
		this._heap.x = this.meli.sprite.x+this.meli.sprite.center.x - (this.row2.length<<2); // <<3 then /2
		for (this._heap.i=0; this._heap.i<this.row2.length; ++this._heap.i){
			this.row2[this._heap.i].x = this._heap.x;
			this.row2[this._heap.i].y = this._heap.y;
			this._heap.x += 8;

			this.row2[this._heap.i].visible = (!this.done || (this.blink & 8));
		}
	};

	GO_KittyMeter.prototype.addToRoom = function(mapRoom){
		//mapRoom.addSprite(this.sprite);
		for (this._heap.i = 0; this._heap.i<this.row1.length; ++this._heap.i){
			mapRoom.addSprite(this.row1[this._heap.i]);
		}
		for (this._heap.i = 0; this._heap.i<this.row2.length; ++this._heap.i){
			mapRoom.addSprite(this.row2[this._heap.i]);
		}
	}
	GO_KittyMeter.prototype.clearFromRoom = function(mapRoom){
		//mapRoom.removeSprite(this.sprite);
		
		for (this._heap.i = 0; this._heap.i<this.row1.length; ++this._heap.i){
			mapRoom.removeSprite(this.row1[this._heap.i]);
		}
		for (this._heap.i = 0; this._heap.i<this.row2.length; ++this._heap.i){
			mapRoom.removeSprite(this.row2[this._heap.i]);
		}
	}

	// very atypical to override this one, but not illegal.
	// except... this is never used -_-;
	// will need to make a million normal sprites
	/*GO_KittyMeter.prototype.drawIntoCanvas = function(canvas){
		//this.sprite.drawIntoCanvas(canvas);
		if (!this.meli) return;

		this.sprite.y = this.meli.sprite.y - 16;
		this.sprite.x = this.meli.sprite.x+this.meli.sprite.center.x - (this.row1.length<<2); // <<3 then /2
		for (this._heap.i=0; this._heap.i<this.row1.length; ++this._heap.i){
			this.sprite.frame = this.row1[this._heap.i];
			this.sprite.drawIntoCanvas(canvas);
			this.sprite.x += 8;
		}

		if (!this.done || (this.blink & 8)){
			this.sprite.y = this.meli.sprite.y - 8;
			this.sprite.x = this.meli.sprite.x+this.meli.sprite.center.x - (this.row2.length<<2); // <<3 then /2
			for (this._heap.i=0; this._heap.i<this.row2.length; ++this._heap.i){
				this.sprite.frame = this.row2[this._heap.i];
				this.sprite.drawIntoCanvas(canvas);
				this.sprite.x += 8;
			}
		}
	}
	*/
