#include"./Stage/Script/LoadSounds.txt"

@Initialize{
	SetAutoDeleteObject(true);

	TBackground();
	TReplaySelectScene();

	SetScriptResult("");}

@MainLoop{yield;}
@Finalize{}

let objText2 = ObjText_Create();
let objBlack = ObjPrim_Create(OBJ_SPRITE_2D); 
let objTitleImage = ObjPrim_Create(OBJ_SPRITE_2D); 

task TBackground(){
	let dir = GetCurrentScriptDirectory();
	let pathTitle = dir ~ "./Image/menu.png";

	Obj_SetRenderPriorityI(objTitleImage, 20);
	ObjPrim_SetTexture(objTitleImage, pathTitle);
	ObjSprite2D_SetSourceRect(objTitleImage, 0, 0, 800, 600);
	ObjSprite2D_SetDestRect(objTitleImage, 0, 0, 640, 480);
	ObjRender_SetAlpha(objTitleImage, 120);

	ObjText_SetText(objText2, "Replay");
	ObjText_SetFontType(objText2,"Blogger Sans");
	ObjText_SetFontSize(objText2, 40);
	ObjText_SetFontColorTop(objText2, 255, 255, 255);
	ObjText_SetFontColorBottom(objText2, 255, 255, 255);
	Obj_SetRenderPriorityI(objText2, 30);
	ObjRender_SetX(objText2, 16);
	ObjRender_SetY(objText2, 16);
	let scale=0;
	loop(20){
		ObjRender_SetScaleX(objText2,scale);
		scale+=0.05; yield;}}

task TReplaySelectScene{
	LoadReplayList();
	let listValidReplayIndex = GetValidReplayIndices();
	let pathFire = GetCurrentScriptDirectory ~ "./Image/firesheet.png";

	let cursorY = 0;
	let page = 0;
	let countMaxItem = length(listValidReplayIndex);
	let countItemPerPage = 20;
	let pageMax = trunc((countMaxItem - 1) / countItemPerPage);
	pageMax = max(pageMax, 0);
	let lastPageMaxCursorY = trunc(countMaxItem % countItemPerPage);
	if(countMaxItem % countItemPerPage == 0){
		lastPageMaxCursorY = countItemPerPage;}

	task TMenuItem(let itemY){
		let objText = CreateTextObject(45, 75 + 26 * itemY, 20, "");
		let objSelect = CreateTextObject(45, 75 + 26 * itemY, 20, "");
		ObjRender_SetBlendType(objSelect, BLEND_ADD_ARGB);

		let oldPage = -1;
		loop{
			if(page != oldPage){
				let text = "";
				let indexList = page * countItemPerPage + itemY;
				if(indexList < countMaxItem){
					let indexReplay = listValidReplayIndex[indexList];
					if(IsValidReplayIndex(indexReplay)){
						text = rtos("00", indexReplay) ~ " ";
						text = text ~ vtos("-8s", GetReplayInfo(indexReplay, REPLAY_USER_NAME)) ~ " ";
						text = text ~ rtos("000000000000", GetReplayInfo(indexReplay, REPLAY_TOTAL_SCORE)) ~ " ";
						text = text ~ vtos("-8s", GetReplayInfo(indexReplay, REPLAY_PLAYER_NAME)) ~ " ";
						text = text ~ vtos("03.2f", GetReplayInfo(indexReplay, REPLAY_FPS_AVERAGE)) ~ "fps ";
						text = text ~ GetReplayInfo(indexReplay, REPLAY_DATE_TIME) ~ " ";}}

				ObjText_SetText(objText, text);
				ObjText_SetText(objSelect, text);
				let scaleSS = 0;
				loop(20){
					ObjRender_SetScaleX(objText,scaleSS);
					ObjRender_SetScaleX(objSelect,scaleSS);
					scaleSS+=0.05;yield;}
				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);}
				
			let scaleSS = 1;
			let alphaT = 120;
			if(GetVirtualKeyState(VK_OK) == KEY_PUSH){
			SE(se_ok);
			loop(20){
				ObjRender_SetScaleX(objText,scaleSS);
				ObjRender_SetScaleX(objText2,scaleSS);
				ObjRender_SetScaleX(objSelect,scaleSS);
				ObjRender_SetAlpha(objTitleImage,alphaT);
				alphaT-=120/20;scaleSS-=0.0528;yield;}}
			if(GetVirtualKeyState(VK_CANCEL) == KEY_PUSH){

			loop(20){
				ObjRender_SetScaleX(objText,scaleSS);
				ObjRender_SetScaleX(objText2,scaleSS);
				ObjRender_SetScaleX(objSelect,scaleSS);
			scaleSS-=0.0528;yield;}}
			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;
	loop{
		if(GetVirtualKeyState(VK_OK) == KEY_PULL){
			let indexList = page * countItemPerPage + cursorY;
			if(indexList < countMaxItem) {
				loop(20){yield;}
				let indexReplay = listValidReplayIndex[indexList];
				let pathReplay = GetReplayInfo(indexReplay, REPLAY_FILE_PATH);
				SetScriptResult(pathReplay);
				CloseScript(GetOwnScriptID());
				break;}}

		if(GetVirtualKeyState(VK_CANCEL) == KEY_PULL){
			SE(se_cancel);
			loop(20){yield;}
			Obj_Delete(objTitleImage);
			Obj_Delete(objText2);
			CloseScript(GetOwnScriptID());
			break;}

		if(GetVirtualKeyState(VK_UP) == KEY_PUSH || GetVirtualKeyState(VK_UP) == KEY_HOLD){
			frameKeyHold++; 
			if(GetVirtualKeyState(VK_UP) == KEY_PUSH ||
				frameKeyHold == 20 ||
				 (frameKeyHold > 20 && (frameKeyHold % 5 == 0))){SE(se_select);
				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 % 5 == 0))){SE(se_select);
				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 % 5 == 0))){SE(se_select);
				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 % 5 == 0))){SE(se_select);
				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;}}

function CreateTextObject(let mx, let my, let size, let text){
	let obj = ObjText_Create();
	ObjText_SetText(obj, text);
	ObjText_SetFontSize(obj, size);
	ObjText_SetFontColorTop(obj, 255, 255, 255);
	ObjText_SetFontColorBottom(obj, 255, 255, 255);
	Obj_SetRenderPriorityI(obj, 40);
	ObjRender_SetX(obj, mx);
	ObjRender_SetY(obj, my);
	return obj;}