require( "scripts/style.lua" );
require( "scripts/ui/style_text.lua" );

local playername = gEnterNameValue;
local deleteselected = false;

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

local dlgname = "chooseplayer";
local dlg_timer = {
    name = "panels",
    category = "UI",
};

local kOffBtnX = 85;
local kOffBtnY = 95;
local kResult = nil;

MakeDialog
{
    WindowExt
    {
        x=512,y=384,
        fit=true,
        
        name = dlgname,
        
        state_handlers = {
            hidden = function()
                CloseWindow(kResult);
            end;
        },
        
        Bitmap
        {
            image="backgrounds/dialog",
            x=kCenter,
            y=kCenter,
            
            SetStyle(TextStyle),
            CreateMinionSbItText(24, CaptionBrown, kCenter+1, 31, "#"..GetString("caption_outline", GetString("entername")), 250, 24),
            CreateMinionSbItText(24, CaptionYellow, kCenter, 30, "#"..GetString("caption_outline", GetString("entername")), 250, 24),
            SetStyle({}),
            
            Bitmap
            {
                image="backgrounds/textfield.png",
                x=kCenter,
                y=kCenter,
                scale = 1.1,

                TextEdit
                {
                    font = CreateFont(14, BlackColor),
                    name = "nameedit",
                    label = playername,
                    flags = kVAlignCenter,
                    enablewindow = "enterok",
                    utf8=true,
                    clearinitial = deleteselected,
                    x=4,y=0,w=kMax,h=kMax,
                    length=20,
                    ignore = kIllegalNameChars
                };
            };

            SetStyle(ButtonStyle),
            Button
            {
                x = kCenter - kOffBtnX,
                y = kMax - kOffBtnY,
                font = StandardButtonFont,
                graphics = StandardButtonGraphics,
                sound = kMenuButtonSound,
				rolloversound = kMenuRolloverSound,
                name="enterok",
                label="ok",
                type = kPush,
                default=true,    -- Make this the default button
                font = CreateNamedFont(MinionSbItSymbolSet, 24, ButtonBrown),
                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
						PlaySound("audio/sfx/main_menu/error.ogg");
                        DisplayDialog {
                            "scripts/ok.lua",
                            body ="nameinuse",
                            title = ""
                        };
                        SetFocus( "nameedit" )
                    else
                        kResult = newname;
                        HideControl(dlgname);
                    end
                    --HideControl(dlgname);
                    --CloseWindow();
                end,
            },

            Button
            {
                x = kCenter + kOffBtnX,
                y = kMax - kOffBtnY,
                font = StandardButtonFont,
                graphics = StandardButtonGraphics,
                sound = kMenuButtonSound,
				rolloversound = kMenuRolloverSound,
                name="entercancel",
                label="cancel",
                type = kPush,
                font = CreateNamedFont(MinionSbItSymbolSet, 24, ButtonBrown),
                command = function()
                    HideControl(dlgname);
                end
            };

            -- LAMP FX

            WndFxSprite
            {
                name = "left_lamp_firebugs_fx",
                x = 0,
                y = 0,
                w = 580,
                h = 310,
                effect_position = "67,96",
                file_name = "fx/lamp_firebugs.lua",
                start_activate = true,
                advance = 50,
                timer_settings = {
                    name = "control",
                    category = "control",
                },
            },

            WndFxSprite
            {
                name = "right_lamp_firebugs_fx",
                x = 0,
                y = 0,
                w = 580,
                h = 310,
                effect_position = "518,96",
                file_name = "fx/lamp_firebugs.lua",
                start_activate = true,
                advance = 50,
                timer_settings = {
                    name = "control",
                    category = "control",
                },
            },

            WndFxSprite
            {
                name = "left_lamp_light_fx",
                x = 0,
                y = 0,
                w = 580,
                h = 310,
                effect_position = "67,96",
                file_name = "fx/lamp_light.lua",
                start_activate = true,
                advance = 50,
                timer_settings = {
                    name = "control",
                    category = "control",
                },
            },

            WndFxSprite
            {
                name = "right_lamp_light_fx",
                x = 0,
                y = 0,
                w = 580,
                h = 310,
                effect_position = "518,96",
                file_name = "fx/lamp_light.lua",
                start_activate = true,
                advance = 50,
                timer_settings = {
                    name = "control",
                    category = "control",
                },
            },
        },
    },
}

SetFocus( "nameedit" );

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


