require( "scripts/style.lua" );
require( "scripts/ui/style_text.lua" );

LeftButtonGraphics = {
    "buttons/arrowleft_up",
    "buttons/arrowleft_down",
    "buttons/arrowleft_over"
};


kNumberX = 37;
kNameX = 62;
kScoreX = 335;
kP1X = kNumberX + 1;

kNameW = 250
kNameH = 21

eLocalView=0;
eRequestingCategories=1;
eRequestingScores=2;
eSubmitting=3;
eGlobalView=4;
eError=5;

kRowStartY = 75;
kScoreRowSpace = 35;
kPlaceOffX = 35;
kNameOffX = 65;
kScoreOffX = 320;

function UpdateButtons()
    local state = GetState();
    local localHS = IsEnabled(kHiscoreLocalOnly);
    local anonHS = IsEnabled(kHiscoreAnonymous);
    local fullHS = (not localHS) and (not anonHS);

    EnableWindow("view",false);
    EnableWindow("viewlocal",false);
    EnableWindow("submit",false);
    EnableWindow("categoryleft",false);
    EnableWindow("categoryright",false);
    
    if (state==eLocalView) then
        local eligibleScore;

        if fullHS then
            EnableWindow("moreinfo",true);
        else 
            EnableWindow("moreinfo",false);
        end
        
        if not localHS then
            EnableWindow("view",true);

            if (ScoreAvailable()) then
                EnableWindow("submit",true);
            end
        end
    elseif (state == eGlobalView) then
        EnableWindow("viewlocal",true);
        EnableWindow("categoryleft",true);
        EnableWindow("categoryright",true);
    elseif (state == eError) then
        EnableWindow("viewlocal",true);
    end

end

fn_Spawn_Score_Block = function(index)
    return Group
    {
        Bitmap
        {
            image = "hiscore/p1icon",
            name = "p1_"..tostring(index),
            x = kP1X, y = kRowStartY + (index - 1) * kScoreRowSpace,
        },
        AppendStyle({flags = kVAlignCenter + kHAlignRight,}),
        CreateMinionNamedShadowText(20, WhiteColor, 
            kPlaceOffX, kRowStartY + (index - 1) * kScoreRowSpace,
            tostring(index)..".", 25, 20, tostring(index)),
            
        AppendStyle({flags = kVAlignCenter + kHAlignLeft,}),
        CreateMinionNamedShadowText(20, WhiteColor,
            kNameOffX, kRowStartY + (index - 1) * kScoreRowSpace,
            "#name", 250, 20, "name"..tostring(index)),
            
        AppendStyle({flags = kVAlignCenter + kHAlignRight,}),
        CreateMinionNamedShadowText(20, WhiteColor,
            kScoreOffX, kRowStartY + (index - 1) * kScoreRowSpace,
            "12345", 50, 20, "score"..tostring(index)),
        AppendStyle({flags = kVAlignCenter + kHAlignCenter,}),
    };
end;

MakeDialog
{
    -- Name the modal dialog
    name="hiscorescreen",
    BitmapVisual
    {
        name  = "bmp_fade_menu",
        image = "cutscenes/black",
        alpha_val = 0.5,
        --scale_val = 2000.0,
        x = 512, y = 384,
        w = 1024, h = 768,
        
        HiscoreWindow
        {
            x=0,y=0,h=kMax,w=kMax,

            Bitmap
            {
                x = kCenter,
                y = kCenter,
                image="backgrounds/largedialog",
                name="leftpanel",

                -- [!!!] changing code required to remove shadow [!!!]
                SetStyle(TextStyle),
                CreateMinionNamedText(34, CaptionBrown, kCenter+1, 31, "#"..GetString("caption_outline", GetString("localhighscores")), 350, 34, "shadow_local"),
                CreateMinionNamedText(34, CaptionYellow, kCenter, 30, "#"..GetString("caption_outline", GetString("localhighscores")), 350, 34, "local"),
                
                CreateMinionNamedText(34, CaptionBrown, kCenter+1, 31, "#"..GetString("caption_outline", GetString("topplayers")), 350, 34, "shadow_topplayers"),
                CreateMinionNamedText(34, CaptionYellow, kCenter, 30, "#"..GetString("caption_outline", GetString("topplayers")), 350, 34, "topplayers"),
                
                fn_Spawn_Score_Block(1),
                fn_Spawn_Score_Block(2),
                fn_Spawn_Score_Block(3),
                fn_Spawn_Score_Block(4),
                fn_Spawn_Score_Block(5),
                fn_Spawn_Score_Block(6),
                fn_Spawn_Score_Block(7),
                fn_Spawn_Score_Block(8),
                fn_Spawn_Score_Block(9),
                fn_Spawn_Score_Block(10),
                
                SetStyle(ButtonStyle),
                Button
                {
                    graphics = LeftButtonGraphics,
                    rotate=true,
                    name="scrollup",
                    x=kMax-75,
                    y=60,
                };

                Button
                {
                    graphics = LeftButtonGraphics,
                    rotate=true,
                    hflip=true,
                    name="scrolldown",
                    x=kMax-75,
                    y=kMax -100,
                };

                Button
                {
                    x=kCenter,
                    y=kMax - 80,
                    name="back",
                    type = kPush,
                    flags = kHAlignCenter + kVAlignCenter,
                    label="back",
                    font = CreateNamedFont(MinionSbItSymbolSet, 24, ButtonBrown),
                    command = function()
                        PopModal("hiscorescreen"); -- take off high score menu, restart game loop
                    end
                };
            };
        },
    },
}