--============================================================================
--  enter_name.lua
--  Copyright (c) 2010, Bamtang Games. All Rights Reserved.
------------------------------------------------------------------------------
require( "scripts/style.lua" );

HeadFont = {
    TextFont;
    24;
    PanelColor
};

BodyFont = {
	TextFont;
	20;
	PanelColor
};

local playerName = gEnterNameValue;
local deleteSelected = false;

if not playerName or playerName == "" then
    deleteSelected = true;
    playerName = GetString("enternameprompt") ;
end

MakeDialog {
    name = "EnterNameDialog";

    Bitmap {
        name = "bmpBgAlpha";
        image = "gpx/screens/transparent_background";
        x = 0; y = 0;
    };

    wndMove {
        x = kCenter; y = kCenter;
		w = kMax; h = kMax;

		Bitmap {
			image = "gpx/screens/panel_newplayer_base";
			name = "bmpPanelContainer";
			x = 250; y = kCenter;

			Text {
				name = "lblTitle";
				font = Font24;
				x = kCenter + 20; y = 48;
				w = 400; h = 50;
				flags = kHAlignCenter;
				label = "lbl_enteraname";
			};

			Bitmap {
				image = "gpx/screens/player_name_scroll";
				x = 156;
				y = 99;

				swtTextEdit {
					font = Font20White; --BodyFont;
					name = "nameedit";
					label = playerName;
					flags = kVAlignCenter + kHAlignCenter;
					windowToEnable = "enterok";
					utf8 = true;
					clearAtBeginning = deleteSelected;
					x = 4; y = 0;
					w = kMax - 4; h = kMax;
					length = gMaxLengthUserName;
					ignore = kIllegalNameChars
				};
			};

			SetStyle(PanelButtonStyle);
			Button {
				x = 126; y = 163;
				name = "enterok";
				label = "btnOk";
				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/ok.lua";
								body ="lbl_enteraname";
							};
							SetFocus("nameedit")
						elseif (newname ~= playerName and gsw_isNameInUse(newname)) then
							DisplayDialog {
								"scripts/ok.lua";
								body ="nameinuse";
							};
							SetFocus("nameedit");
						else
							CloseWindow(newname);
						end
					end
			};
			Button {
				name = "btnCancel";
				label = "btnCancel";
				x = 291; 
				y = 163;
				cancel = true;
				command = 
					function() 
						CloseWindow(nil); 
					end
			};
		},
	};
}

SetFocus("nameedit");
if (deleteSelected) then
    EnableWindow("enterok", false);
end
