@Initialize
{
	SetAutoDeleteObject(true);

	TBackground();
	TReplaySelectScene();
	UnderExplain;
	SetScriptResult("");
}


@MainLoop
{

	yield;
}

@Finalize
{
}

task TBackground()
{
	//背景
	let dir = GetCurrentScriptDirectory();
	let pathTitle = dir ~ "img/Title.png";

	let objImage = ObjPrim_Create(OBJ_SPRITE_2D); //2Dスプライトオブジェクト生成
	Obj_SetRenderPriorityI(objImage, 1); //描画優先度を設定
	ObjPrim_SetTexture(objImage, pathTitle); //テクスチャを設定
	ObjSprite2D_SetSourceRect(objImage, 0, 0, 640, 480);
	ObjSprite2D_SetDestRect(objImage, 0, 0, 640, 480);
//	ObjRender_SetAlpha(objImage, 64);

	let objText = ObjText_Create();
	ObjText_SetText(objText, "リプレイ選択");
	ObjText_SetFontSize(objText, 24);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 160, 160, 255);
//	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderType(objText, BORDER_NONE);
	ObjText_SetFontBorderColor(objText,255, 255, 255);
	ObjText_SetFontBorderWidth(objText, 2*0);
	Obj_SetRenderPriorityI(objText, 30);
	ObjRender_SetX(objText, 14);
	ObjRender_SetY(objText, 14);

	ObjText_SetFontType(objText,"ＭＳ 明朝");
}


//----------------------------------------------------
// リプレイ選択
//----------------------------------------------------
task TReplaySelectScene
{
	LoadReplayList();
	let listValidReplayIndex = GetValidReplayIndices();

	let cursorY = 0;
	let page = 0;

	let countMaxItem = length(listValidReplayIndex);
	let countItemPerPage = 16;
	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 mx=14;
		let objText = CreateTextObject(mx, 13+39 + 25 * itemY, 18, "");

		let oldPage = -1;
		let AlphaRate=1;
		//EraseEffect(objText);
		loop
		{
			if(page != oldPage)
			{
				let text = "";
				let indexList = page * countItemPerPage + itemY;
				if(indexList < countMaxItem)
				{
					let indexReplay = listValidReplayIndex[indexList];
					if(IsValidReplayIndex(indexReplay))
					{
						text = rtos("000", GetReplayInfo(indexReplay, REPLAY_COMMENT)) ~ " ";
					//	text = text ~ GetReplayInfo(indexReplay, REPLAY_COMMENT) ~ " ";
						text = text ~ vtos("-8s", GetReplayInfo(indexReplay, REPLAY_USER_NAME)) ~ " ";
						text = text ~ rtos("0000000000000", 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);
				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);
			}
*/
			Obj_SetVisible(objText,1);
			if(cursorY==itemY)
			{
				ObjRender_SetAlpha(objText,255*AlphaRate);
			//	ObjRender_SetColor(objText,255*AlphaRate,255*AlphaRate,255*AlphaRate);
			}
			else
			{
				ObjRender_SetAlpha(objText,255*AlphaRate*0.6);
			//	ObjRender_SetColor(objText,150,150,150);
				
			}

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

		task EraseEffect(obj)
		{
			let dir=1;
			loop
			{

			loop
			{
				if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH)
				{
					dir=1;
					break;
				}
				if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH)
				{
					dir=-1;
					break;
				}
				yield;
			}

			ObjRender_SetX(obj, mx+100*dir);
			loop(10)
			{
				ObjRender_SetX(obj, ObjRender_GetX(obj)-10*dir);
				AlphaRate-=0.1;
				yield;
			}
			AlphaRate=0;

			ObjRender_SetX(obj, mx+100*dir);
			loop(10)
			{
				ObjRender_SetX(obj, ObjRender_GetX(obj)-10*dir);
				AlphaRate+=0.1;
				yield;
			}
			AlphaRate=1;
			ObjRender_SetX(obj, mx);
			}
		}
	}

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


	//キー状態がリセットされるまで待機
	while(GetVirtualKeyState(VK_OK) != KEY_FREE){yield;}

	//キー処理
	let frameKeyHold = 0;//キー押しっぱなしフレーム数
	SelectEffect;
	PageDraw;

	if(!IsCommonDataAreaExists("ReplaySelect")){CreateCommonDataArea("ReplaySelect");}

	cursorY=GetAreaCommonData("ReplaySelect","CursorY",cursorY);
	page=GetAreaCommonData("ReplaySelect","Page",page);

	loop
	{
		SetAreaCommonData("ReplaySelect","CursorY",cursorY);
		SetAreaCommonData("ReplaySelect","Page",page);
		//決定
		if(GetVirtualKeyState(VK_OK) == KEY_PUSH)
		{
			let indexList = page * countItemPerPage + cursorY;
			if(indexList < countMaxItem) 
			{
				let indexReplay = listValidReplayIndex[indexList];
				let pathReplay = GetReplayInfo(indexReplay, REPLAY_FILE_PATH);
				SetScriptResult(pathReplay);
				CloseScript(GetOwnScriptID());
				break;
			}
		}

		if(GetVirtualKeyState(VK_CANCEL) == KEY_PUSH)
		{
			//キャンセル
			yield;
			CloseScript(GetOwnScriptID());
			break;
		}
		if(GetVirtualKeyState(VK_USER1) == KEY_PUSH)
		{
			//キャンセル
			yield;
			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 % 8 == 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 % 8 == 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 % 8 == 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 % 8 == 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 PageDraw
	{
	let objText = CreateTextObject(480, 14, 24,"Page　"~"1 / "~IntToString(pageMax+1));
	loop
	{
	ObjText_SetText(objText, "Page　"~IntToString(page+1)~" / "~IntToString(pageMax+1));
	yield;
	}
	}

	task SelectEffect
	{
		let x=0;
		let y=0;
		loop
		{
			y=13+39+25*cursorY;
		//	Light(rand(0,640),y+rand(0,16),rand(1,3),180+rand(-5,5));
			Light(rand(0,640),y+rand(15,16),rand(1,2)/3,270+rand(-5,5));
			Light(rand(0,640),y+rand(1,0),rand(1,2)/3,90+rand(-5,5));
			yield;
		}

		task Band
		{

		}

		task Light(X,Y,speed,angle)
		{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"../img/effect/star.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		ObjRender_SetAngleZ(obj,0);
		Obj_SetRenderPriorityI(obj, 60);

		ObjPrim_SetVertexPosition(obj, 0, -16, -16, -0);
		ObjPrim_SetVertexPosition(obj, 1, -16, 16, -0);
		ObjPrim_SetVertexPosition(obj, 2, 16, -16, 0);
		ObjPrim_SetVertexPosition(obj, 3, 16,16, 0);

		ObjPrim_SetVertexUVT(obj, 0, 0, 0);
		ObjPrim_SetVertexUVT(obj, 1, 0, 32);
		ObjPrim_SetVertexUVT(obj, 2, 32, 0);
		ObjPrim_SetVertexUVT(obj, 3, 32, 32);
		ObjRender_SetPosition(obj,X,Y,0);

		let scale=0.5;
		let Alpha=180;
		let Zangle=0;
		let ZanglePlus=rand(-5,5);

		loop(30)
		{
			scale-=0.5/30;
			Zangle+=ZanglePlus;
			ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(angle),ObjRender_GetY(obj)+speed*sin(angle),0);
			ObjRender_SetScaleXYZ(obj,scale,scale,scale);
			ObjRender_SetAngleZ(obj,Zangle);
			ObjRender_SetColor(obj,Alpha*0.75,Alpha*0.75,Alpha);
			yield;
		}
		Obj_Delete(obj);
		}
	}
}


