require "scripts/style"

--------------------------------------
-- 					Init
--------------------------------------
local kButtonYSpace = 50;
local kDeltaY = 76;
local kXBtnsOffSet = 200;
local kYBtnsOffSet = 114;

--------------------------------------
-- 					Styles
--------------------------------------
NameFont = 
{
  defaultFont,
  24,
  BlackColor
};

NameButtonGraphics = 
{
	"buttons/buttonchoosenamedown.png", 
	"buttons/buttonchoosename.png",
	"buttons/buttonchoosenameover.png",
};

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

--------------------------------------
--				Functions
--------------------------------------
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 >= gMaxPlayers) 
	then
		ShowCreateOptions(false)
	else
		ShowCreateOptions(true)
	end

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

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

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

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

function AddButtons( N )

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

	for i = 0,N-1 
	do
		table.insert(t,
			Button
			{
				name="button"..i,
				ColorShadowColorText(kDefaultButtonFontWidth+50,
										kDefaultButtonFontHeight,
										kCenter,
										kCenter,
										22,
										"# ", 
										Color(247,248,249,255),	
										Color(65,72,116,255),
										"name"..i,
										1,
										kHAlignCenter + kVAlignCenter,
										defaultgameplayfont);	
				x= kCenter,
				y= 155 + (kButtonYSpace*i),
			}
		);
	end

	return Group(t);
end


--------------------------------------
--				Controls
--------------------------------------
txtSelectAPlayer = 
	ColorShadowColorText(kMax,
							kMax,
							0,
							112,
							35,
							"selectaplayer", 
							Color(65,72,116,255),	
							Color(247,248,249,255),
							"selectaplayer",
							1,
							kHAlignCenter + kVAlignTop,
							defaultgameplayfont);	
	
btnNewPlayer = 
	Button
	{
		name = "newplayer",
		x = kCenter,
		y = 250 + kYBtnsOffSet,
		ButtonLabel("newplayer",24,22,2),
		command =
			function()
				gEnterNameValue = "";
				SetWindowVisible("dialogshadow",false);
				
				name = DoModal("scripts/popups/popupentername.lua");
				
				if (not(name == nil)) 
				then
					if (gCurrentPlayers > gMaxPlayers)
					then
						SetWindowVisible("dialogshadow",false);	
						DisplayDialog 
						{
							"scripts/popups/popupconfirm.lua",
							body = "maxplayers",
							title = ""
						};
						SetFocus( "nameedit" );
						SetWindowVisible("dialogshadow",true);
					else
						CreateNewUser(name);
						SetPlayerChoices();
						SetWindowVisible("btnSoundOn",true);
						SetWindowVisible("btnSoundOff",false);
					end
				end
				SetWindowVisible("dialogshadow",true);
			end
	};

btnEditName = 
	Button
	{
		name = "editname",
		x = kCenter,
		y = 250 + kDeltaY*1 + kYBtnsOffSet,
		ButtonLabel("editname",24,22,2),
		command =
			function()
				SetWindowVisible("dialogshadow",false);

				gEnterNameValue = GetSelectedPlayerName();
				name = DoModal("scripts/popups/popupentername.lua");

				if (not(name == nil)) 
				then
					SetCurrentUser(GetCurrentSelectedUser());
					ChangeCurrentUserName(name);
					SetPlayerChoices();
					SetWelcomeName();
				end
				
				SetWindowVisible("dialogshadow",true);
			end
	};

btnDelete = 
	Button
	{
		name = "delete",
		x = kCenter,
		y = 250 + kDeltaY*2 + kYBtnsOffSet,
		ButtonLabel("deleteplayer",24,22,2),
		command =
			function()
				SetWindowVisible("dialogshadow",false);
				result = DisplayDialog 
				{
					"scripts/popups/popupyesno.lua",
					body ="deleteconfirm",
					title = "#"
				};

				if (result=='yes') 
				then
					DeleteUser(GetCurrentSelectedUser())
					SetPlayerChoices()
					if (isSoundMuted())
					then
						SetWindowVisible("btnSoundOn",false);
						SetWindowVisible("btnSoundOff",true);
					else
						SetWindowVisible("btnSoundOn",true);
						SetWindowVisible("btnSoundOff",false);
					end
				end
				SetWindowVisible("dialogshadow",true);
			end
	};

btnChooseOk = 
	Button
	{
		name = "chooseok",
		x = kCenter,
		y = 250 + kDeltaY*3 + kYBtnsOffSet,
		ButtonLabel("ok",24,12,2),--DefaultButtonLabel("ok"),
		default = true,
		cancel = true,	-- ALSO allow them to exit the dialog by hitting escape
		command =
			function()
				SetCurrentUser(GetCurrentSelectedUser());
				SetWelcomeName();
				PopModal("chooseplayer");
			end
	};


--------------------------------------
-- 				Main Dialog
--------------------------------------
MakeDialog
{
	-- Name the modal dialog
	name = "chooseplayer",

	Bitmap
	{
		name = "dialogshadow",
		image = "backgrounds/dialogs/backgroundalpha",
	},

	Bitmap
	{
		x = kCenter,
		y = kCenter,
		image = "backgrounds/popups/popupchangeplayer",
		alpha = true,
		ChoosePlayerWindow
		{
			x = 0, 
			y = 0, 
			w = kMax, 
			h = kMax,
			
			-- Title
			txtSelectAPlayer,
			
			-- Adding buttons according gMaxPlayers
			AddButtons( gMaxPlayers );

			-- Buttons
			SetStyle(ButtonStyle);
			btnEditName,
			btnChooseOk,
			btnNewPlayer,
			btnDelete,
		}
	}
}