require( "scripts/style.lua" );
require( "scripts/ui/style_text.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 ] );
        SetShadowLabel( "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

--	DebugOut("UpdatePlayerChoices");
end

ButtonX = kCenter;
ButtonY = 60;
ButtonYSpace = 30;

kMaxUsers = 10;

NameButtonGraphics = {
    "buttons/choosenameover.png",
    "buttons/choosenamedown.png",
    "buttons/choosenameover.png",
    "buttons/choosenamedown.png"
};	

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

--gSelected = -1 ;

function AddButtons( N )

    t = { SetStyle(NameStyle), BeginGroup() };

    for i = 0,N-1 do
        table.insert(t,
            PlayerButton{
                font = CreateNamedFont(MinionSbItSymbolSet, 16, WhiteColor),
                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",
    BitmapVisual
    {
        name  = "bmp_fade_menu",
        image = "cutscenes/black",
        alpha_val = 0.5,
        --scale_val = 2000.0,
        x = 512, y = 384,
        w = 1024, h = 768,
        
        Bitmap
        { 
            image="backgrounds/largedialog",
            x=kCenter,
            y=kCenter,
            
            ChoosePlayerWindow
            {
                x = 0, y = 0, w=kMax, h=kMax,
                
                SetStyle(TextStyle),
                CreateMinionSbItText(24, CaptionBrown, kCenter+1, 31, "#"..GetString("caption_outline", GetString("selectaplayer")), 250, 24),
                CreateMinionSbItText(24, CaptionYellow, kCenter, 30, "#"..GetString("caption_outline", GetString("selectaplayer")), 250, 24),

                AddButtons( kMaxUsers );
                
                SetStyle(ButtonStyle);
                Button
                {
                    x=60,
                    y=360,
                    name="editname",
                    label="",
                    label="editname",
                    font = CreateNamedFont(MinionSbItSymbolSet, 20, ButtonBrown),
                    command = function()
                        gEnterNameValue = GetSelectedPlayerName();
                        name = DoModal("scripts/entername.lua");
                        if (not(name == nil)) then
                            SetCurrentUser(GetCurrentSelectedUser());
                            ChangeCurrentUserName(name);
                            SetPlayerChoices();
                            SetWelcomeName();
                        end
                    end
                };

                Button
                {
                    x=220,
                    y=415,
                    name="chooseok",
                    label="ok",
                    default = true,
                    cancel=true,    -- ALSO allow them to exit the dialog by hitting escape
                    font = CreateNamedFont(MinionSbItSymbolSet, 20, ButtonBrown),
                    command = function()
                        SetCurrentUser(GetCurrentSelectedUser());
                        SetWelcomeName();
                        --UpdateGems();
                        PlaySound("audio/sfx/main_menu/screenswitch.ogg");
                        PopModal("chooseplayer");
                    end
                    
                };
                    
                Button
                {
                    x=220,
                    y=360,
                    name="newplayer",
                    label="newplayer",
                    font = CreateNamedFont(MinionSbItSymbolSet, 20, ButtonBrown),
                    command = function()
                        gEnterNameValue = "";
                        name = DoModal("scripts/entername.lua");
                        if (not(name == nil)) then
                            CreateNewUser(name);
                            kSwapToGame=false;
                            kCutScene1Showed=false;
                            SetPlayerChoices()
                        end
                    end
                };
                    
                Button
                {
                    x=60,
                    y=415,
                    name="delete",
                    label="deleteplayer",
                    font = CreateNamedFont(MinionSbItSymbolSet, 16, ButtonBrown),
                    command =
                        function()
                            result = DisplayDialog {
                                "scripts/dlg_yesno.lua",
                                body ="deleteconfirm",
                                title = "deletetitle",
                                image = "interface/dlg_yes_no/delete_user",
                            };
                            if (result=='yes') then
                                DeleteUser(GetCurrentSelectedUser())
                                SetPlayerChoices()
                            end
                        end
                    
                };
            },
        },
    },
}


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

--SetPlayerChoices();