//----------------------------------------------------
// ユーティリティ
//----------------------------------------------------
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_SetFontColorTop(obj, 255, 255, 255);
	ObjText_SetFontColorBottom(obj, 160, 160, 255);
//	ObjText_SetFontBorderType(obj, BORDER_FULL);
	ObjText_SetFontBorderType(obj, BORDER_NONE);
	ObjText_SetFontBorderColor(obj,255, 255, 255);
	ObjText_SetFontBorderWidth(obj, 2*0);
	Obj_SetRenderPriorityI(obj, 40);
	ObjRender_SetX(obj, mx);
	ObjRender_SetY(obj, my);

	ObjText_SetFontType(obj,"ＭＳ 明朝");
	return obj;
}

	task UnderExplain
	{
		let obj = ObjText_Create();
		ObjText_SetText(obj, "Shot Decide  User1 Back  Cancel Back");
		ObjText_SetFontSize(obj, 12);
		ObjText_SetFontBold(obj, true);
//		ObjText_SetFontColorTop(obj, 128, 128, 255);
//		ObjText_SetFontColorBottom(obj, 64, 64, 255);
		ObjText_SetFontBorderType(obj, BORDER_NONE);
		ObjText_SetFontBorderColor(obj,255, 255, 255);
		ObjText_SetFontBorderWidth(obj, 0);
		Obj_SetRenderPriorityI(obj, 30);
		ObjRender_SetX(obj, 14);
		ObjRender_SetY(obj, 464);
		let Alpha=255;
		let AlphaAngle=0;

		loop
		{
			ObjRender_SetAlpha(obj,200+55*cos(AlphaAngle));
			AlphaAngle+=2;
			yield;
		}
		Obj_Delete(obj);

	}