#include "./../system/Sound.txt"
#include "./../system/Music.txt"
#include "./MenuResult.txt"

let INVALID = -1;
let ReplaySwitch = false;

@Initialize
{	
	SetAutoDeleteObject(true);
	TBackground();
	TMenu();
	loop(75){yield;}
	PlayMusic(2);
}

@MainLoop
{
	yield;
}
@Finalize
{
	EndMusic(2);
}

task TBackground
{
	task TVertex(var index, var left, var top, var right, var bottom, var time)
		{
		ObjPrim_SetVertexPosition(obj, index + 0, left, top, 0);
		ObjPrim_SetVertexPosition(obj, index + 1, left, bottom, 0);
		ObjPrim_SetVertexPosition(obj, index + 2, right, top, 0);
		ObjPrim_SetVertexPosition(obj, index + 3, right, top, 0);
		ObjPrim_SetVertexPosition(obj, index + 4, left, bottom, 0);
		ObjPrim_SetVertexPosition(obj, index + 5, right, bottom, 0);

		ObjPrim_SetVertexUVT(obj, index + 0, left, top);
		ObjPrim_SetVertexUVT(obj, index + 1, left, bottom);
		ObjPrim_SetVertexUVT(obj, index + 2, right, top);
		ObjPrim_SetVertexUVT(obj, index + 3, right, top);
		ObjPrim_SetVertexUVT(obj, index + 4, left, bottom);
		ObjPrim_SetVertexUVT(obj, index + 5, right, bottom);	
		loop(30){yield;}
		ascent(i in 0..6)
			{
			ObjPrim_SetVertexAlpha(obj, index+i, 235);
			}
		let color = 0;
		let colorA = rand(0,360);
		let colorB = 0;
		loop
			{
			if(color < 155)
				{
				color+=0.35*time;
				}
			colorA+=2;
			colorB+=0.2*time;
			ascent(i in 0..6)
				{
				ObjPrim_SetVertexColor(obj, index+i, 255,max(0,255-color*cos(i*60+colorA)-colorB),max(0,255-color*cos(i*60+colorA)-colorB));
				}
			yield;
			}
		}

	let frame = 30;
	let countH = 20;
	let countV = 30; 
	let width = 640 / countH;
	let height = 480 / countV;
	let target = GetTransitionRenderTargetName();
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLELIST);
	ObjPrim_SetVertexCount(obj, countH * countV * 6);
	Obj_SetRenderPriorityI(obj, 0);
	ObjPrim_SetTexture(obj, target);

	ascent(ix in 0.. countH)
	{
		ascent(iy in 0.. countV)
		{
			let time = (countV-iy)*0.75/countV+0.25;
			let index = (ix + iy * countH) * 6;
			let left = ix * width;
			let right = left + width;
			let top = iy * height;
			let bottom = top + height;
			TVertex(index, left, top, right, bottom,time);
		}
	}

}

