---------------------------------------
--					Init
---------------------------------------
require "scripts/style"

local btnsXOffSet = 110;
local btnsYOffSet = 325;

local playername = gEnterNameValue;
local deleteselected = false;

if not playername or playername=="" then
	deleteselected = true;
	playername = GetString("enternameprompt") ;
end

---------------------------------------
--					Styles
---------------------------------------
MyFont = 
{
  defaultgameplayfont,
  30,
  BlackColor
};

NameFont = 
{
  defaultgameplayfont,
  22,
  BlackColor
};

---------------------------------------
--				Main Dialog
---------------------------------------
MakeDialog
{
	Bitmap
	{
		name = "dialogshadow",
		image = "backgrounds/dialogs/backgroundalpha",
	},

	Bitmap
	{
		image = "backgrounds/popups/popupdefault",
		x = kCenter,
		y = kCenter,

		Text
		{
			name = "entername",
			font = MyFont,
			x = 0,
			y = kCenter - 90,
			w = kMax,
			h = 30,
			flags = kHAlignCenter + kVAlignTop,
			label = "entername"
		};

		Bitmap
		{
			image = "buttons/buttonchoosename",
			x = kCenter,
			y = kCenter,
			scale = 1.1,
			TextEdit
			{
				name = "nameedit",
				x = 4,
				y = 0,
				w = kMax - 4,
				h = kMax,
				font = NameFont,
				label = playername,
				flags = kVAlignCenter,
				enablewindow = "enterok",
				utf8 = true,
				clearinitial = deleteselected,
				length = 11,
				ignore = kIllegalNameChars
			};
		};

		SetStyle( ButtonStyle ),
		Button
		{
			name = "enterok",
			x = kCenter - btnsXOffSet, 
			y = kMax - btnsYOffSet,
			DefaultButtonLabel("ok"),
			default = true,	-- Make this the default button
			command =
				function()
					local newname = GetLabel("nameedit");
					-- crop leading and trailing spaces
					newname = string.gsub( newname, "^%s+", "" );
					newname = string.gsub( newname, "%s+$", "" );
					if (string.len( newname )==0 ) 
					then
						DisplayDialog 
						{
							"scripts/popups/popupok.lua",
							body = "pleaseenteraname",
							title = ""
						};
						SetFocus( "nameedit" )						
					elseif (newname ~= playername and IsNameInUse(newname)) 
					then
						SetWindowVisible("dialogshadow",false);
						DisplayDialog 
						{
							"scripts/popups/popupconfirm.lua",
							body = "nameinuse",
							title = ""
						};
						SetFocus( "nameedit" );
						SetWindowVisible("dialogshadow",true);
					elseif (gCurrentPlayers > gMaxPlayers)
					then
						SetWindowVisible("dialogshadow",false);	
						DisplayDialog 
						{
							"scripts/popups/popupconfirm.lua",
							body = "maxplayers",
							title = ""
						};
						SetFocus( "nameedit" );
						SetWindowVisible("dialogshadow",true);
					else
						CloseWindow(newname);
					end
				end
		},

		Button
		{
			name = "entercancel",
			x = kCenter + btnsXOffSet, 
			y = kMax - btnsYOffSet,
			DefaultButtonLabel("cancel"),
			cancel = true,
			command = 
				function() 
					CloseWindow(nil); 
				end
		};
	},
}

SetFocus( "nameedit" );

if (deleteselected) 
then
	SetWindowVisible("enterok", false);
end