@Initialize
{
	SetAutoDeleteObject(true);

	TBackground();
//	TReplaySelectScene();
	test;
	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 objImage2 = ObjPrim_Create(OBJ_SPRITE_2D); //2Dスプライトオブジェクト生成
	Obj_SetRenderPriorityI(objImage2, 3); //描画優先度を設定
	ObjPrim_SetTexture(objImage2, pathTitle); //テクスチャを設定
	ObjSprite2D_SetSourceRect(objImage2, 32, 16, 384+32, 448+16);
	ObjSprite2D_SetDestRect(objImage2, 32, 16, 384+32, 448+16);
	ObjRender_SetColor(objImage2,150,150,150);
/*
	//タイトル文字列
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Choose Load Replay");
	ObjText_SetFontSize(objText, 24);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 128, 128, 255);
	ObjText_SetFontColorBottom(objText, 64, 64, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,255, 255, 255);
	ObjText_SetFontBorderWidth(objText, 2);
	Obj_SetRenderPriorityI(objText, 30);
	ObjRender_SetX(objText, 88);
	ObjRender_SetY(objText, 64);
*/
}


//----------------------------------------------------
// リプレイ選択
//----------------------------------------------------
task test
{
//	yield;
let PackageFlag=0;
	//メニュー
	let INDEX_BGM = 0;
	let INDEX_EFFECT = 1;
	let INDEX_PLAYER = 2;

	let selectIndex = 0;//選択位置
	DrawMainExplain;

	//タイトルシーン終了フラグ
	let bEndTitleScene = false;

	//タイトルパス設定
	let dir = GetCurrentScriptDirectory();
	let pathTitle = dir ~ "Title.png";

	task 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_SetFontSize(obj, 20);
			ObjText_SetFontBold(obj, true);
			ObjText_SetFontColorTop(obj, 128, 128, 255);
			ObjText_SetFontColorBottom(obj, 64, 64, 255);
			ObjText_SetFontBorderType(obj, BORDER_FULL);
			ObjText_SetFontBorderColor(obj,255, 255, 255);
			ObjText_SetFontBorderWidth(obj, 2);
			Obj_SetRenderPriorityI(obj, 30);
			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);
		while(!PackageFlag)
		{
			Obj_SetVisible(objSelect, index == selectIndex);
			yield;
		}
		Obj_Delete(objText);
		Obj_Delete(objSelect);
	}

	//メニュー配置
	let mx = 48;
	let my = 360-240+80+32;
//	let texts = ["Start", "SpellPractice","Tuning","Achievement","Quit"];
	let texts = ["BGM","Effect"];
	var countMenu = length(texts);
	ascent(var iText in 0 .. countMenu)
	{
		TMenuItem(iText, mx, my, texts[iText]);
		DrawText(mx+128,my,iText);
		my += 32;
	}

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

	//メニュー選択処理
	let frameKeyHold = 0;//キー押しっぱなしフレーム数
	loop
	{
		//決定
		if(GetVirtualKeyState(VK_BOMB) == KEY_PUSH)
		{
			CloseScript(GetOwnScriptID());
		}

		//カーソル移動
		if(GetVirtualKeyState(VK_UP) == KEY_PUSH)
		{
			selectIndex--;
		}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH)
		{
			selectIndex++;
		}
		else if(GetVirtualKeyState(VK_UP) == KEY_HOLD)
		{
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0)))
			{
				selectIndex--;
			}
		}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_HOLD)
		{
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0)))
			{
				selectIndex++;
			}
		}
		else
		{
			frameKeyHold = 0;
		}

		if(selectIndex < 0) 
		{
			selectIndex = countMenu - 1;
		}
		else
		{
			selectIndex %= countMenu;
		}

		if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_PUSH)
		{
			if(selectIndex == INDEX_BGM)
			{
				if(GetAreaCommonData("TUNING","BGM",1)==0)
				{
					SetAreaCommonData("TUNING","BGM",1);
				}
				else
				{
					SetAreaCommonData("TUNING","BGM",0);
				}
			}
			else if(selectIndex == INDEX_EFFECT)
			{
				if(GetAreaCommonData("TUNING","Effect",1)==0)
				{
					SetAreaCommonData("TUNING","Effect",1);
				}
				else
				{
					SetAreaCommonData("TUNING","Effect",0);
				}
			}
			else if(selectIndex == INDEX_PLAYER)
			{
				if(GetAreaCommonData("TUNING","Player",1)==0)
				{
					SetAreaCommonData("TUNING","Player",1);
				}
				else
				{
					SetAreaCommonData("TUNING","Player",0);
				}
			}
		}

		yield;
	}

	task DrawMainExplain
	{
		let obj = ObjText_Create();
		ObjText_SetText(obj, "ステージを開始します");
		ObjText_SetFontSize(obj, 18);
		ObjText_SetFontBold(obj, false);
//		ObjText_SetFontColorTop(obj, 128, 128, 255);
//		ObjText_SetFontColorBottom(obj, 64, 64, 255);
		ObjText_SetFontBorderType(obj, BORDER_SHADOW);
		ObjText_SetFontBorderColor(obj,255, 255, 255);
		ObjText_SetFontBorderWidth(obj, 0);
		Obj_SetRenderPriorityI(obj, 30);
		ObjRender_SetX(obj, 44);
		ObjRender_SetY(obj, 400);
		ObjRender_SetAlpha(obj,225);
//		let Text=["ステージを開始します","スペルプラクティスを開始します","設定を変更します","実績を確認します","終了します"];
		let Text=["BGM再生を設定します","エフェクト量を設定します","プレイヤーを設定します"];
		while(!PackageFlag)
		{
			ObjText_SetText(obj, Text[selectIndex]);
			yield;
		}
		Obj_Delete(obj);
	}

	task DrawMainUnderExplain
	{
		let obj = ObjText_Create();
		ObjText_SetText(obj, "Shot Start  User1 Replay  Skip PlayerDetail");
		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, 34);
		ObjRender_SetY(obj, 450);
		let Alpha=255;
		let AlphaAngle=0;
		while(!PackageFlag)
		{
			ObjRender_SetAlpha(obj,200+55*cos(AlphaAngle));
			AlphaAngle+=2;
			yield;
		}
		Obj_Delete(obj);
	}
	task DrawPlayer
	{
		let obj = ObjText_Create();
		ObjText_SetText(obj, "Player");
		ObjText_SetFontSize(obj, 24);
		ObjText_SetFontBold(obj, true);
		ObjText_SetFontColorTop(obj, 192, 96, 96);
		ObjText_SetFontColorBottom(obj, 192, 48, 48);
		ObjText_SetFontBorderType(obj, BORDER_FULL);
		ObjText_SetFontBorderColor(obj,50, 50, 50);
		ObjText_SetFontBorderWidth(obj, 2);
		Obj_SetRenderPriorityI(obj, 30);
		ObjRender_SetX(obj, 72);
		ObjRender_SetY(obj, 420);
		while(!PackageFlag)
		{
			yield;
		}
		Obj_Delete(obj);
	}
}

