#include"./../resource/[library] se.txt"
let object = -1;

@Initialize{
	SaveCommonDataAreaA2("HiScore",GetCurrentScriptDirectory ~ "./../resource/data/hiscore.dat");
	InstallFont(GetCurrentScriptDirectory ~ "./Blogger Sans-Bold.ttf");
	SetAutoDeleteObject(true);
	TBackground();
	TMenu();
	SE(se_crystal);
}

@MainLoop{yield;}

@Finalize{}

task TBackground{
	let img = "";
	let x1 = 0;
	let x2 = 0;
	let text1 = "";
	let text2 = "";
	if(GetCommonData("BOSS",0) == "Eternity"){ x1 = 265; x2 = 230; text1 = "Night 1"; text2 = "Eternity & Nemuno";
		img = GetCurrentScriptDirectory ~ "./../resource/img/pause01.png";}
	if(GetCommonData("BOSS",0) == "Nemuno"){ x1 = 50; x2 = 80; text1 = "Night 1"; text2 = "Eternity & Nemuno";
		img = GetCurrentScriptDirectory ~ "./../resource/img/pause02.png";}
	if(GetCommonData("BOSS",0) == "Aunn"){ x1 = 265; x2 = 230; text1 = "Night 2"; text2 = "Aunn & Lily";
		img = GetCurrentScriptDirectory ~ "./../resource/img/pause03.png";}
	if(GetCommonData("BOSS",0) == "Lily"){ x1 = 50; x2 = 80; text1 = "Night 2"; text2 = "Aunn & Lily";
		img = GetCurrentScriptDirectory ~ "./../resource/img/pause04.png";}
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj, 2);
	ObjPrim_SetTexture(obj, img);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 384, 500);
	ObjSprite2D_SetDestRect(obj, 32, -10, 416, 500-10);
	
	let target = GetTransitionRenderTargetName();
	let objB = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objB, 1);
	ObjPrim_SetTexture(objB, target);
	ObjSprite2D_SetSourceRect(objB, 32, 16, 416, 464);
	ObjSprite2D_SetDestRect(objB, 32, 16, 416, 464);
	ObjRender_SetColor(objB, 64, 64, 64);
	
	let target2 = GetTransitionRenderTargetName();
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj2, 0);
	ObjPrim_SetTexture(obj2, target2);
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 640, 480);
	ObjSprite2D_SetDestRect(obj2, 0, 0, 640, 480);
	
	let obj1 = ObjText_Create();
	ObjText_SetText(obj1, text1);
	ObjText_SetFontSize(obj1, 45);
	ObjText_SetFontBold(obj1, true);
	ObjText_SetFontType(obj1,"Blogger Sans");
	ObjText_SetFontColorTop(obj1, 80, 80, 255);
	ObjText_SetFontColorBottom(obj1, 200, 200, 255);
	ObjText_SetFontBorderType(obj1, BORDER_FULL);
	ObjText_SetFontBorderColor(obj1,0, 0, 0);
	ObjText_SetFontBorderWidth(obj1, 1);
	Obj_SetRenderPriorityI(obj1, 99);
	ObjRender_SetX(obj1, x1);
	ObjRender_SetY(obj1, 60);
	
	let obj3 = ObjText_Create();
	ObjText_SetText(obj3, text2);
	ObjText_SetFontSize(obj3, 20);
	ObjText_SetFontBold(obj3, true);
	ObjText_SetFontType(obj3,"Blogger Sans");
	ObjText_SetFontColorTop(obj3, 150, 150, 255);
	ObjText_SetFontColorBottom(obj3, 220, 220, 255);
	ObjText_SetFontBorderType(obj3, BORDER_FULL);
	ObjText_SetFontBorderColor(obj3,0, 0, 0);
	ObjText_SetFontBorderWidth(obj3, 1);
	Obj_SetRenderPriorityI(obj3, 99);
	ObjRender_SetX(obj3, x2);
	ObjRender_SetY(obj3, 105);
}

