require( "scripts/style.lua" );

NameFont = {
  standardFont,
  24,
  BlackColor
};

ButtonX = kCenter-110;
ButtonY = 90+130;
ButtonYSpace = 60;

kMaxUsers = 6

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

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

function UpdatePlayerChoices( curUser, numUsers, userTable )
	SetButtonToggleState( "button"..0, true );
	
	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 ] );
		EnableWindow( "button"..i, true ) ;
	end

	for i = numUsers, kMaxUsers do
		EnableWindow( "button"..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,
			QEMenuButton{
				name="button"..i,
				type = kRadio,
        sound = defaultButtonSound,    
        rolloversound = mainMenuOverSound,    
				command=function() gSelected = i; end,
				label = "#name",
				x=ButtonX,
				y=ButtonY+(ButtonYSpace*i),				
			}
		);
	end

	return Group(t);
end

profBtnX = kCenter+186;
profBtnY = 260;
profBtndY = 66;
    
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=160,w=kMax,h=kMax,
			ProfileButtonLabel("selectaplayer",HeaderFontSize)
		};
		
		-- список профилей
		AddButtons( kMaxUsers );
		
		-- кнопки управления профилями
		SetStyle(BackButtonStyle);
		
		-- ОК
		QEMenuButton
		{
			x=kCenter+160,
			y=500,
			cancel=true,
			default=true,
			name="chooseok",
			command =
				function()
					SetCurrProfile(GetCurrentSelectedProfile());
					CloseFade("chooseplayer","CloseWindow()");
				end
		};
		
				-- новый профиль
		SetStyle( ProfilesButtonStyle3 ),
		QEMenuButton
		{
			x=profBtnX,
			y=profBtnY,
			name="newplayer",
			label="newplayer",
			command =
				function()
				  PlaySfx(mainMenuOpenSound);
					gEnterNameValue = "";
					name = DoModal("scripts/entername.lua");
					if (not(name == nil)) then
						AddProfile(name);
						SetPlayerChoices();
					end
				end
		};
		
		-- редактирование профиля
		SetStyle( ProfilesButtonStyle2 ),
		QEMenuButton
		{
			x=profBtnX,
			y=profBtnY+profBtndY,
			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
		};
		
		-- удаление профиля
		SetStyle( ProfilesButtonStyle1 ),
		QEMenuButton
		{
			x=profBtnX,
			y=profBtnY+profBtndY*2,
			name="delete",
			label="deleteplayer",
			command =
				function()
				  PlaySfx(mainMenuOpenSound);
					result = DoModal("scripts/yesno.lua");
					if (result=='yes') then
						DeleteProfile(GetCurrentSelectedProfile());
						SetPlayerChoices();
					end
				end

		};
	};
};


UpdatePlayerChoices( 0,0, {} );

SetPlayerChoices();