task TReplaySelectScene
{
	LoadReplayList();
	let listValidReplayIndex = GetValidReplayIndices();

	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(32, 64 + 16 * itemY, 14, "");
		ObjText_SetFontBorderWidth(objText, 1);
		let objSelect = CreateTextObject(32, 64 + 16 * itemY, 14, "");
		ObjText_SetFontBorderWidth(objSelect, 1);
		ObjRender_SetBlendType(objSelect, BLEND_ADD_RGB);

		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);
				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;//キー押しっぱなしフレーム数
	loop
	{
		//決定
		if(GetVirtualKeyState(VK_OK) == KEY_PULL)
		{
			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_PULL)
		{
			//キャンセル
			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)))
			{
				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)))
			{
				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)))
			{
				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)))
			{
				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 DrawText(x,y,number)
{
	let text="a";
	let obj = ObjText_Create();
	ObjText_SetText(obj, text);
	ObjText_SetFontSize(obj, 20);
	ObjText_SetFontBold(obj, true);
	ObjText_SetFontColorTop(obj, 128, 128, 255);
	ObjText_SetFontColorBottom(obj, 64, 64, 255);
	ObjText_SetFontBorderType(obj, BORDER_FULL);
	ObjText_SetFontBorderColor(obj,255, 255, 255);
	ObjText_SetFontBorderWidth(obj, 2);
	Obj_SetRenderPriorityI(obj, 30);
	ObjRender_SetX(obj, x);
	ObjRender_SetY(obj, y);
//	while(!PackageFlag)
loop
	{
		alternative(number)
		case(0)
		{
			if(GetAreaCommonData("TUNING","BGM",1)==1){text="ON";}
			else{text="OFF";}
			ObjText_SetText(obj, text);
		}
		case(1)
		{
			if(GetAreaCommonData("TUNING","Effect",1)==1){text="HIGH";}
			else{text="LOW";}
			ObjText_SetText(obj, text);
		}
		case(2)
		{
			if(GetAreaCommonData("TUNING","Player",1)==1){text="DEFAULT";}
			else{text="FREE";}
			ObjText_SetText(obj, text);
		}
		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, 128, 128, 255);
	ObjText_SetFontColorBottom(obj, 64, 64, 255);
	ObjText_SetFontBorderType(obj, BORDER_FULL);
	ObjText_SetFontBorderColor(obj,255, 255, 255);
	ObjText_SetFontBorderWidth(obj, 2);
	Obj_SetRenderPriorityI(obj, 40);
	ObjRender_SetX(obj, mx);
	ObjRender_SetY(obj, my);
	return obj;
}

