let Setting=GetAreaCommonData("TUNING","Setting",[]);

@Initialize
{
	SetAutoDeleteObject(true);

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


@MainLoop
{

	yield;
}

@Finalize
{
	SetAreaCommonData("TUNING","Setting",Setting);
}


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
{
	let SettingItemText=[
	"BGM Volume",
	"SE Volume",
	"LIFE Drawing",
	"LIFEGAUGE Drawing"
	];

	let cursorY = 0;
	let page = 0;

	let countMaxItem = length(SettingItemText);
	let countItemPerPage = 14;
	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=38;
		let objText = CreateTextObject(mx, 72 + 28 * itemY, 20, "ffty");

		let oldPage = -1;
		let Alpha=255;
		loop
		{
			let text = SettingItemText[itemY];
			while(length(text)<25)
			{
				text = text~" ";
			}
			alternative(itemY)
			case(0)
			{
				text=text~IntToString(Setting[0]);
			}
			case(1)
			{
				text=text~IntToString(Setting[1]);
			}
			case(2)
			{
				if(Setting[2]==1)
				{
					text=text~"ON";
				}
				else
				{
					text=text~"OFF";
				}
			}
			case(3)
			{
				if(Setting[3]==0)
				{
					text=text~"BAR";
				}
				else
				{
					text=text~"CIRCLE";
				}
			}
			ObjText_SetText(objText, text);

			if(cursorY==itemY)
			{
				ObjRender_SetAlpha(objText,Alpha);
			}
			else
			{
				ObjRender_SetAlpha(objText,Alpha*0.6);			
			}

			yield;
		}
		Obj_Delete(objText);

	}

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


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

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

	loop
	{
/*
		//決定
		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)))
			{
				alternative(cursorY)
				case(0)
				{
					if(Setting[0]>0)
					{
						Setting[0]=Setting[0]-10;
					}
				}
				case(1)
				{
					if(Setting[1]>0)
					{
						Setting[1]=Setting[1]-10;
					}
				}
				case(2)
				{
					if(Setting[2]==0)
					{
						Setting[2]=1;
					}
					else
					{
						Setting[2]=0;
					}
				}
				case(3)
				{
					if(Setting[3]==0)
					{
						Setting[3]=1;
					}
					else
					{
						Setting[3]=0;
					}
				}
			}
			SetAreaCommonData("TUNING","Setting",Setting);
		}
		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)))
			{
				alternative(cursorY)
				case(0)
				{
					if(Setting[0]<100)
					{
						Setting[0]=Setting[0]+10;
					}
				}
				case(1)
				{
					if(Setting[1]<100)
					{
						Setting[1]=Setting[1]+10;
					}
				}
				case(2)
				{
					if(Setting[2]==0)
					{
						Setting[2]=1;
					}
					else
					{
						Setting[2]=0;
					}
				}
				case(3)
				{
					if(Setting[3]==0)
					{
						Setting[3]=1;
					}
					else
					{
						Setting[3]=0;
					}
				}
			}
			SetAreaCommonData("TUNING","Setting",Setting);
		}
		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=72+28*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_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, "Cancel Back");
	ObjText_SetFontSize(obj, 12);
	ObjText_SetFontBold(obj, true);
	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);

}