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

NameFont = {
    textFont,
    22,
    WhiteColor
};

BodyFont = {
	titleFont,
	20,
	StandarColor
};

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 = 141;
kRbtTimedY = 227;
kRbtRelaxedX = 274;
kRbtRelaxedY = 227;
kRbtLabelSpace = 30;

MakeDialog {
    name = "RegisterUser";

    RegisterUserWindow {
        x = 0; y = 0;
        w = 800; h = 600;

        Bitmap {
            image = "images/panelblack",
            name = "bmpPanelBlack",
            x = 0,
            y = 0,
            w = 800, h = 600,
        },

        Bitmap {
            image = "images/gui/ae_panelenteryourname";
            name = "bmpPanelContainer",
            x = kCenter;
            y = kCenter;

            Bitmap {
                name = "bmpTitle";
                image = gLocalizedPath.."images/ae_enteryourname";
                x = kCenter;
                y = 72;
            };

            SetStyle(RadioStyle);
            BeginGroup();
            Button {
                name = "rbtTimed";
                x = kRbtTimedX;
                y = kRbtTimedY;
                scale = 0.8;
                w = 125;
                font = RadioFont;
                flags = kVAlignCenter;
                tx = 32;
                label = "lblTimed",
                --command = SetTimedMode(true);
            };
            Button {
                name = "rbtRelaxed";
                x = kRbtRelaxedX;
                y = kRbtRelaxedY;
                scale = 0.8;
                w = 125;
                font = RadioFont;
                flags = kVAlignCenter;
                tx = 32;
                label = "lblRelaxed";
                --command = SetTimedMode(false);
            };

            Bitmap {
                image = "images/gui/ae_enteryourname_text",
                x = kCenter,
                y = 138,

                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
                };
            };


            Text {
                name = "lblGameMode";
                font = BodyFont;
                x = 91, y = 193,
                w = 350, h = 30,
                label = "lblSelectMode";--"#Select a game mode";
                flags = kHAlignCenter + kVAlignCenter,
            };

            SetStyle(ButtonStyle),
            Button {
                x = kCenter, 
                y = 284,
                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
            },
        };
    },
}

SetFocus("nameedit");

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