@Initialize{
	InstallFont(GetCurrentScriptDirectory ~ "./Amaranth-Bold.otf");
	SetAutoDeleteObject(true);
	LoadReplayList();

	TBackground();
	TReplayIndexSelection();}

@MainLoop{yield;}

@Finalize{}

let MENU_INDEX_SELECTION = 1;
let MENU_NAME_ENTRY = 2;
let menuMode = MENU_INDEX_SELECTION;

function CreateTextObject(let mx, let my, let size, let text){
	let obj = ObjText_Create();
	ObjText_SetText(obj, text);
	ObjText_SetFontSize(obj, size);
	ObjText_SetFontBold(obj, true);
	ObjText_SetFontType(obj,"Amaranth");
	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, 10);
	ObjRender_SetX(obj, mx);
	ObjRender_SetY(obj, my);
	return obj;
}

task TBackground{
	let target = GetTransitionRenderTargetName();
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, target);
	Obj_SetRenderPriority(obj, 0.1);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 640, 480);
	ObjSprite2D_SetDestRect(obj, 0, 0, 640, 480);
	ObjRender_SetAlpha(obj, 64);}

task TReplayIndexSelection(){
	let cursorY = 0;
	let page = 0;
	let countMaxItem = REPLAY_INDEX_DIGIT_MAX - REPLAY_INDEX_DIGIT_MIN + 1;
	let countItemPerPage = 10;
	let pageMax = trunc((countMaxItem - 1) / countItemPerPage);
	pageMax = max(pageMax, 1);
	let lastPageMaxCursorY = trunc(countMaxItem / countItemPerPage);

	task TMenuItem(let itemY){
		let objText = CreateTextObject(64, 64 + 30 * itemY, 18, "");
		let objSelect = CreateTextObject(64, 64 + 30 * itemY, 18, "");
		ObjRender_SetBlendType(objSelect, BLEND_ADD_RGB);

		let oldPage = -1;
		while(menuMode == MENU_INDEX_SELECTION){
			if(page != oldPage){
				let index = page * countItemPerPage + itemY + 1;
				let text = rtos("00", index) ~ " ";
				if(IsValidReplayIndex(index)){
					text = text ~ vtos("-8s", GetReplayInfo(index, REPLAY_USER_NAME)) ~ " ";
					text = text ~ GetReplayInfo(index, REPLAY_DATE_TIME) ~ " ";
					text = text ~ rtos("000000000000", GetReplayInfo(index, REPLAY_TOTAL_SCORE)) ~ " ";}
				else{text = text ~ "No Data";}
				ObjText_SetText(objText, text);
				ObjText_SetText(objSelect, text);
				oldPage = page;}
			
			if(page == pageMax && itemY >= lastPageMaxCursorY){
				Obj_SetVisible(objText, false);
				Obj_SetVisible(objSelect, false);}
			else{
				Obj_SetVisible(objText, true);
				Obj_SetVisible(objSelect, itemY == cursorY);}

			yield;}
		Obj_Delete(objText);
		Obj_Delete(objSelect);}

	ascent(let iItem in 0 .. countItemPerPage){TMenuItem(iItem);}

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

	let frameKeyHold = 0;
	while(menuMode == MENU_INDEX_SELECTION){
		if(GetVirtualKeyState(VK_OK) == KEY_PULL){
			menuMode = MENU_NAME_ENTRY;
			let index = page * countItemPerPage + cursorY + 1;
			TNameEntry(index);
			
			break;}

		if(GetVirtualKeyState(VK_UP) == KEY_PUSH || GetVirtualKeyState(VK_UP) == KEY_HOLD){
			frameKeyHold++;
			if(GetVirtualKeyState(VK_UP) == KEY_PUSH ||
				frameKeyHold == 20 ||
				 (frameKeyHold > 20 && (frameKeyHold % 10 == 0))){
				cursorY--;}}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH || GetVirtualKeyState(VK_DOWN) == KEY_HOLD){
			frameKeyHold++;
			if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH ||
				frameKeyHold == 20 ||
				 (frameKeyHold > 20 && (frameKeyHold % 10 == 0))){
				cursorY++;}}
		else if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD){
			frameKeyHold++;
			if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH ||
				frameKeyHold == 20 ||
				 (frameKeyHold > 20 && (frameKeyHold % 10 == 0))){
				page--;}}
		else if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD){
			frameKeyHold++;
			if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH ||
				frameKeyHold == 20 ||
				 (frameKeyHold > 20 && (frameKeyHold % 10 == 0))){
				page++;}}
		else{frameKeyHold = 0;}
		if(page < 0){page = pageMax;}
		else if(page > pageMax){page = 0;}

		if(page != pageMax){
			if(cursorY < 0){cursorY = countItemPerPage - 1;}
			else if(cursorY >= countItemPerPage){cursorY = 0;}}
		else{
			if(cursorY < 0){cursorY = lastPageMaxCursorY - 1;}
			else if(cursorY >= lastPageMaxCursorY){cursorY = 0;}}
		yield;}}


