require( "scripts/style.lua" );

NameFont = {
  standardFont,
  24,
  BlackColor
};

ButtonX = kCenter;
ButtonY = 90+125;
ButtonYSpace = 58;

kMaxUsers = 5

function ShowSubmitOptions( show )
	EnableWindow( "delete", show )
	EnableWindow( "editname", show )
end

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

function UpdatePlayerChoices( curUser, numUsers, userTable, playTime )
	SetButtonToggleState( "button"..0, true );
	
	if (numUsers >= kMaxUsers) then
		ShowCreateOptions(false)
	else
		ShowCreateOptions(true)
	end

	if (numUsers==0) then
		ShowSubmitOptions( false );
		--EnableWindow( "timePlayedTitle", false );
	else
		ShowSubmitOptions( true )
		--EnableWindow( "timePlayedTitle", true );
	end

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

	end

	for i = numUsers, kMaxUsers do
		EnableWindow( "button"..i, false );
		--EnableWindow( "playTime"..i, false );
	end

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

--	DebugOut("UpdatePlayerChoices");
end

gSelected = -1 ;

function AddButtons( N )

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

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

	return Group(t);
end

function AddTimeBtns( N )

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

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

	return Group(t);
end


MakeDialog
{
	ChoosePlayerWindow
	{
  	name="chooseplayer",
		x = 0, y = 0, 
		w = ScreenWight, h = ScreenHeight,

    Bitmap
	  {
		  image="menu/dialog",
		  x=kCenter,
		  y=kCenter,
		};

		-- заголовок
		Text
		{
			font = HeaderFont,
			name = "selectaplayer",
			x=0,y=170,w=kMax,h=kMax,
			flags = kHAlignCenter + kVAlignTop,
			label = "selectaplayer"
		};

		-- время игры
		-- Text
		-- {
			-- font = DialogBodyFont,
			-- name = "timePlayedTitle",
			-- x=470,y=190,w=kMax,h=kMax,
			-- flags = kHAlignCenter + kVAlignTop,
			-- label = "timeplayed"
		-- };

		
		-- список профилей
		AddButtons( kMaxUsers );
		--AddTimeBtns( kMaxUsers );

		SetStyle(MenuButtonEmpty);
		-- отмена
		Button
		{
			x=kCenter+320,
			y=150,
			name="esc",
			cancel=true,
			command =
				function()
					SetCurrProfile(GetCurrentSelectedProfile());
					CloseFade("chooseplayer","CloseWindow()");
				end
		};
		
		-- кнопки управления профилями
		SetStyle(ProfilesButtonStyle);
		
		-- ОК
		Button
		{
			x=kCenter+150,
			y=450+120,
			name="chooseok",
			label="ok",
			default=true,
			command =
				function()
					SetCurrProfile(GetCurrentSelectedProfile());
					CloseFade("chooseplayer","CloseWindow()");
				end
		};

		-- редактирование профиля
		Button
		{
			x=kCenter-150,
			y=395+120,
			name="editname",
			label="editname",
			command =
				function()
					PlaySfx(mainMenuOpenSound);
					gEnterNameValue = GetCurrentSelectedProfile();
					name = DoModal("scripts/entername.lua");					
					if (not(name == nil)) then
						CurrProfileChangeName(name);
						SetPlayerChoices();
						SetWelcomeName();
					end
				end
		};


		-- новый профиль
		Button
		{
			x=kCenter+150,
			y=395+120,
			name="newplayer",
			label="newplayer",
			command =
				function()
					PlaySfx(mainMenuOpenSound);
					gEnterNameValue = "";
					name = DoModal("scripts/entername.lua");
					if (not(name == nil)) then
						AddProfile(name);
						SetPlayerChoices();
					end
				end
		};
		
		-- удаление профиля
		Button
		{
			x=kCenter-150,
			y=450+120,
			name="delete",
			label="deleteplayer",
			command =
				function()
					PlaySfx(mainMenuOpenSound);
					result = DisplayDialog {
						"scripts/yesno.lua",
						body ="deleteconfirm",
						title = "deletetitle"
					};
					if (result=='yes') then
						DeleteProfile(GetCurrentSelectedProfile());
						SetPlayerChoices();
					end
				end

		};
	};
};


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

SetPlayerChoices();

