require "scripts/style"


MyFont = {
  standardFont,
  30,
  BlackColor
};

NameFont = {
  standardFont,
  25,
  BlackColor
};

local playername = gEnterNameValue;
local deleteselected = false;

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


MakeDialog
{
	Bitmap
	{
		name="enternamedialog",
		image="backgrounds/deed",
		x=kCenter,
		y=kCenter,

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

		Bitmap
		{
			image="backgrounds/dialog_textfield.png",
			x=kCenter,
			y=kCenter,
			--scale = 1.1,

			TextEdit
			{
				font = NameFont,
				name = "nameedit",
				label = playername,
				flags = kVAlignCenter,
				enablewindow = "enterok",
				utf8=true,
				clearinitial = deleteselected,
				x=14,y=0,w=kMax-14,h=kMax,
				length=10,
				ignore = kIllegalNameChars

			};
		};

		SetStyle( ButtonStyle ),

		Button
		{
			x=150, y=kMax-60,

			name="enterok",
			label="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/ok.lua",
							body ="pleaseenteraname",
							title = ""
						};
						SetFocus( "nameedit" )
					elseif (newname ~= playername and IsNameInUse(newname)) then
						DisplayDialog {
							"scripts/ok.lua",
							body ="nameinuse",
							title = ""
						};
						SetFocus( "nameedit" )
					else
						CreateNewUser(newname);
						resetRoomData();
						initRoomData(1);
						resetHintNumber();
						SetWelcomeName();
						ResetGameState();
						UpdateUpgradeTextures();
						SetGameState("innview");
						setIsANewChar(1);
						ResetUpgradeStats();
						ResetPlayedTutorials();
						saveRoom();
						saveData();
						CloseWindow(newname);
					end
				end
		},

		--Button
		--{
		--	x=250, y=kMax-60,

		--	name="entercancel",
		--	label="cancel",
		--	cancel=true,
		--	command = function() CloseWindow(nil); end;

		--};

	},

}

SetFocus( "nameedit" );

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


