"use strict";

// This controls the Boss Room.
// The Boss Room is also the Boss.
// Which is also a Room.
// Q.E.D.
// Hey... hey Brossentia... you think this boss was a WALL for many players?
var GO_BossRoom = function(defeated){
	this.sprite = new Sprite(null,"BossRoom");
	this.sprite.collision_flags = 0;
	this.sprite.setBoundingBox(0,0,1,1);
	this.sprite.game_object = this; // no longer a generic GO

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

	this.chutes = [];
	this.beams = [];

	this.timer = 0;

	this.active = false;
	this.defeated = defeated;

	this.available_chutes = [];
};

GO_BossRoom.prototype = new GameObject(
	new Sprite(
		null,
		"BossRoom"
	),
	"BossRoom"
	);

	GO_BossRoom.prototype.interactions = 0;

	GO_BossRoom.prototype.setBeams = function(active){
		for (this._heap.i=0; this._heap.i<this.beams.length; ++this._heap.i){
			this.beams[this._heap.i].active = active;
		}
		//Sprite.AGGRESSIVE_CRUSH = active; // arena walls can be clipped through otherwise :P
	}

	GO_BossRoom.prototype.Update = function(){
		if (!this.meli && !this.meliFind){
			this.meli = this.manager.getNamedObject("Meli");

			// also find other boss parts while we're here
			this.manager.getAllNamedObjects("BossRoomChute",this.chutes);
			for (this._heap.i=0; this.defeated && this._heap.i<this.chutes.length; ++this._heap.i){
				this.chutes[this._heap.i].jammed = true;
			}

			this.manager.getAllNamedObjects("Beamwall",this.beams);

			this.meliFind = true;
		}

		if (this.active && !this.defeated){
			++this.timer;
			this.timer &= 15;
			if (!this.timer){
				if (!this.available_chutes.length){
					for (this._heap.i=0; this._heap.i<this.chutes.length; ++this._heap.i){
						if (!this.chutes[this._heap.i].jammed){
							this.available_chutes.push(this.chutes[this._heap.i]);
						}
					}
				}
				if (!this.available_chutes.length){
					this.endFight();
				}
				else{
					this._heap.i = this.meli.direction>0 ? 0 : this.available_chutes.length-1;

					this.available_chutes[this._heap.i].signal(this);

					this.available_chutes.splice(this._heap.i,1);
				}
			}
		}
		
	};

	GO_BossRoom.prototype.endFight = function(){

		this.defeated = true;
		this.setBeams(false);

		// full-room plot trigger (hack)
		this._heap.triggerFn = function(sourceGO){
			// remember, "this" is no longer the BossRoom
			this.sfx = this.sfx || new Chiptune("FX_fallcrash",SDS.sfxchip);

			if (GLOBAL_SCRATCHPAD.check("BOSS_CLEAN")) return "";
			else if (GLOBAL_DIARY.check(4,"BOSS")){
				// boss has been fully beaten, including shutdown dlg - clean up stray blocks
				this._heap.block = this.manager.getNamedObject("Boulder");
				if (this._heap.block==null){
					SDS.switchBgm("Forged in Steel",true);
					GLOBAL_SCRATCHPAD.note("BOSS_CLEAN",true);
					if (this.sfx) this.sfx.stop();
					return "";
				}
				if (this._heap.block.held) this.manager.getNamedObject("Boulder");
				if (!this._heap.block.held){
					this._heap.poof = new GO_Poof("");
					this._heap.poof.spawnOnSprite(this._heap.block.sprite,this.manager);
					this._heap.poof = null;

					if (!this.sfx.is_playing) this.sfx.play(true);

					this._heap.block.manager.remove(this._heap.block);
				}

				return "";
			}
			else if (GLOBAL_SCRATCHPAD.check("BOSS_FINISH")) return "";

			GLOBAL_SCRATCHPAD.note("BOSS_FINISH",true);

			this._heap.blocks = [];
			this.manager.getAllNamedObjects("Boulder",this._heap.blocks);
			for (this._heap.i=0; this._heap.i<this._heap.blocks.length; ++this._heap.i){
				if (!this._heap.blocks[this._heap.i].held){
					this._heap.blocks[this._heap.i].interactions &= ~DEFINE_GAMEOBJECT_INTERACTIONS.GRAB;
				}
			}
			this._heap.blocks = null;

			return "BossRoom_end";
		};

		this._heap.trig = new GO_Trigger(this.sprite.map_room.pixel_width,this.sprite.map_room.pixel_height,this._heap.triggerFn);
		this._heap.trig.sprite.x = 0;
		this._heap.trig.sprite.y = 0;

		this.sprite.map_room.addSprite(this._heap.trig.sprite);
		this.manager.add(this._heap.trig);
		this._heap.trig = null;
		this._heap.triggerFn = null;
	};

