require( "scripts/style.lua" );
require( "config/config.cfg" );


-- MyFont = {
  -- standardFont,
  -- 30,
  -- BlackColor
-- };

NameFont = {
  standardFont,
  15,
  BlackColor
};

local tribename = gEnterNameValue;
local deleteselected = false;

if not tribename or tribename=="" then
	deleteselected = true;
	tribename = "" ;
end

function test_tribe_name()
  local newname = GetLabel( "nameedit" );
  if( not( IsValid( newname ) ) ) then
    DisplayDialog {
      "scripts/ok.lua",
      body ="not_valid_name",
      title = ""
    };
    SetFocus( "nameedit" )
  else
    if( IsNameInUse( newname ) ) then
      DisplayDialog {
        "scripts/ok.lua",
        body ="nameinuse",
        title = ""
      };
      SetFocus( "nameedit" )
    else
      CloseWindow(newname);
    end
  end
end


MakeDialog
{
	Bitmap
	{
		image="backgrounds/speedmenu",
		x=kCenter,
		y=kCenter,

		Text
		{
			font = BookAntiquaTitle28Font,
			name = "create_first_tribe_title",
			label = "enternameprompt",
			x=12, y=30,
			w=kMax,h=kMax,
			flags = kVAlignTop + kHAlignCenter,
		};
		
		Text
		{
			font = NameFont,
			name = "4charmin",
			x=0,y=70,w=kMax,h=30,
			flags = kHAlignCenter + kVAlignTop,
			label = "4charmin",
		}; 
		
		SetStyle(UpHUDStyle),
		Text
		{
			name = "name",
			x=190,y=150,
			w=70,h=40,
			flags = kVAlignCenter + kHAlignCenter,
			label= "Name",
		};

		Bitmap
		{
			image="backgrounds/textfield.png",
			x=270,
			y=150,

			TextEdit
			{
				font = NameFont,
				name = "nameedit",
				label = tribename,
				flags = kVAlignCenter,
				enablewindow = "enterok",
        clearonselection = false,
				utf8=true,
				x=4,y=0,w=kMax-4,h=kMax,
        minacceptedlength = 4,
				length = tribe_name_max_length,
				ignore = kIllegalNameChars,
				command = test_tribe_name,
			};
		}; 

	SetStyle( OptionsValidationButtonStyle ),
    Bitmap
    {
      name = "fake_ok",
      x = kCenter - 75,
      y = kMax - 90,
      
      image = "buttons/menubuttondown.png",
      
      Text
      {
        flags = kVAlignCenter + kHAlignCenter,
        label = "ok",
        w = kMax, h = kMax,
      },
    },
    
    Button
    {
      name = "enterok",
      x = kCenter - 75,
      y = kMax - 90,
      w = 128,
      h = 53,
      label = "ok",
      default=true,	-- Make this the default button
      command = test_tribe_name,
        
    },
    
    SetStyle(OptionsCancelButtonStyle),
    Button
    {
      name = "quitcancel",
      x = kCenter + 75,
      y = kMax - 90,
      label = "cancel",
      command = 
        function()
          CloseWindow( nil );
        end
      
    },
	},

}

SetFocus( "nameedit" );

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


