require( "scripts/style.lua" );


MyFont = {
  standardFont,
  42,
  WhiteColor
};

TextFieldFont = {
  standardFont,
  30,
  BlackColor
};

local playername = gEnterNameValue;
local deleteselected = false;

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


MakeDialog
{
  FadeWindow
  {
  	name = "entername",
    x = 0, y = 0,
		w = ScreenWight, h = ScreenHeight,
		Bitmap
	  {
		  image="menu/dialog_01_1",
		  x=kCenter,
		  y=kCenter,
		},

    Bitmap
    {
	    image="menu/dialog_01",
	    x=kCenter,
	    y=kCenter,
	   },

		  -- поле ввода имени игрока
		  Bitmap
		  {
			  image="Menu/textfield.png",
			  x=kCenter,
			  y=220+90,
			  scale = 0.9,
			},

		  TextEdit
		  {
			  font = TextFieldFont,
			  name = "nameedit",
			  label = playername,
			  flags = kHAlignCenter+kVAlignCenter,
			  enablewindow = "enterok",
			  utf8=true,
			  clearinitial = deleteselected,
			  x=kCenter,y=220+96,w=400,h=50,
			  length=10,
			  ignore = kIllegalNameChars
		  };
		  
 		  SetStyle( MenuButtonCross ),
		  QEMenuButton
		  {
			  x=kCenter+100, y=370+70,
			  name="entercancel",
			   cancel=true,
			  command = function() 
			  CloseFade("entername","CloseWindow()");
			  end;
		  };
  		
		  SetStyle( MenuButtonOk ),
		  QEMenuButton
		  {
			  x=kCenter-100, y=370+70,
			  name="enterok",
			  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
					    PlaySfx(mainMenuOpenSound);
						  DisplayDialog {
							  "scripts/ok.lua",
							  body ="pleaseenteraname",
							  title = ""
						  };
						  SetFocus( "nameedit" )
					  elseif (newname ~= playername and IsNameInUse(newname)) then
					    PlaySfx(mainMenuOpenSound);
						  DisplayDialog {
							  "scripts/ok.lua",
							  body ="nameinuse",
							  title = ""
						  };
						  SetFocus( "nameedit" )
					  else
					    CloseFade("entername","CloseWindow('"..newname.."')");
					  end
				  end
		  },
  		
	}
}

SetFocus( "nameedit" );

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