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",
		  x=kCenter,
		  y=kCenter,

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

		  -- поле ввода имени игрова
		  Bitmap
		  {
			  image="Menu/textfield.png",
			  x=kCenter,
			  y=130,
			  scale = 1.1,

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

}

SetFocus( "nameedit" );

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