---------------------------------------------------------------------------------------------------
--  Copyright © 2009 PoBros, Incorporated. All Rights Reserved.
--
--  This source code is the proprietary confidential property of PoBros Incorporated.
--  Modification; reproduction; publication; or distribution in any form to any party 
--  other than the licensee is strictly prohibited.
--
--  FOR USE BY AUTHORIZED PERSONS ONLY
---------------------------------------------------------------------------------------------------

require( "scripts/style.lua" );

local kButtonX = kCenter - 20;
local kButtonY = kCenter - 120;
local kButtonYSpace = 40;
local kButtonYSliderOffset = 3;
local kSliderX = kCenter + 197;
local kSliderXOffset = -4;
local kActionButtonY = kCenter + 150;
local kActionButtonYLow = kActionButtonY + 50;
kMaxProfileButtons = 5;

---------------------------------------------------------------------------------------------------

function AddButtons( N )
	t = 
	{ 
		SetStyle( PlayerNameStyle ), 
		BeginGroup() 
	};

	for i = 0, N - 1 do
		table.insert( t,
					  Button
					  {
						  name = "button"..i,	-- read from selectplayer.cpp
						  text = "#name",
						  x = kButtonX,
						  y = kButtonY + ( kButtonYSpace * ( i + 1 ) )
					  } );	
	end

	return Group( t );
end

---------------------------------------

function CheckForUserMax()
	if( GetNumUsers() >= 12 ) then
		EnableWindow( "createnewplayer", false );
	else
		EnableWindow( "createnewplayer", true );
	end
end

---------------------------------------------------------------------------------------------------

MakeDialog
{
	name = "selectplayer";
	SetStyle( GameWindowStyle );
	
	SelectPlayerWindow
	{
		SetStyle( LargeDialogWindowStyle );
			
		DialogWindow
		{
			SetStyle( LargeDialogTitleText );

			Text
			{
				name = "selectplayer";
				label = "selectplayer";
			};
			
			AddButtons( kMaxProfileButtons );
			SetStyle( PlayerNameStyle );
			
			Button
			{
				name = "createnewplayer";
				text = "createnewplayer";
				type = kPush;
				x = kButtonX;
				y = kButtonY;
				buttongraphics = LargeButtonGraphics;

				command = 
					function()
						name = DoModal( "scripts/createplayer.lua" );
						
						if ( not( name == "" ) ) then
							CreateNewUser( name );
							NewGame();
							LoadProfile();
							SetSelectedPlayer();
							SetPlayerChoices();
							SetWelcomeName();
							CheckForUserMax();
						end
					end
			};
			
			SetStyle( ThreeButtonDialogButton1Style );

			Button
			{
				y = kActionButtonY;
				name = "select";
				text = "select";
				type = kPush;
				
				command = 
					function()
						SetCurrentUser( GetCurrentSelectedUser() );
						LoadProfile();
						SetWelcomeName();
						WindowReturn( "selectplayer" );
					end
				
			};
			
			SetStyle( ThreeButtonDialogButton2Style );

			Button
			{
				y = kActionButtonYLow;
				name = "delete";
				text = "delete";
				type = kPush;
				
				command =
					function()
						result = DisplayDialog 
								 { 
								     "scripts/yesno.lua";
								     body = "#"..GetString( "playerdeletebody", GetSelectedPlayerName() );
									 title = "playerdeleteheader";
								 };
								 
						if( result == 'yes' ) then
							DeleteUser( GetCurrentSelectedUser() );
							SetSelectedPlayer();
							SetPlayerChoices();
							SetWelcomeName();
							CheckForUserMax();
						end
					end	
			};
			
			SetStyle( ThreeButtonDialogButton3Style );
			
			Button
			{
				y = kActionButtonY;
				name = "cancel";
				text = "cancel";
				type = kPush;

				command = 
					function()
						WindowReturn( "selectplayer" );
					end
				
			};
			
			SetStyle( SliderStyle );
			
			Slider
			{
				name = "slider";		-- read from selectplayer.cpp
				x = kSliderX;
				y = kButtonY + ( ( kMaxProfileButtons * kButtonYSpace ) / 2 );
				w = 50;
				yoffset = -5;
				h = kButtonYSpace * ( kMaxProfileButtons - 1 );
			};
			
			SetStyle( UpButtonStyle );
			
			Button
			{
				name = "playerup";
				x = kSliderX + kSliderXOffset;
				y = kButtonY - kButtonYSliderOffset;
				command =
					function()
						MoveTopPlayerUp();
					end
			};
			
			SetStyle( DownButtonStyle );
			
			Button
			{
				name = "playerdown";
				x = kSliderX + kSliderXOffset;
				y = kButtonY + kButtonYSliderOffset + ( kMaxProfileButtons * kButtonYSpace );
				command =
					function()
						MoveTopPlayerDown();
					end
			};
		};
	};
};

---------------------------------------------------------------------------------------------------

CheckForUserMax();