"use strict";
/* A constant companion within the goddess catacombs */
var GO_MirrorImage = function(){
	this.sprite = new Sprite(GO_MirrorImage.spriteSheet,"MirrorImage");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE |
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;
	this.sprite.setBoundingBox(2,1,13,15);
	this.sprite.game_object = this; // no longer a generic GO

	this.controls = new Controls(); // just to init

	this.horizontal_move_index=0;
	this.vertical_move_index = 0;
	this.anim_state = this.ANIM_STATES.bob;
	this.frame_timer=0;

	// note the timing of this:
	// to save on local vars, shouldDeferUpdate flips this and then returns it
	// so if it's _true_, the defer will NOT happen next frame
	// but if it's _false_ the defer WILL happen.
	// Update sets it back to false.
	// defer is needed so we always mirror Meli's actual-result-of-this-frame state.
	this.defer_toggle = false;

	this.direction = 1; // +/-1 for facing

	this._heap.collisions = [];

	this.meli = null;

	this.blessingOverlay = new Sprite(GO_Meli.blessingSheet,"Meli_blessing");
	this.blessingOverlay.game_object = this;

};
GO_MirrorImage.spriteImg = SDS_Load_Image("art/sheets/sprite/mirrorimage.png");
GO_MirrorImage.spriteSheet = new TileSheet(4, 4, 3, 3, GO_MirrorImage.spriteImg);

GO_MirrorImage.prototype = new GameObject(
	new Sprite(
		GO_MirrorImage.spriteSheet,
		"MirrorImage"
	),
	"MirrorImage"
	);

	// need to not take the CHECK interaction since we're generally first in the pool
	// and will block checking other things
	GO_MirrorImage.prototype.interactions = 
		DEFINE_GAMEOBJECT_INTERACTIONS.CONTROL;// |
		//DEFINE_GAMEOBJECT_INTERACTIONS.CHECK;

	GO_MirrorImage.prototype.leftward_move_water = [0,-1,-2];
	GO_MirrorImage.prototype.rightward_move_water = [0,1,2];
	GO_MirrorImage.prototype.upward_move_water = [0,-1,-2];
	GO_MirrorImage.prototype.downward_move_water = [0,1,2];
	GO_MirrorImage.prototype.ANIM_STATES = {
		"bob" : 0,
		"swim" : 1,
		"out" : 2,
		"land" : 3
	};
	GO_MirrorImage.prototype.anim_frames = [
		[0,0,1,1], //"bob" : 
		[0,1,0,2], //"swim" :  
	];

	GO_MirrorImage.prototype.addToRoom = function(mapRoom){
		mapRoom.addSprite(this.sprite);
		mapRoom.addSprite(this.blessingOverlay);
	}

	GO_MirrorImage.prototype.checkEvent = function(sourceGO){
		if (this.controls.held.A) return null;
		if (sourceGO && sourceGO.sprite) this.direction = (sourceGO.sprite.x<this.sprite.x) ? -1 : 1;
		
		return Dialogue_getCurrentEvent[this.name]();

		return null;
	}

	GO_MirrorImage.prototype.shouldDeferUpdate = function(){
		this.defer_toggle = !this.defer_toggle;
		return this.controls.held.A && this.defer_toggle;
	}

	GO_MirrorImage.prototype.Update = function(){

		this._heap.oldAnimState = this.anim_state;

		this._heap.i=0;

		if (this.controls.held.A){
			this.interactions &= ~DEFINE_GAMEOBJECT_INTERACTIONS.CHECK;
			// mirror behavior
			if (!this.meli) this.meli=this.manager.getNamedObject("Meli");

			if (this.meli && this.sprite.map_room){
				this._heap.offcenter = {
					x: this.meli.sprite.x + this.meli.sprite.center.x - (this.sprite.map_room.pixel_width>>1),
					y: this.meli.sprite.y + this.meli.sprite.center.y - (this.sprite.map_room.pixel_height>>1),
				};
				// prorate offset (oldschool method)
				this._heap.offcenter.x -= (this._heap.offcenter.x>>5);
				this._heap.offcenter.y -= (this._heap.offcenter.y>>5);

				this.sprite.x = (this.sprite.map_room.pixel_width>>1) - this.sprite.center.x + this._heap.offcenter.x;
				this.sprite.y = this.meli.sprite.y;//(this.sprite.map_room.pixel_height>>1) - this.sprite.center.y + this._heap.offcenter.y;

				this.sprite.frame = this.meli.sprite.frame;

				this.blessingOverlay.visible = false;

				//++this.frame_timer;
				//this.sprite.visible = (this.frame_timer&1);
				// TODO if needed: force-hide if on a standing frame (8-15, 40-47)
			}
		}
		else{
			// normal control

			this.interactions |= DEFINE_GAMEOBJECT_INTERACTIONS.CHECK;

			this.NPCSwim();


			if (this._heap.oldAnimState != this.anim_state){
				this.frame_timer=0;
			}
			++this.frame_timer;
			this._heap.aniFrame = (this.frame_timer>>2) % this.anim_frames[this.anim_state].length;

			this.sprite.frame = this.anim_frames[this.anim_state][this._heap.aniFrame];

			this.sprite.frame = this.sprite.frame & 7;
			if (this.controls.held.B) this.sprite.frame += 4
			if (this.direction<0) this.sprite.frame+=32;

			// because we needed to shift the "main" sprites to an unused row
			this.sprite.frame += 8;

			this.blessingOverlay.x = this.sprite.x;
			this.blessingOverlay.y = this.sprite.y;
			this.blessingOverlay.frame = this.sprite.frame-8; // countershift
			this.blessingOverlay.visible = true;
		}

		this.defer_toggle = false;
	}

