--============================================================================
--  entername.lua
--  Copyright (c) 2009, Bamtang Games. All Rights Reserved.
------------------------------------------------------------------------------
require( "scripts/style.lua" );

NameFont = {
    textFont,
    22,
    WhiteColor
};

local playername = gEnterNameValue;
local deleteselected = false;

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

RadioFont = {
    titleFont,
    20,
    Color(255, 242, 108),
};

kRbtTimedX = 124;
kRbtTimedY = 176;
kRbtRelaxedX = 257;
kRbtRelaxedY = 175;
kRbtLabelSpace = 30;

MakeDialog {
    name = "EnterName";
    RegisterUserWindow {
        x = 0; y = 0;
        w = 800; h = 600;
        Bitmap {
            name = "bmpPanelBlack",
            image = "images/gui/ae_background",
            alpha = true;
            x = 0, y = 0,
            w = 800, h = 600,
        },
        Bitmap {
            image = "images/gui/ae_paneloptions",
            name = "bmpPanelContainer",
            x = kCenter,
            y = kCenter,
            Bitmap {
                name = "bmpTitle";
                image = gLocalizedPath.."images/ae_enteryourname";
                x = kCenter;
                y = 72;
                scale = 0.8;
            };
            SetStyle(RadioStyle);
            BeginGroup();
            Button {
                name = "rbtTimed";
                x = kRbtTimedX;
                y = kRbtTimedY;
                scale = 0.8;
                --command = SetTimedMode(true);
                font = RadioFont;
                label = "lblTimed";
                w = 125;
                flags = kVAlignCenter;
                tx = 32;
            };
            Button {
                name = "rbtRelaxed";
                x = kRbtRelaxedX;
                y = kRbtRelaxedY;
                scale = 0.8;
                --command = SetTimedMode(false);
                font = RadioFont;
                label = "lblRelaxed";
                w = 125;
                flags = kVAlignCenter;
                tx = 32;
            };
            Bitmap {
                image = "images/gui/ae_enteryourname_text",
                x = kCenter,
                y = 125,
                TextEdit {
                    font = NameFont,
                    name = "nameedit",
                    label = playername,
                    flags = kVAlignCenter + kHAlignCenter,
                    enablewindow = "enterok",
                    utf8 = true,
                    clearinitial = deleteselected,
                    x = 4, y = 0,
                    w = kMax - 4, h = kMax,
                    length = gMaxLengthUserName,
                    ignore = kIllegalNameChars
                };
            };
            SetStyle(ButtonStyle),
            Button {
                x = 51,
                y = 223,
                name = "enterok",
                graphics = OkButtonGraphics;
                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
                            SetTimedMode(GetRbtSelected() == 0);
                            CloseWindow(newname);
                        end
                    end
            },
            Button {
                x = 240, y = 223,
                name = "entercancel",
                graphics = CancelButtonGraphics;
                cancel = true,
                command = function() CloseWindow(nil); end;
            };
        },
    },
}
SetFocus("nameedit");
if (deleteselected) then
    EnableWindow("enterok", false);
else
    EnableWindow("rbtTimed", false);
    EnableWindow("lblTimed", false);
    EnableWindow("rbtRelaxed", false);
    EnableWindow("lblRelaxed", false);
end
