-- requestname.lua
require( "scripts/styles/accountsstyle.lua" );
require( "scripts/styles/uistyle.lua" );

local function trim (s)
	return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end

local initText = "";

if GetNumUsers() == 0 then
	initText = "#"..GetString("player").." 1";
end

local function createCancelButton()
	local t = {};

	if GetNumUsers() ~= 0 then
		t = {
			-- Cancel button
			SetStyle(Button2Style),
			BlgButton{
				name="cancelBtn",
				transformMatrix = {x = 572, y = 413},
				label = "wnd_requestname_cancel",
				close = true,
				
				upState = {
					bitmap = "common/buttons/btn_default",
				},
				overState = {
					bitmap = "common/buttons/btn_rollover",
				},
				downState = {
					bitmap = "common/buttons/btn_pushed",
					sound = kDefaultButtonSound,
				},
			},
		};
	end
	
	return Group(t);
end

MakeState
{
	BlgBitmap{
		image = "common/backgrounds/alpha_black",
		name = "mainmenuBG",
		center = {x = 0, y = 0},
	},
	
	BlgMovieClip {
		transformMatrix = {x = 0, y = 0},

		SetStyle(WindowBGStyle),
		
		BlgBitmap {
			image = "common/backgrounds/umbrella",
			center = {x = 0, y = 0},
			transformMatrix = {x = 697, y = 134},
		},
		BlgBitmap {
			image = "common/popups/quit/box",
			center = {x = 0, y = 0},
			transformMatrix = {x = 198, y = 199},
		},
		BlgBitmap {
			image = "common/backgrounds/text_container",
			center = {x = 0, y = 0},
			transformMatrix = {x = 305, y = 310},
		},
		BlgContainer{
			transformMatrix = {x = 168, y = 199},

			
				
			-- Create the label that asks for the name
			BlgText
			{
				transformMatrix = {x = 240, y = 50},
				w = 300, h = ChangePlayerLabelFont[2] + 6,
				flags = kHAlignLeft | kVAlignTop,
				label = "wnd_requestname_title",
				font = ChangePlayerLabelFont,
				--dropShadow = true,
			},
			
			-- Create the edit box where the name must be inserted
			CustomTextField
			{
				name = "editRequestName",
				transformMatrix = {x = 230, y = 140},
				w = 300, h = 24,
				label = initText,
				font = {
					standardFont,
					18,
					BlackColor,
				},
				length = 9,
				ignore = kIllegalNameChars,
				background = true,
			},
		},

		--SetStyle(WindowLogoStyle),
		BlgBitmap{
			image = "common/backgrounds/logo_01_big",
			name = "mainmenuBG",
			center = {x = 0, y = 0},
			transformMatrix = {x = 151, y = 168},
		},
--~ 		BlgMovieClip{
--~ 			SetStyle(LogoCircleStyle),
--~ 			repeatCount = -1,
--~ 			BlgBitmap{
--~ 				image = "common/backgrounds/logo_bg",
--~ 				name = "mainmenuBG",
--~ 				center = {x = 0, y = 0},
--~ 				transformMatrix = {x = 19, y = 112},
--~ 			},
--~ 			SetStyle(nil),
--~ 			BlgBitmap{
--~ 				image = "common/backgrounds/logo_menu",
--~ 				name = "mainmenuBG",
--~ 				center = {x = 80, y = 32},
--~ 				playAnim = "DoAnim",
--~ 			},
--~ 		},

		SetStyle(Button1Style),
		BlgButton{
			name="okBtn",
			transformMatrix = {x = 413, y = 416},
			label = "wnd_requestname_ok",
			
			upState = {
				bitmap = "common/buttons/btn_default",
			},
			overState = {
				bitmap = "common/buttons/btn_rollover",
			},
			downState = {
				bitmap = "common/buttons/btn_pushed",
				sound = kDefaultButtonSound,
			},
			command = function()
				local name = BlgGetLabel("editRequestName");
				name = trim(name);

				if name ~= "" and not IsNameInUse(name) then
					CreateNewUser(name);
					LoadUserData();
					SetWelcomeName();
					PopState();
				else
					BlgSetFocus("editRequestName");
				end
			end
		},
		
		createCancelButton();
	},
};

BlgSetDefaultButton("okBtn");

BlgSetFocus("editRequestName");

if GetNumUsers() == 0 then
	BlgSetCancelButton("okBtn");
else
	BlgSetCancelButton("cancelBtn");
end