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

local playerName = gEnterNameValue;
local deleteSelected = false;

local panelTitle = "lblEditAvatar";

local btnOkX = 527;

if playerName == "" then
    deleteSelected = true;
    playerName = GetString("enternameprompt") ;
    panelTitle = "lblSelectAvatar";
end

if gTypeAvatar == "" then
    gTypeAvatar = "boy";
end

if (gRegisterMode == "nocancel") then
	btnOkX = 512 - 54 + 5;
end

MakeDialog {
    name = "RegisterUserDialog";

    Bitmap {
        image = "gpx/screens/newplayer_background";
        name = "bmpPanelBlack";
        x = 0; y = 0;
        autoscale = true;
    };

    wndRegisterUser {
        name = "RegisterWindow";
        x = 0; y = 0; w = kMax; h = kMax;

        avatar_default = gTypeAvatar;

        avatar_boy  = { x = 336; y = 128 };
        avatar_girl = { x = 336; y = 125 };
        avatar_cat  = { x = 366; y = 190 };
        avatar_dog  = { x = 330; y = 130 };

        clsText {
            name = "lblTitle";
            font = Font44White;
            x = kCenter; y = 23;
            w = 800; h = 100;
            flags = kHAlignCenter + kVAlignCenter;
            label = panelTitle;
            Scale = 0.475;
            Space = -10;
        };

        SetStyle(CharacterRadioButtonStyle);
        BeginGroup();
        Button {
            name = "btnAvatar_boy";
            x = 24; y = 85;
            on = true;
            graphics = {
                "anim/character/mw_mavatar_select_off_thumb";
                "anim/character/mw_mavatar_select_on_thumb";
                "anim/character/mw_mavatar_select_on_thumb";
                "anim/character/mw_mavatar_select_on_thumb";
            };
            command = function()
                gTypeAvatar = "boy";
                gfn_setAvatarBoy();
            end
        };
        Bitmap {
            name = "bmpAvatarLocked_boy";
            image = "anim/character/mw_avatar_locked";
            x = 24; y = 85;
        };

        Button {
            name = "btnAvatar_girl";
            x = 708; y = 85;
            graphics = {
                "anim/character/mw_favatar_select_off_thumb";
                "anim/character/mw_favatar_select_on_thumb";
                "anim/character/mw_favatar_select_on_thumb";
                "anim/character/mw_favatar_select_on_thumb";
            };
            command = function()
                gTypeAvatar = "girl"
                gfn_setAvatarGirl();
            end
        };
        Bitmap {
            name = "bmpAvatarLocked_girl";
            image = "anim/character/mw_avatar_locked";
            x = 708; y = 85;
        };

        Button {
            name = "btnAvatar_cat";
            x = 24; y = 343;
            graphics = {
                "anim/character/mw_cavatar_select_off_thumb";
                "anim/character/mw_cavatar_select_on_thumb";
                "anim/character/mw_cavatar_select_on_thumb";
                "anim/character/mw_cavatar_select_on_thumb";
            };
            command = function()
                gTypeAvatar = "cat"
                gfn_setAvatarCat();
            end
        };
        Bitmap {
            name = "bmpAvatarLocked_cat";
            image = "anim/character/mw_avatar_locked";
            x = 24; y = 343;
        };

        Button {
            name = "btnAvatar_dog";
            x = 708; y = 343;
            graphics = {
                "anim/character/mw_davatar_select_off_thumb";
                "anim/character/mw_davatar_select_on_thumb";
                "anim/character/mw_davatar_select_on_thumb";
                "anim/character/mw_davatar_select_on_thumb";
            };
            command = function()
                gTypeAvatar = "dog"
                gfn_setAvatarDog();
            end
        };
        Bitmap {
            name = "bmpAvatarLocked_dog";
            image = "anim/character/mw_avatar_locked";
            x = 708; y = 343;
        };

        Bitmap {
            image = "gpx/screens/player_name_podium";
            name = "bmpPanelBase";
            x = 328; y = 589;

            Bitmap {
                image = "gpx/screens/player_name_scroll_01";
                x = 66; y = 40;
            };
			Bitmap {
				x = 51; y = 29;
				name = "bmpPanelBase_h";
				image = "gpx/screens/player_name_scroll_over";
			};
            swtTextEdit {
                font = Font28;
                name = "nameedit";
                label = playerName;
                flags = kVAlignTop + kHAlignCenter;
                windowToEnable = "enterok";
                utf8 = true;
                clearAtBeginning = deleteSelected;
                x = 4; y = 50;
                w = kMax - 4; h = kMax;
                length = gMaxLengthUserName;
                ignore = kIllegalNameChars
            };
        };

        SetStyle(SmallButtonStyle);
        Bitmap {
            x = btnOkX; y = 718;
            image = "gpx/buttons/buttonsmall_i";
			Text {
				x = 0; y = 0; w = kMax; h = kMax;
				label = "btnOk";
				flags = kVAlignCenter + kHAlignCenter;
			};
        };

        Button {
            x = btnOkX; y = 718;
            name = "enterok";
            label = "btnOk";
            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+$", "");
                    newname = string.lower(newname);
                    if (string.len(newname) == 0) then
                        DisplayDialog {
                            "scripts/ok.lua";
                            body ="lbl_enteraname";
                        };
                        SetFocus("nameedit")
                    elseif (newname ~= playerName and gsw_isNameInUse(newname)) then
                        DisplayDialog {
                            "scripts/ok.lua";
                            body ="nameinuse";
                        };
                        SetFocus("nameedit");
                    else
                        CloseWindow(newname);
                    end
                end
        };

        Button {
            name = "btnCancel";
            label = "btnCancel";
            x = 402; y = 718;
            cancel = true;
            command = function()
                CloseWindow(nil);
            end
        };
    };
};

SetFocus("nameedit");

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

if (gRegisterMode == "nocancel") then
	EnableWindow("btnCancel", false);
end

