--============================================================================
--  chooseplayer.lua
--  Copyright (c) 2009, Bamtang Games. All Rights Reserved.
------------------------------------------------------------------------------

require("scripts/style.lua");

function ShowSubmitOptions(show)
    EnableWindow("delete", show)
    EnableWindow("chooseok", show);
    EnableWindow("editname", show);
end

function ShowCreateOptions(show)
    EnableWindow("newplayer", show);
end

function UpdatePlayerChoices(curUser, numUsers, userTable)

    if (numUsers >= kMaxUsers) then
        ShowCreateOptions(false);
    else
        ShowCreateOptions(true);
    end

    if (numUsers == 0) then
        ShowSubmitOptions(false);
    else
        ShowSubmitOptions(true);
    end

    for i = 0, numUsers - 1 do
        SetLabel("button"..i, userTable[i + 1]);
        EnableWindow("button"..i, true);
    end

    for i = numUsers, kMaxUsers do
        EnableWindow("button"..i, false);
    end

    if (numUsers > 0) then
        SetButtonToggleState("button"..curUser, true);
    end
end

NameFont = {
    titleFont,
    22,
    Color(252, 226, 115, 255)
};

ButtonX = kCenter;
ButtonY = 140;
ButtonYSpace = 38;

kMaxUsers = 7;

NameButtonGraphics = {
    "images/gui/choosenameover",
    "images/gui/chosenamedown",
    "images/gui/choosenameover",
    "images/gui/chosenamedown"
};

NameStyle= {
    type = kRadio,
    flags = kHAlignCenter + kVAlignCenter,
    font = NameFont,
    graphics = NameButtonGraphics,
    sound = kDefaultButtonSound
};

function AddButtons(N)
    t = { SetStyle(NameStyle), BeginGroup() };

    for i = 0, N - 1 do
        table.insert(t,
            Button{
                name = "button"..i,
                --command=function() gSelected = i; end,
                label = "#name",
                x = ButtonX,
                y = ButtonY + (ButtonYSpace*i),
            }
        );
    end

    return Group(t);
end

MakeDialog {
 -- Name the modal dialog
    name = "chooseplayer",

    ChoosePlayerWindow {
        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 {
            name = "bmpPanelContainer";
            image = "images/gui/ae_panelselectplayer";
            x = kCenter;
            y = kCenter;

            Bitmap {
                name = "bmtTitle";
                image = gLocalizedPath.."images/chooseplayer/ae_selectaplayer";
                x = kCenter;
                y = 70;
            };

            AddButtons(kMaxUsers);

            SetStyle(ButtonStyle);
            Button {
                x = 83;
                y = 412;
                name = "editname";
                graphics = EditNameButtonGraphics;
                command =
                    function()
                        gEnterNameValue = GetSelectedPlayerName();
                        name = DoModal("scripts/entername.lua");
                        if (not(name == nil)) then
                            SetCurrentUser(GetCurrentSelectedUser());
                            ChangeCurrentUserName(name);
                            SetPlayerChoices();
                            SetWelcomeName();
                        end
                    end
            };

            Button {
                x = 281,
                y = 461,
                name = "chooseok",
                graphics = OkNameButtonGraphics;
                default = true,
                cancel = true, -- ALSO allow them to exit the dialog by hitting escape
                command =
                    function()
                        SetCurrentUser(GetCurrentSelectedUser());
                        ChangeUserData();
                        SetWelcomeName();
                        PopModal("chooseplayer");
                    end
            };

            Button {
                x = 281,
                y = 412,
                name = "newplayer",
                graphics = NewPlayerButtonGraphics;
                command =
                    function()
                        gEnterNameValue = "";
                        name = DoModal("scripts/entername.lua");
                        if (not(name == nil)) then
                            CreateNewUser(name);
                            SetPlayerChoices();
                        end
                    end
            };

            Button {
                x = 83,
                y = 461,
                name = "delete",
                graphics = DeletePlayerButtonGraphics;
                command =
                    function()
                        result = DisplayDialog {
                            "scripts/yesno.lua",
                            title = "", 
                            body = "deleteconfirm" 
                        };
                        if (result=='yes') then
                            DeleteUser(GetCurrentSelectedUser())
                            SetPlayerChoices()
                        end
                    end
            };
        };
    };
}

--UpdatePlayerChoices( 0,0, {} );

--SetPlayerChoices();
