"use strict";

/*
basic events structure, and some global, system, and chapter-agnostic events
*/

function DATA_EARTHQUAKE_FN(cacheVars){
	if (cacheVars.meli){
		cacheVars.meli.sprite.y += cacheVars.delta;
	}

	cacheVars.delta = (cacheVars.delta>0) ? -1 : 1;
}

function DATA_SHRINEQUAKE_FN(cacheVars){
	if (cacheVars.meli){
		cacheVars.meli.sprite.y += cacheVars.delta;
	}
	if (cacheVars.rinda){
		cacheVars.rinda.sprite.y += cacheVars.delta;
	}

	switch(cacheVars.delta){
		case -1:
			cacheVars.delta = 1;
			break;
		case 1:
			cacheVars.delta = -2;
			break;
		case -2:
			cacheVars.delta = 2;
			break;
		case 2:
			cacheVars.delta = -1;
			break;
	}
}

var DATA_SDS_SCRIPTED_EVENTS = {
	Generic_dialogue : {
		0 : {
			customPre : function(cacheVars){
				cacheVars.dlgMgr.open(cacheVars.dialogueName,noop);
			},
			controls : {},
			customPost : noop
		}
	},

	Save_dialogue : {
		0 : {
			customPre : function(cacheVars){
				var saveDlg = "Save";
				if (GLOBAL_SPEEDRUN_EXTREME){
					saveDlg = "Save_speedrun";
				}
				else if(GLOBAL_DIARY.narrative[GLOBAL_DIARY.narrative.length-1].chaptergory=="DIARIES" &&
					GLOBAL_DIARY.narrative[GLOBAL_DIARY.narrative.length-1].eventname==cacheVars.diaryName){
					saveDlg = "Save_null";
				}

				if (DATA_SDS_DIALOGUE[saveDlg]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open(saveDlg,function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							cacheVars.retval=retval;
						}
					});
				}
			},
			controls : {},
			customPost : noop
		},
		1 : {
			customPre : function(cacheVars){
				GLOBAL_DIARY.tagged = cacheVars.diaryName;
				if (cacheVars.retval){
					var diaryel = document.getElementById("diary");
					diaryel.value=GLOBAL_DIARY.record(cacheVars.diaryName);
					diaryel.scrollTop = diaryel.scrollHeight;

					if (cacheVars.diaryName){
						var diaryobj = cacheVars.objMgr.getNamedObject("Diary_"+cacheVars.diaryName);
						if (diaryobj){
							diaryobj.sprite.frame=1;
						}
					}

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

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

				cacheVars.bgmName = SDS.bgmName;
				SDS.switchBgm("A Strange Gift",false);

			},
			controls : {},
			customPost : noop
		},
		1 : {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				SDS.switchBgm(cacheVars.bgmName,true);
			}
		}
	},
	Get_Soul : {
		0 : {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Get_Soul"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Get_Soul",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}

				cacheVars.bgmName = SDS.bgmName;
				SDS.switchBgm("Soylent Purple",false);

			},
			controls : {},
			customPost : noop
		},
		1 : {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				SDS.switchBgm(cacheVars.bgmName,true);
			}
		}
	},

	Get_Bracelets : {
		0 : {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Get_Bracelets"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Get_Bracelets",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
		},
		1 : {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				SDS.switchBgm(cacheVars.bgmName,true);
			}
		}
	},

	Get_Necklace : {
		0 : {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Get_Necklace"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Get_Necklace",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
		},
		1 : {
			customPre : function(cacheVars){
				cacheVars.grace = cacheVars.objMgr.getNamedObject("Grace");
				if (cacheVars.grace){
					if (DATA_SDS_DIALOGUE["Grace_c2_jobdone"]){
						cacheVars.wait=true;
						cacheVars.dlgMgr.open("Grace_c2_jobdone",function(index,retval){
							if (retval!=null){
								cacheVars.wait=false; // yay js scoping
							}
						});
					}
				}
			},
			controls : {},
			customPost : noop
		},
		2 : {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				SDS.switchBgm(cacheVars.bgmName,true);
			}
		}
	},

	Get_Belt : {
		0 : {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Get_Belt"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Get_Belt",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
		},
		1 : {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				SDS.switchBgm(cacheVars.bgmName,true);
			}
		}
	},

	Get_Circlet : {
		0 : {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Get_Circlet"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Get_Circlet",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
		},
		1 : {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				SDS.switchBgm(cacheVars.bgmName,true);

				GLOBAL_SCRATCHPAD.note("Dlg_Zeke_Line",0);
			}
		}
	},

	Deaths_introduction : {
		0 : {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Death_newly_acquainted"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Death_newly_acquainted",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							GLOBAL_DIARY.note(0,"MET_DEATH");
							var door = cacheVars.objMgr.getNamedObject("Death's Door");
							if (door){
								door.unlocked=true;
								var poof = new GO_Poof();
								poof.spawnOnSprite(door.sprite,cacheVars.objMgr);
							}
						}
					});
				}
			},
			controls : {},
			customPost : noop
		}
	},

	grey : {
		0 : {
			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.bgmName = SDS.bgmName;
				if (SDS.bgm) SDS.bgm.stop();
				SDS.bgm = new Chiptune("Writing a Character",SDS.bgmchip);
				SDS.bgm.play(true);

				
				if (DATA_SDS_DIALOGUE["grey_greeting_"+GLOBAL_DIARY.greyCount()]){
					// not a fan of editing my "static" data, but since grey can be wherever,
					// be sure we don't occlude
					if (cacheVars.meli){
						DATA_SDS_DIALOGUE["grey_greeting_"+GLOBAL_DIARY.greyCount()].y = (cacheVars.meli.sprite.y < ((7+cacheVars.meli.sprite.map_room.margin)<<4)) ? 
							DEFINE_DIALOGUE_BOX_LOWER.y :
							DEFINE_DIALOGUE_BOX_UPPER.y;
					}
					
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("grey_greeting_"+GLOBAL_DIARY.greyCount(),function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping

							if (retval==0){
								cacheVars.frameCounter=39; // skip straight to events at frame 27 (cleanup)
							}
						}
					});
				}
			},
			controls : {},
			customPost : noop
		},
		1 : {
			customPre : noop,
			controls : {
				"grey":{B:true}
			},
			customPost : noop
		},
		8 : {
			customPre : function(cacheVars){
				if (cacheVars.meli){
					cacheVars.meli.overrideFrame = 27;
					cacheVars.meli.overrideFrame += cacheVars.meli.direction>0 ? 0 : 32;
				}

				if (DATA_SDS_DIALOGUE["grey_speech_"+GLOBAL_DIARY.greyCount()]){
					// not a fan of editing my "static" data, but since grey can be wherever,
					// be sure we don't occlude
					if (cacheVars.meli){
						DATA_SDS_DIALOGUE["grey_speech_"+GLOBAL_DIARY.greyCount()].y = (cacheVars.meli.sprite.y < ((7+cacheVars.meli.sprite.map_room.margin)<<4)) ? 
							DEFINE_DIALOGUE_BOX_LOWER.y :
							DEFINE_DIALOGUE_BOX_UPPER.y;
					}

					cacheVars.wait=true;
					cacheVars.dlgMgr.open("grey_speech_"+GLOBAL_DIARY.greyCount(),function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping

							if (cacheVars.grey){
								GLOBAL_DIARY.note(0,"GREY_LOC_"+cacheVars.grey.location);
								GLOBAL_DIARY.note(0,"GREY_"+GLOBAL_DIARY.greyCount());
							};
						}
					});
				}
			},
			controls : {},
			customPost : noop
		},
		9 : {
			customPre : noop,
			controls : {
				"grey":{B: false}
			},
			customPost : function(cacheVars){
				if (cacheVars.meli){
					cacheVars.meli.overrideFrame=null;
				}
			}
		},
		39 : {
			customPre : noop,
			controls : {
				"grey":{A: true}
			},
			customPost : noop
		},
		40 : {
			customPre : noop,
			controls : {
				"grey":{A: false}
			},
			customPost : noop
		},
		
		50 : {
			customPre : noop,
			controls : {},
			customPost : 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.controls){
					cacheVars.meli.controls = cacheVars.controls;
					delete cacheVars.controls;
				}
			}
		},
	},

}