task TNameEntry(let replayIndex){
	let strTextIn = [
		["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P"],
		
		["Q","R","S","T","U","V","W","X","Y","Z",".",",","(",")","_","@"],
		
		["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p"],
		
		["q","r","s","t","u","v","w","x","y","z","+","-","/","<",">","&","~"],
		
		["0","1","2","3","4","5","6","7","8","9","!","?","'"," "," ","Done"]];

	let strTextView = [
		["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P"],
		
		["Q","R","S","T","U","V","W","X","Y","Z",".",",","(",")","_","@"],
		
		["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p"],
		
		["q","r","s","t","u","v","w","x","y","z","+","-","/","<",">","&","~"],
		
		["0","1","2","3","4","5","6","7","8","9","!","?","'"," "," ","Done"]];

	let cursorX = 0;
	let cursorY = 0;
	let maxCursorX = length(strTextIn[0]);
	let maxCursorY = length(strTextIn);

	task TMenuItem(let itemX, let itemY){
		let objText = CreateTextObject(120 + itemX * 24, 200 + itemY * 24, 22, strTextView[itemY][itemX]);
		let objSelect = CreateTextObject(120 + itemX * 24, 200 + itemY * 24, 22, strTextView[itemY][itemX]);
		ObjRender_SetBlendType(objSelect, BLEND_ADD_RGB);

		while(menuMode == MENU_NAME_ENTRY){
			Obj_SetVisible(objSelect, itemX == cursorX && itemY == cursorY);
			yield;}
		Obj_Delete(objText);
		Obj_Delete(objSelect);}

	ascent(let iY in 0..maxCursorY){
		ascent(let iX in 0 .. maxCursorX){
			TMenuItem(iX, iY);}}

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

	let userName = "";
	let objName = CreateTextObject(160, 96, 28, "");

	let frameKeyHold = 0;
	while(menuMode == MENU_NAME_ENTRY){
		
		if(GetVirtualKeyState(VK_OK) == KEY_PULL){
			let nameLength = length(userName);
			if(cursorX == maxCursorX-1 && cursorY == maxCursorY-1){
				if(nameLength == 0){
					userName = "No Name";}
				else{
					SaveReplay(replayIndex, userName);
					SetScriptResult(RESULT_END);
					CloseScript(GetOwnScriptID());
					return;}}
			else if(nameLength < 8){
				userName = userName ~ strTextIn[cursorY][cursorX];}}
		if(GetVirtualKeyState(VK_CANCEL) == KEY_PULL){
			let nameLength = length(userName);
			if(nameLength > 0){
				userName = userName[0..nameLength-1];}}
		ObjText_SetText(objName, userName);

		if(GetVirtualKeyState(VK_UP) == KEY_PUSH || GetVirtualKeyState(VK_UP) == KEY_HOLD){
			frameKeyHold++;
			if(GetVirtualKeyState(VK_UP) == KEY_PUSH ||
				frameKeyHold == 20 ||
				 (frameKeyHold > 20 && (frameKeyHold % 10 == 0))){cursorY--;}}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH || GetVirtualKeyState(VK_DOWN) == KEY_HOLD){
			frameKeyHold++;
			if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH ||
				frameKeyHold == 20 ||
				 (frameKeyHold > 20 && (frameKeyHold % 10 == 0))){
				cursorY++;}}
		else if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD){
			frameKeyHold++;
			if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH ||
				frameKeyHold == 20 ||
				 (frameKeyHold > 20 && (frameKeyHold % 10 == 0))){cursorX--;}}
		else if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD){
			frameKeyHold++;
			if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH ||
				frameKeyHold == 20 ||
				 (frameKeyHold > 20 && (frameKeyHold % 10 == 0))){
				cursorX++;}}
		else{frameKeyHold = 0;}

		if(cursorX < 0){cursorX = maxCursorX-1;}
		else if(cursorX >= maxCursorX){cursorX = 0;}

		if(cursorY < 0){cursorY = maxCursorY-1;}
		else if(cursorY >= maxCursorY){cursorY = 0;}

		yield;}}