"use strict";
/* Enjoys tea.  And making things complicated. */
var GO_grey = function(location){
	this.sprite = new Sprite(GO_grey.spriteSheet,"grey");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE |
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;
	this.sprite.setBoundingBox(2,0,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.hidden;
	this.frame_timer=0;

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

	this.audience = null;
	this.hidden = true;
	this.origX = 0;
	this.origY = 0;

	this.tea = null;

	this.location=location;
};
GO_grey.spriteImg = SDS_Load_Image("art/sheets/sprite/grey.png");
GO_grey.spriteSheet = new TileSheet(4, 4, 2, 2, GO_grey.spriteImg);

GO_grey.prototype = new GameObject(
	new Sprite(
		GO_grey.spriteSheet,
		"grey"
	),
	"grey"
	);

	GO_grey.prototype.interactions = 
		DEFINE_GAMEOBJECT_INTERACTIONS.CONTROL |
		DEFINE_GAMEOBJECT_INTERACTIONS.CHECK;

	GO_grey.prototype.leftward_move = [0,-1];
	GO_grey.prototype.rightward_move = [0,1];
	GO_grey.prototype.upward_move = [0,-1];
	GO_grey.prototype.downward_move = [0,1];
	GO_grey.prototype.ANIM_STATES = {
		"hidden" : 0,
		"enter" : 1,
		"stand" : 2,
		"tea" : 3,
		"exit" : 4
	};
	GO_grey.prototype.anim_frames = [
		[0], //"hidden" : 
		[0,0,0,0,1,2,3,4,5,6,7,8], //"enter" : 
		[8], //"stand" :
		[9], //"tea" :
		[8,7,6,5,4,3,2,1,0], //"exit" : 
	];

	GO_grey.prototype.checkEvent = function(sourceGO){
		if (GLOBAL_DIARY.check(0,"GREY_LOC_"+this.location)){
			return "GD_grey_essence_"+(GLOBAL_DIARY.greyCount()-1);
		}

		if (sourceGO && sourceGO.sprite){
			this.direction = (sourceGO.sprite.x<this.sprite.x) ? -1 : 1;
			this.audience = sourceGO;
			this.origX = this.sprite.x;
			this.origY = this.sprite.y;
		}

		if (this.location=="ARENA"){
			this.audience = null;
			if (!GLOBAL_SCRATCHPAD.check("GREY_LINE")){
				return "grey_challenge";
			}
			else{
				// mock typical NPC line advance logic
				GLOBAL_SCRATCHPAD.note("GREY_LINE",Math.min(GLOBAL_SCRATCHPAD.check("GREY_LINE")+1,7)); // 7 or max de facto index in data + 1
				return "GD_grey_challenge_"+(GLOBAL_SCRATCHPAD.check("GREY_LINE")-1);
			}
		}
		else{
			this.hidden=false;
			this.anim_state=this.ANIM_STATES.enter;
			this.frame_timer=0;
			
			return "grey";
		}
	};

	GO_grey.prototype.Update = function(){
		this._heap.oldAnimState = this.anim_state;

		if (this.audience && !this.hidden){
			// assume an equal-sized audience meli
			// since positioning must be pixel-precise
			this.controls.held.up=(this.audience.sprite.y<this.sprite.y);
			this.controls.held.down=(this.audience.sprite.y>this.sprite.y);
			this.controls.held.left=(this.audience.sprite.x<(this.sprite.x+this.direction*24));
			this.controls.held.right=(this.audience.sprite.x>(this.sprite.x+this.direction*24));
		}

		this.SimpleMove();

		if (this.tea){
			this.tea.sprite.x = this.sprite.x+((this.direction>0)?4:-20);
			this.tea.sprite.y = this.sprite.y;
		}
		
		if (this.controls.pressed.B){
			if (!this.tea){
				this.tea = new GO_Teaset();
				this.sprite.map_room.addSprite(this.tea.sprite);
				this.manager.add(this.tea);
			}
			this.tea.Show();
		}
		if (this.controls.held.B){
			this.anim_state=this.ANIM_STATES.tea;
			this.frame_timer=0;
		}
		if (this.controls.released.B && this.tea){
			this.tea.Clear();
		}
		if (this.controls.pressed.A){
			this.anim_state = this.hidden ? this.ANIM_STATES.enter : this.ANIM_STATES.exit;
			this.hidden = !this.hidden;
			this.frame_timer=-1;
		}

		//if (this._heap.oldAnimState != this.anim_state){
		//	this.frame_timer=0;
		//}
		if (this.anim_state==this.ANIM_STATES.enter || this.anim_state==this.ANIM_STATES.exit){
			this.frame_timer++;
			this._heap.aniFrame = (this.frame_timer>>1);
			if (this._heap.aniFrame>=this.anim_frames[this.anim_state].length){
				this.anim_state = (this.anim_state==this.ANIM_STATES.enter) ? this.ANIM_STATES.stand : this.ANIM_STATES.hidden;
				this.frame_timer=0;
				this._heap.aniFrame=0;
				if (this.anim_state==this.ANIM_STATES.hidden){
					this.sprite.x = this.origX;
					this.sprite.y = this.origY;
				}
			}
			
			this.sprite.frame = this.anim_frames[this.anim_state][this._heap.aniFrame];
		}
		else{
			this._heap.aniFrame = this.frame_timer;
			this.sprite.frame = this.anim_frames[this.anim_state][this._heap.aniFrame];
			//this.sprite.frame = this.sprite.frame & 7;
			if (this.direction<0) this.sprite.frame+=4;
		}
		
		this.sprite.visible=(this.anim_state!=this.ANIM_STATES.hidden);

		if (!this.sprite.visible && this.tea){
			this.tea.sprite.map_room.removeSprite(this.tea.sprite);
			this.tea.manager.remove(this.tea);
			this.tea=null;
		}

	};
