"use strict";

DATA_SDS_SCRIPTED_EVENTS["grey_challenge"] = {};
var evt = DATA_SDS_SCRIPTED_EVENTS["grey_challenge"];
var t=0;
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				if (cacheVars.meli){
					// this is a multi-dialog event; we want to lock down movement between them
					cacheVars.controls = cacheVars.meli.controls;
					cacheVars.meli.controls = new Controls();
				}

				cacheVars.grey = cacheVars.objMgr.getNamedObject("grey");
				cacheVars.wall = cacheVars.objMgr.getNamedObject("Sealwall_CHALLENGE");

				cacheVars.bgmName = SDS.bgmName;
				if (SDS.bgm) SDS.bgm.stop();
				SDS.bgm = new Chiptune("Writing a Character",SDS.bgmchip);
				SDS.bgm.play(true);

				if (!GLOBAL_DIARY.check(0,"GREY_CHALLENGE") && DATA_SDS_DIALOGUE["grey_challenge_1"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("grey_challenge_1",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							if (retval==0) cacheVars.frameCounter=99; // escape out
						}
					});
				}
				else if (GLOBAL_DIARY.check(0,"GREY_CHALLENGE") && DATA_SDS_DIALOGUE["grey_re-challenge"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("grey_re-challenge",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							if (retval==0) cacheVars.frameCounter=99; // escape out
							else cacheVars.frameCounter+=3;
						}
					});
				}
			},
			controls : {
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["grey_challenge_2"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("grey_challenge_2",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							if (retval==0) cacheVars.frameCounter=99; // escape out
							if (retval==-1) cacheVars.frameCounter=199; // skip to BAD end
						}
					});
				}
			},
			controls : {
			},
			customPost : noop
		};
evt[++t] = DlgEvent("grey_challenge_3");
evt[++t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["grey_challenge_4"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("grey_challenge_4",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							if (retval==0) cacheVars.frameCounter=99; // escape out
						}
					});
				}
			},
			controls : {
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				SDS.bgmName = "Ready - Pet - Go!";
				SDS.bgm.stop();
				SDS.bgm = new Chiptune(SDS.bgmName,SDS.bgmchip);
				SDS.bgm.play(true);

				if (cacheVars.meli){
					cacheVars.meli.controls = cacheVars.controls;
					delete cacheVars.controls;

					cacheVars.meter = new GO_KittyMeter();
					cacheVars.meter.addToRoom(cacheVars.meli.sprite.map_room);
					cacheVars.objMgr.add(cacheVars.meter);
					delete cacheVars.meter;
				}

				if (cacheVars.wall) cacheVars.wall.Destruct();

				if (cacheVars.grey){
					cacheVars.grey.interactions &= ~DEFINE_GAMEOBJECT_INTERACTIONS.CHECK;
					// and will tap A to prompt a hide.
					// relocation/show will occur on win.
					GLOBAL_SCRATCHPAD.note("GREY_LINE",5); // set up for ending
				}
				
			},
			controls : {
				"grey" : {A: true}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["ultimate_challenge_start"]){
					//cacheVars.wait=true;
					cacheVars.dlgMgr.open("ultimate_challenge_start",function(index,retval){
						//if (retval!=null){
						//	cacheVars.wait=false; // yay js scoping
						//}
					});
				}
				// can't skip here or we'll miss our controls index
				//cacheVars.frameCounter=203; // skip to end
			},
			controls : {
				"grey" : {A: false}
			},
			customPost : function(cacheVars){
				cacheVars.frameCounter=203; // skip to end
			},
		};


// escape
t=100;
evt[t] = {
			customPre : function(cacheVars){
				SDS.bgmName = cacheVars.bgmName;
				SDS.bgm.stop();
				SDS.bgm = new Chiptune(SDS.bgmName,SDS.bgmchip);
				SDS.bgm.play(true);

				if (cacheVars.meli){
					cacheVars.meli.controls = cacheVars.controls;
					delete cacheVars.controls;
				}
				cacheVars.frameCounter=203; // skip to end
			},
			controls : {},
			customPost : noop
		};


// BAD END
t=200;
evt[t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["grey_prepare"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("grey_prepare",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							if (retval==0) cacheVars.frameCounter=99; // escape out
						}
					});
				}
			},
			controls : {
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (cacheVars.meli){
					cacheVars.meli.overrideFrame = 30; // empty

					cacheVars.poof = new GO_Poof("FX_splat");
					cacheVars.poof.spawnOnSprite(cacheVars.meli.sprite,cacheVars.objMgr);
					delete cacheVars.poof;
				}

				if (DATA_SDS_DIALOGUE["grey_wins"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("grey_wins",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["grey_penalty"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("grey_penalty",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping

							// TODO?
							// scrub diary entries

							// END GAME
							SDS.ending = "She Lost";
						}
					});
				}
			},
			controls : {
			},
			customPost : noop
		};


DATA_SDS_SCRIPTED_EVENTS["grey_challenge_win"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["grey_challenge_win"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.grey = cacheVars.objMgr.getNamedObject("grey");
				if (cacheVars.grey){
					cacheVars.grey.sprite.x = (13<<4);
					cacheVars.grey.sprite.y = (5<<4);
					cacheVars.grey.interactions |= DEFINE_GAMEOBJECT_INTERACTIONS.CHECK;
					cacheVars.grey.direction = 1;
				}
				cacheVars.ice = cacheVars.objMgr.getNamedObject("IceCream");
				if (cacheVars.ice) cacheVars.ice.filled = true;

				if (!GLOBAL_DIARY.check(0,"GREY_CHALLENGE")){
					GLOBAL_DIARY.note(0,"GREY_CHALLENGE")
				}

				if (DATA_SDS_DIALOGUE["floof_glomped"]){
					//cacheVars.wait=true;
					cacheVars.dlgMgr.open("floof_glomped",function(index,retval){
						//if (retval!=null){
						//	cacheVars.wait=false; // yay js scoping
						//}
					});
				}
			},
			controls : {
				"grey" : {A: true}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"grey" : {A: false}
			},
			customPost : noop
		};

DATA_SDS_SCRIPTED_EVENTS["Get_IceCream"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Get_IceCream"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Get_IceCream"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Get_IceCream",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}

				cacheVars.bgmName = SDS.bgmName;
				SDS.switchBgm("Found A Shiny Thing",false);

			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				SDS.switchBgm(cacheVars.bgmName,true);
			}
		};


evt = null;
t=null;