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

ButtonY = 75;
ButtonYSpace = 36;

name = "none";
players = {};
gv_Current_Player = 0;
changePlayerButton = true;

--gInit = true;

function ShowSubmitOptions(show)
    EnableWindow("btnOk", show);
    EnableWindow("btnDeletePlayer", show);
    EnableWindow("btnEdit", show);
end

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

function UpdatePlayerChoices(curUser, numUsers, userTable)
    if (numUsers >= gMaximumNumberOfPlayers) then
        ShowCreateOptions(false);
    else
        ShowCreateOptions(true);
    end

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

    for i = 1, gMaximumNumberOfPlayers do
        if i <= numUsers then
            enable = true;
            players[i] = userTable[i];
            SetLabel("lblUser"..i, userTable[i]);
        else
            enable = false;
        end

        EnableWindow("btnUser"..i, enable);
        EnableWindow("lblUser"..i, enable);
    end

    gv_Current_Player = curUser + 1;
    name = userTable[gv_Current_Player];

    if changePlayerButton then
        changePlayerButton = false;
        for i = 1, gMaximumNumberOfPlayers do
            if i == gv_Current_Player then
                show = true;
            else
                show = false;
            end
            SetButtonToggleState("btnUser"..i, show);
        end
    end

end

MakeDialog {
    name = "screenChangePlayer";

    Bitmap {
        name = "bmpBgAlpha";
        image = "gpx/screens/transparent_background";
        x = 0; y = 0;
    };

    wndMove {
        x = kCenter; y = kCenter; w = kMax; h = kMax;
        wndChangePlayer {
            x = 0; y = 0; w = 1024; h = 768;

            Bitmap {
                image = "gpx/screens/panel02";
                name = "bmpBackGround";
                x = kCenter; y = kCenter;

                Bitmap {
                    image = "gpx/screens/trophy_icon_01";
                    x = 2; y = 1;
                };

                Bitmap {
                    image = "gpx/screens/trophy_icon_02";
                    x = 445; y = 1;
                };

                clsText {
                    font = Font44;
                    name = "lblSelectPlayer";
                    label = "lblSelectPlayer";
                    x = kCenter; y = 22;
                    w = kMax; h = 60;
                    flags = kHAlignCenter + kVAlignCenter;
                    Scale = 0.43;
                    Space = -10;
                };

                SetStyle(ChangePlayerRadioButtonStyle);
                Button {
                    name = "btnUser1";
                    x = kCenter + 10; y = ButtonY;
                    command = function()
                        name = players[1];
                        gv_Current_Player = 1;
                    end
                };
                Button {
                    name = "btnUser2";
                    x = kCenter + 10; y = ButtonY + ButtonYSpace;
                    command = function()
                        name = players[2];
                        gv_Current_Player = 2;
                    end
                };
                Button {
                    name = "btnUser3";
                    x = kCenter + 10; y = ButtonY + ButtonYSpace * 2;
                    command = function()
                        name = players[3];
                        gv_Current_Player = 3;
                    end
                };
                Button {
                    name = "btnUser4";
                    x = kCenter + 10; y = ButtonY + ButtonYSpace * 3;
                    command = function()
                        name = players[4];
                        gv_Current_Player = 4;
                    end
                };
                Button {
                    name = "btnUser5";
                    x = kCenter + 10; y = ButtonY + ButtonYSpace * 4;
                    command = function()
                        name = players[5];
                        gv_Current_Player = 5;
                    end
                };

                Text {
                    name = "lblUser1";
                    font = Font20White;
                    flags = kHAlignCenter + kVAlignCenter;
                    x = kCenter; y = ButtonY; w = 180; h = 33;
                };
                Text {
                    name = "lblUser2";
                    font = Font20White;
                    flags = kHAlignCenter + kVAlignCenter;
                    x = kCenter; y = ButtonY + ButtonYSpace; w = 180; h = 33;
                };
                Text {
                    name = "lblUser3";
                    font = Font20White;
                    flags = kHAlignCenter + kVAlignCenter;
                    x = kCenter; y = ButtonY + ButtonYSpace * 2; w = 180; h = 33;
                };
                Text {
                    name = "lblUser4";
                    font = Font20White;
                    flags = kHAlignCenter + kVAlignCenter;
                    x = kCenter; y = ButtonY + ButtonYSpace * 3; w = 180; h = 33;
                };
                Text {
                    name = "lblUser5";
                    font = Font20White;
                    flags = kHAlignCenter + kVAlignCenter;
                    x = kCenter; y = ButtonY + ButtonYSpace * 4; w = 180; h = 33;
                };

                SetStyle(PanelButtonStyle);
                Bitmap {
                    x = 111; y = 262;
                    image = "gpx/buttons/newrow_i";
                };
                Text {
                    x = 111; y = 262; w = 152; h = 42;
                    label = "btnNewPlayer";
                    font = Font22;
                    flags = kHAlignCenter + kVAlignCenter;
                };
                Button {
                    x = 111; y = 262;
                    name = "btnNewPlayer",
                    label = "btnNewPlayer",
                    command =
                        function()
                            gEnterNameValue = "";
                            gTypeAvatar = "";
					        gRegisterMode = "cancel";
                            name = DoModal("scripts/register_user.lua");
                            if (not(name == nil)) then
                                gsw_createNewUser(name);
                                gnf_changeAvatar(gTypeAvatar);
                                gfn_changeUserData();
                                if gInit then
                                    gfn_updateData();
                                    gfn_animOpen();
                                end

                                PopModal("screenChangePlayer");
                            end
                        end;
                };

                Bitmap {
                    x = 277; y = 262;
                    image = "gpx/buttons/newrow_i";
                };
                Text {
                    x = 277; y = 262; w = 152; h = 42;
                    label = "btnDeletePlayer";
                    font = Font22;
                    flags = kHAlignCenter + kVAlignCenter;
                };
                Button {
                    x = 277; y = 262;
                    name = "btnDeletePlayer",
                    label = "btnDeletePlayer",
                    command =
                        function()
                            result = DisplayDialog {
                                "scripts/yesno.lua",
                                title = "deleteprofile",
                                body = GetString("deleteconfirm", name);
                            };
                            if (result == 'yes') then
                                changePlayerButton = true;
                                gsw_deleteUser(gv_Current_Player-1);
                                gfn_changeUserData();
                                gfn_setPlayerChoices();
                            end
                        end
                };

                Bitmap {
                    x = 111; y = 306;
                    image = "gpx/buttons/newrow_i";

                    Text {
                        w = kMax; h = kMax;
                        label = "btnEdit";
                        font = Font22;
                        flags = kHAlignCenter + kVAlignCenter;
                    };
                };
                Button {
                    x = 111; y = 306;
                    name = "btnEdit";
                    label = "btnEdit";
                    command = function ()
                        gEnterNameValue = gfn_getSelectedPlayerName();
                        gTypeAvatar = gfn_getSelectedPlayerAvatar();
				        gRegisterMode = "cancel";
                        name = DoModal("scripts/register_user.lua");
                        if (not(name == nil)) then
                            gsw_setCurrentUser(gfn_getCurrentSelectedUser());
                            gsw_changeCurrentUserName(name);
                            gnf_changeAvatar(gTypeAvatar);
                            gfn_changeUserData();
                            if gInit then
                                gfn_updateData();
                                gfn_animOpen();
                            end

                            PopModal("screenChangePlayer");
                        end
                    end
                };

                Bitmap {
                    x = 277; y = 306;
                    image = "gpx/buttons/newrow_i";
                };
                Text {
                    x = 277; y = 306; w = 152; h = 42;
                    label = "btnOk";
                    font = Font22;
                    flags = kHAlignCenter + kVAlignCenter;
                };
                Button {
                    name = "btnOk";
                    label = "btnOk";
                    x = 277; y = 306;
                    command =
                        function()
                            gsw_setCurrentUser(gv_Current_Player-1);
                            gfn_changeUserData();
                            gfn_updateData();
                            PopModal("screenChangePlayer");
                        end
                };
            };
        };
    };
}