task TMenu
{
	let selectIndex = 0;
	function TMenuItem(let index, let mx, let my, let text)
		{
		function CreateTextObject(let mx, let my, let text)
			{
			let obj = ObjText_Create();
			ObjText_SetText(obj, text);
			ObjText_SetFontType(obj,"Century Gothic");
			ObjText_SetFontSize(obj, 24);
			ObjText_SetSidePitch(obj,1);
			ObjText_SetFontBold(obj, true);
			ObjText_SetFontColorTop(obj, 128, 255, 255);
			ObjText_SetFontColorBottom(obj, 64, 196, 196);
			ObjText_SetFontBorderType(obj, BORDER_FULL);
			ObjText_SetFontBorderColor(obj,64,64,64);
			ObjText_SetFontBorderWidth(obj, 2);
			Obj_SetRenderPriorityI(obj, 10);
			ObjRender_SetX(obj, mx);
			ObjRender_SetY(obj, my);
			return obj;
			}

		let objText = CreateTextObject(mx, my, text);
		let objSelect = CreateTextObject(mx, my, text);
		ObjRender_SetBlendType(objSelect, BLEND_ADD_RGB);
		TVis;
		return [objText,objSelect];
		task TVis
			{
			loop
				{
				Obj_SetVisible(objSelect, index == selectIndex);
				yield;
				}
			}
		}

	let mx = 128;
	let my = 32;
	let texts = ["Try Again", "Return to Title", "Save Replay", "Close Game"];
	var countMenu = length(texts);	
	let objText = [];
	let listResult = [RESULT_RESTART, RESULT_TITLE, RESULT_SAVE_VIDEO,RESULT_CLOSE];
	ascent(var iText in 0 .. countMenu)
		{
		objText = objText ~ [TMenuItem(iText, mx, my, texts[iText])];
		my += 32;
		}

	let frameKeyHold = 0;
	while(GetVirtualKeyState(VK_UP) == KEY_PUSH || GetVirtualKeyState(VK_UP) == KEY_HOLD || GetVirtualKeyState(VK_DOWN) == KEY_PUSH || GetVirtualKeyState(VK_DOWN) == KEY_HOLD || GetVirtualKeyState(VK_SHOT) == KEY_PUSH || GetVirtualKeyState(VK_SHOT) == KEY_HOLD){yield;}
	yield;
	loop
		{
		if(GetKeyState(KEY_R) == KEY_PUSH)
			{
			selectIndex = 0;
			SetScriptResult(listResult[selectIndex]);
			PlaySound("se_Confirm",120);
			CloseScript(GetOwnScriptID());
			return;
			}
		if(GetVirtualKeyState(VK_OK) == KEY_PUSH)
			{
			if(selectIndex == 2)
				{		
				PlaySound("se_Confirm",120);
				TDel(objText);
				TReplaySaveScene();
				return;
				}
			else
				{
				SetScriptResult(listResult[selectIndex]);
				PlaySound("se_Confirm",120);
				loop(20){yield;}
				CloseScript(GetOwnScriptID());
				return;
				}
			}
		if(GetVirtualKeyState(VK_UP) == KEY_PUSH)
			{
			PlaySound("se_Select",120);
			selectIndex--;
			if(selectIndex == 2 && ReplaySwitch == true)
				{
				selectIndex--;
				}
			}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH)
			{
			PlaySound("se_Select",120);
			selectIndex++;
			if(selectIndex == 2 && ReplaySwitch == true)
				{
				selectIndex++;
				}
			}
		else if(GetVirtualKeyState(VK_UP) == KEY_HOLD)
			{
			frameKeyHold--;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0)))
				{
				PlaySound("se_Select",120);
				selectIndex--;
				}
			if(selectIndex == 2 && ReplaySwitch == true)
				{
				selectIndex--;
				}
			}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_HOLD)
			{
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0)))
				{
				PlaySound("se_Select",120);
				selectIndex++;
				}
			if(selectIndex == 2 && ReplaySwitch == true)
				{
				selectIndex++;
				}
			}
		else
			{
			frameKeyHold = 0;
			}

		if(selectIndex < 0) 
			{
			selectIndex = countMenu - 1;
			}
		else
			{
			selectIndex %= countMenu;
			}
		if(ReplaySwitch == true)
			{
			ObjText_SetFontColorTop(objText[2][0], 64, 64, 128);
			ObjText_SetFontColorBottom(objText[2][0], 32, 32, 64);
			}
		yield;
		}
}

task TDel(obj)
{
	ascent(i0 in 0..length(obj))
		{
		ascent(i1 in 0..length(obj[i0]))
			{
			Obj_Delete(obj[i0][i1]);
			}
		}
}

function TReplaySaveScene()
{
	let pathScript = GetCurrentScriptDirectory ~ "ReplaySave.txt";

	let idScript = LoadScript(pathScript);
	StartScript(idScript);

	while(!IsCloseScript(idScript)){yield;}
	
	ReplaySwitch = GetScriptResult(idScript);
	if(ReplaySwitch == true)
		{
		PlaySound("se_Confirm",120);
		}
	else
		{
		PlaySound("se_Cancel",120);
		}
	TMenu;
}
