-- requestname.lua

require("scripts/styles/dialogs/requestnamestyle.lua");
require("scripts/styles/dialogs/changeplayerstyle.lua")
require("scripts/styles/mainmenustyle.lua");

--function extracted from http://www.lua.org/pil/20.3.html
   function trim (s)
      return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
    end


btnPosY = 200.0
offsetBtns = 100.0
 
MakeDialog 
{
	Bitmap
	{
		image = "common/screens/dialog_box/dialog_background",
		x = kCenter, y = kCenter,
		
	},
	
	
	WndUserName
	{
		name = "WndUserName",
		w = width,
		h = height,
			

		
		
	SetStyle(AccountsTitleStyle),
		
	-- Create the label that asks for the name
	Text 
	{
		y = 281.0,
		w = kMax, h = kMax,
		--font = RequestNameLabelFont,
		flags = kHAlignCenter,
		label = "entername",
	},
		
	-- Create a image to represent the editbox
	Bitmap
	{
		image = "backgrounds/textfield",
		x = kCenter, y = kCenter - 15.0,
		
		-- Create the edit box where the name must be inserted
		TextEdit
		{
			name = "editRequestName",
			x = 3, y = 5, w = kMax, h = kMax,
			font = RequestNameFont,
			length = 13,
			ignore = illegalCharacters
		};
	};
	
	-- Create the OK button
	SetStyle(OrangeButtonStyle),
	
	-- OK button
	BlgButton
	{
		name = "btnOK",
		x = 323, y = 495,
		label = "ok",
		width = 300, height = 300,
		positionMatrix = {x = 10, y = 10},
		graphics =
		{
			"common/screens/dialog_box/confirm_red",
			"",
			"",
			"",
		},
		default = true,
		command = function()
					local name = GetLabel("editRequestName");
					name = trim(name);
					if name == "" then
                        DoModal("scripts/windows/dialogs/entername.lua");
                        SetFocus("editRequestName")
                    elseif UserExists(name) then
                        DoModal("scripts/windows/dialogs/nameinuse.lua");
						SetFocus("editRequestName")
					else
						InsertUser(name)
						CloseWindow()
						SetWelcomeName()
					end
				  end
	},
		
	SetStyle(GreenButtonStyle),
		
	-- Cancel button
	BlgButton
	{
		name = "btnCancel",
		x = 499, y = 495,
		label = "cancel",
		width = 300, height = 300,
		close = true,
		positionMatrix = {x = 10, y = 10},
		graphics =
		{
			"common/screens/dialog_box/cancel_green",
			"",
			"",
			"",
		},
		},
		
	},
}

SetFocus("editRequestName")

if UserCount() == 0 then
	EnableWindow("btnCancel", false)
	PositionWindowMainMenu({name="btnOK", x=420 ,y=495,})
end