require "scripts/style"

function ShowSubmitOptions( show )
	SetWindowVisible( "delete", show )
	SetWindowVisible( "chooseok", show )
	SetWindowVisible( "editname", show )
end

function ShowCreateOptions( show )
	SetWindowVisible( "newplayer", show )
end

function UpdatePlayerChoices( curUser, numUsers, userTable )

	if (numUsers >= kMaxUsers) then
		ShowCreateOptions(false)
	else
		ShowCreateOptions(true)
	end

	if (numUsers==0) then
		ShowSubmitOptions( false )
	else
		ShowSubmitOptions( true )
	end

	for i = 0,numUsers-1 do
		SetLabel( "button"..i, userTable[ i+1 ] );
		SetWindowVisible( "button"..i, true ) ;
	end

	for i = numUsers, kMaxUsers do
		SetWindowVisible( "button"..i, false ) ;
	end

	if numUsers>0 then
		SetButtonToggleState( "button"..curUser, true );
	end

--	DebugOut("UpdatePlayerChoices");
end

NameFont = {
  standardFont,
  34,
  BlackColor
};

HeaderFont = {
  standardFont,
  48,
  BlackColor
};

ButtonX = 134;
ButtonY = 205;
ButtonYSpace = 70;

kMaxUsers = 5

NameButtonGraphics = {
	"ui/profiles_menu/profiles/profile1_inactive",
	"ui/profiles_menu/profiles/profile1",
	"ui/profiles_menu/profiles/profile1_inactive_ro",
	"ui/profiles_menu/profiles/profile1_ro",
};

NameStyle= {
	type = kRadio,
	flags = kHAlignCenter + kVAlignCenter,
	font = NameFont,
	graphics = NameButtonGraphics,
	sound = kDefaultButtonSound
};

--gSelected = -1 ;

function AddButtons( N )

	t = { SetStyle(NameStyle), BeginGroup() };

	for i = 0,N-1 do
		table.insert(t,
			Button{
				name="button"..i,
--				command=function() gSelected = i; end,
				label="#name",
				x=ButtonX,
				y=ButtonY+(ButtonYSpace*i),
			}
		);
	end

	return Group(t);
end

local bx = 641
local by = 220
local bdy = 72

ButtonStyleProfile = {
	parent = ButtonStyle,
	graphics={"ui/profiles_menu/buttons/button_in","ui/profiles_menu/buttons/button_ac","ui/profiles_menu/buttons/button_ac"},
}

MakeDialog
{
	-- Name the modal dialog
	name="chooseplayer",
	Bitmap
	{
		image="ui/profiles_menu/background",
		x=kCenter,
		y=kCenter,
		alpha=true,

		ChoosePlayerWindow
		{
			x = 0, y = 0, w=kMax, h=kMax,

			Text
			{
				font = HeaderFont,
				name = "selectaplayer",
				x=0,y=100,w=kMax,h=kMax,
				flags = kHAlignCenter + kVAlignTop,
				label = "selectaplayer"
			};

			AddButtons( kMaxUsers );

			SetStyle(ButtonStyleProfile);
			Bitmap{
			 	image="ui/profiles_menu/buttons/button_grey",
				x=bx,
				y=by+0*bdy,
				Text{ w=kMax,h=kMax,label="newplayer",flags=kHAlignCenter + kVAlignCenter}
			};
			Bitmap{
			 	image="ui/profiles_menu/buttons/button_grey",
				x=bx,
				y=by+1*bdy,
				Text{ w=kMax,h=kMax,label="editname",flags=kHAlignCenter + kVAlignCenter}
			};
			Bitmap{
			 	image="ui/profiles_menu/buttons/button_grey",
				x=bx,
				y=by+2*bdy,
				Text{ w=kMax,h=kMax,label="deleteplayer",flags=kHAlignCenter + kVAlignCenter}
			};
			Bitmap{
			 	image="ui/profiles_menu/buttons/button_grey",
				x=bx,
				y=540,
				Text{ w=kMax,h=kMax,label="ok",flags=kHAlignCenter + kVAlignCenter}
			};

			Button
			{
				x=bx,
				y=by+1*bdy,
				name="editname",
				label="",
				label="editname",
				command =
					function()
						gEnterNameValue = GetSelectedPlayerName();
						name = DoModal("scripts/entername_edit.lua");
						if (not(name == nil)) then
							SetCurrentUser(GetCurrentSelectedUser());
							ChangeCurrentUserName(name);
							SetPlayerChoices();
							SetWelcomeName();
						end
					end
			};

			Button
			{
				x=bx,
				y=540,
				name="chooseok",
				label="ok",
				default=true,
				cancel=true,	-- ALSO allow them to exit the dialog by hitting escape
				command =
					function()
						SetCurrentUser(GetCurrentSelectedUser());
						SetWelcomeName();
						PopModal("chooseplayer");
					end

			};

			Button
			{
				x=bx,
				y=by+0*bdy,
				name="newplayer",
				label="newplayer",
				command =
					function()
						gEnterNameValue = "";
						name = DoModal("scripts/entername.lua");
						if (not(name == nil)) then
							CreateNewUser(name);
							SetPlayerChoices()
						end
					end
			};

			Button
			{
				x=bx,
				y=by+2*bdy,
				name="delete",
				label="deleteplayer",
				command =
					function()
						result = DisplayDialog {
							"scripts/yesno.lua",
							body ="deleteconfirm",
							title = "deletetitle"
						};
						if (result=='yes') then
							DeleteUser(GetCurrentSelectedUser())
							SetPlayerChoices()
						end
					end

			};
		}
	}
}


--UpdatePlayerChoices( 0,0, {} );

--SetPlayerChoices();