task TMenu{
	let selectIndex = 0;
	let ypos = 0;
	task TMenuItem(let index, let my, let text){

		function CreateTextObject(let my, let text){
			let obj = ObjText_Create();
			ObjText_SetText(obj, text);
			ObjText_SetFontSize(obj, 28);
			ObjText_SetFontBold(obj, true);
			ObjText_SetFontType(obj,"Blogger Sans");
			ObjText_SetFontColorTop(obj, 80, 80, 255);
			ObjText_SetFontColorBottom(obj, 200, 200, 255);
			ObjText_SetFontBorderType(obj, BORDER_FULL);
			ObjText_SetFontBorderColor(obj,0, 0, 0);
			ObjText_SetFontBorderWidth(obj, 1);
			Obj_SetRenderPriorityI(obj, 99);

			if(GetCommonData("BOSS",0) == "Eternity"){
				ObjRender_SetX(obj, 60);}
			if(GetCommonData("BOSS",0) == "Nemuno"){
				ObjText_SetHorizontalAlignment(obj, ALIGNMENT_RIGHT);
				ObjText_SetMaxWidth(obj, 32+GetStgFrameWidth-50);}
			if(GetCommonData("BOSS",0) == "Aunn"){
				ObjRender_SetX(obj, 60);}
			if(GetCommonData("BOSS",0) == "Lily"){
				ObjText_SetHorizontalAlignment(obj, ALIGNMENT_RIGHT);
				ObjText_SetMaxWidth(obj, 32+GetStgFrameWidth-50);}
			ObjRender_SetY(obj, my);
			return obj;
		}

		let objText = CreateTextObject(my, text);
		let objSelect = CreateTextObject(my, text);
		ObjRender_SetBlendType(objSelect, BLEND_ADD_RGB);
		loop{
			Obj_SetVisible(objSelect, index == selectIndex);
			yield;
		}
	}

		function Create_Crystal{
			object = ObjPrim_Create(OBJ_SPRITE_2D);
			let pause = GetCurrentScriptDirectory() ~ "./../resource/img/moon.png";
			ObjPrim_SetTexture(object,pause);
			Obj_SetRenderPriorityI(object, 0.99);
			ObjSprite2D_SetSourceRect(object, 1, 1, 72, 72);
			ObjSprite2D_SetDestCenter(object);
			ObjRender_SetScaleXYZ(object,0.6,0.6,0);
			Crystal_Manage(object);
			return object;
		}

		task Crystal_Manage(obj){
			let angle = 0;
			let x3 = 0;
			if(GetCommonData("BOSS",0) == "Eternity"){x3 = 70;}
			if(GetCommonData("BOSS",0) == "Nemuno"){x3 = GetStgFrameWidth-15;}
			if(GetCommonData("BOSS",0) == "Aunn"){x3 = 70;}
			if(GetCommonData("BOSS",0) == "Lily"){x3 = GetStgFrameWidth-15;}
			while(true) {
				ObjRender_SetPosition(obj,x3,340+ypos,0);
				ObjRender_SetAngleZ(obj,100+20*sin(angle));
				angle+=2;
				yield;
			}
		}

	let my = 340;
	let texts = ["Resume", "Return to Menu", "Play Again"];
	var countMenu = length(texts);
	ascent(var iText in 0 .. countMenu){
		TMenuItem(iText, my, texts[iText]);
		my += 30;}

	Create_Crystal;

	while(GetVirtualKeyState(VK_PAUSE) != KEY_FREE){yield;}

	loop{
		if(GetVirtualKeyState(VK_OK) == KEY_PULL){
			let listResult = [RESULT_CANCEL, RESULT_END, RESULT_RETRY];
			SetScriptResult(listResult[selectIndex]);
			CloseScript(GetOwnScriptID());
			break;}
	
		if(GetVirtualKeyState(VK_CANCEL) == KEY_PULL || GetVirtualKeyState(VK_PAUSE) == KEY_PULL){
			SetScriptResult(RESULT_CANCEL);
			CloseScript(GetOwnScriptID());
			break;}

		if(GetVirtualKeyState(VK_UP) == KEY_PUSH){
			SE(se_select); selectIndex--; ypos-=30;}
			
		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH){
			SE(se_select); selectIndex++; ypos+=30;}

		if(selectIndex < 0) {
			selectIndex = countMenu - 1;
			ypos = 60;}
			
		else if(selectIndex > 2){
			selectIndex = 0;
			ypos = 0;}
			
		else{selectIndex %= countMenu;}
		yield;
	}
}