require( "scripts/ui/colors.lua" );

standardFont = "fonts/arial.mvec";

BlackColor = Color(0,0,0,255);
GrayColor = Color(0,0,0,125);
BlueColor = Color(77,115,178,255);
DarkBlueColor = Color(40,60,120,255);
WhiteColor = Color(255,255,255,255);
MenuButtonFontColor = Color(255,255,255,255);

ScrollPlantsLeftArrowButtonGraphics = {
    "buttons/scroll_plants_arrowleft_up",
    "buttons/scroll_plants_arrowleft_down",
    "buttons/scroll_plants_arrowleft_over"
};

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

StandardButtonGraphics = {
    "buttons/buttonup",
    "buttons/buttondown",
    "buttons/buttonrollover"
};
UIButtonGraphics = {
    "buttons/uibutton",
    "buttons/uibutton_p",
    "buttons/uibutton_a"
};

LongButtonGraphics = {
    "buttons/long_button_up.png",
    "buttons/long_button_down.png",
    "buttons/long_button_over.png"
};

CheckboxButtonGraphics = {
    "buttons/checkup",
    "buttons/checkdown",
    "buttons/checkover",
    "buttons/checkdownover"
};

RolloverButtonGraphics = {
    "interface/game/pop_up_object/btn_roll",
    "interface/game/pop_up_object/btn_roll_p",
    "interface/game/pop_up_object/btn_roll_a",
};

StandardButtonFont = {
  standardFont,
  18,
  BlackColor
};

MenuButtonFont = {
  standardFont,
  22,
  MenuButtonFontColor
};

PopupFont = {
  standardFont,
  12,
  WhiteColor
};

ItemButtonFont = {
  standardFont,
  13,
  BlackColor
};
InfoNameFont = {
  standardFont,
  14,
  WhiteColor
}
InfoFont = {
  standardFont,
  18,
  WhiteColor
}
DialogTitleFont = {
  standardFont,
  20,
  BlackColor
};

DialogBodyFont = {
  standardFont,
  18,
  BlackColor
};

QuestDialogBodyFont = {
  standardFont,
  16,
  WhiteColor
};

TaskWindowFont = {
  standardFont,
  10,
  WhiteColor
};

DefaultStyle = {
    font=DialogBodyFont
};

QuestDialogBodyFont1 = {
  standardFont,
  16,
  BlackColor
};

FlashComicStyle = {
    translate = true,
    disableAbort = true,
    allowInput = true
};

QuestDialogBodyText1 = {
    parent=DefaultStyle,
    font = QuestDialogBodyFont1,
    flags = kVAlignCenter + kHAlignCenter,
};


DialogTitleText = {
    parent=DefaultStyle,
    font = DialogTitleFont,
    flags = kVAlignCenter + kHAlignLeft,
    x=16,y=12,w=kMax,h=30
};

DialogBodyText = {
    parent=DefaultStyle,
    font = DialogBodyFont,
    flags = kVAlignCenter + kHAlignCenter,
    x=0,y=46,w=kMax,h=kMax-60
};

QuestDialogBodyText = {
    parent=DefaultStyle,
    font = QuestDialogBodyFont,
    flags = kVAlignCenter + kHAlignCenter,
    x=0,y=46,w=kMax,h=kMax-60
};

InventoryItemCountText = {
    font = { standardFont, 18, BlackColor},
    flags = kVAlignCenter + kHAlignCenter,
    x=0,y=0,w=kMax,h=kMax,
};

InventoryButtonText = {
    font = { standardFont, 14, BlackColor},
    flags = kVAlignCenter + kHAlignCenter,
    x=0,y=0,w=kMax,h=kMax,
};

SliderStyle =
{
    parent=DefaultStyle,
    railtop = "slider/sliderrailtop",
    railmid = "slider/sliderrailmid",
    railbot = "slider/sliderrailbot",
    sliderimage = "slider/sliderknob",
    sliderrollimage = "slider/sliderknobover",

    yoffset = -1, -- scoot the slider down by one pixel to center it.
};

kMenuButtonSound="audio/sfx/main_menu/button_click.ogg";
kMenuRolloverSound="audio/sfx/main_menu/button_rollover.ogg";

MenuButtonStyle = {
    parent = DefaultStyle,
    font = MenuButtonFont,
    sound = kMenuButtonSound,
    rolloversound = kMenuRolloverSound,
    graphics = {},
    w=215, h=40
};

ButtonStyle = {
    parent = DefaultStyle,
    font = StandardButtonFont,
    sound = kMenuButtonSound,
    rolloversound = kMenuRolloverSound,
    type= kPush,
    graphics = StandardButtonGraphics,
    mask = "buttons/buttonup_alpha",
};

UpgradeButtonStyle = {
    parent = DefaultStyle,
    sound = kMenuButtonSound,
    rolloversound = kMenuRolloverSound,
    type= kPush,
    graphics = RolloverButtonGraphics,
    mask = "interface/game/pop_up_object/btn_roll_alpha",
};

LongButtonStyle = {
    parent = ButtonStyle,
    graphics = LongButtonGraphics,
};

CheckboxButtonStyle = {
    parent = DefaultStyle,
    font = StandardButtonFont,
    type = kToggle,
    sound = kMenuButtonSound,
    rolloversound = kMenuRolloverSound,
    graphics = CheckboxButtonGraphics,
    tx=28,
};

SetDefaultStyle(DefaultStyle);

-- Create a TButton with a label
--
-- We're overriding the default "Button" here with
-- some custom behavior. Specifically, we're layering
-- a Text{} object on top of every button, and it's placed
-- on all layers (up, down, roll-over).
function Button( button )
    return function()
        -- Grab the label from the button or current style
        if (GetTag(button,"label")) then

            -- Look for text dimensions
            local tx = GetTag(button,"tx");
            local ty = GetTag(button,"ty");
            local tw = GetTag(button,"tw");
            local th = GetTag(button,"th");

            -- Set defaults
            if (not tx) then
                tx = 0;
            end
            if (not ty) then
                ty = 0;
            end
            if (not tw) then
                tw=kMax ;
            end
            if (not th) then
                th=kMax ;
            end

-- 4.0.15 -- Begin new hotkey support
            -- Get the label
            local label = GetTag(button,"label");

            local defflags = kPushButtonAlignment ;
            if (GetTag(button,"type")==kToggle) then
                defflags = kToggleButtonAlignment ;
            elseif (GetTag(button,"type")==kRadio) then
                defflags = kRadioButtonAlignment ;
            end

            local textfactory = GetTag(button,"textfactory");
            if (not textfactory) then
                textfactory = "_text"
            end
            table.insert(button,
                SelectLayer( kAllLayers )
            );
            -- Grow our window to encompass any children we've added
            -- already.
            table.insert(button,
                FitToChildren()
            );

            table.insert(button,
                AppendStyle{ font=button.font; flags=button.flags; }
            );
            
            if (button.font ~= nil) then
                table.insert(button,
                    Text
                    {
                        label="#"..GetString( "button_outline", GetString(label) ),
                        x=tx+2, y=ty+2, w=tw, h=th,
                        name='shadow_label',
                        font = {button.font[1], button.font[2], ButtonYellow},
                        defflags =defflags
                    }
                )
            end;

            table.insert(button,
                Text
                {
                    label="#"..GetString( "button_outline", GetString(label) ),
                    x=tx, y=ty, w=tw, h=th,
                    name='label',
                    defflags =defflags
                }
            )
        end

        -- Type name of window to create; in our case, "Button".
        -- If you want to create a derived button type, you can do that here.
        button.typename='Button_cr';
        DoWindow( button );
    end
end;

function MenuMirrorButton( button )
    return function()
        -- Grab the label from the button or current style
        if (GetTag(button,"label")) then

            -- Look for text dimensions
            local tx = GetTag(button,"tx");
            local ty = GetTag(button,"ty");
            local tw = GetTag(button,"tw");
            local th = GetTag(button,"th");

            -- Set defaults
            if (not tx) then
                tx = 0;
            end
            if (not ty) then
                ty = 0;
            end
            if (not tw) then
                tw=kMax ;
            end
            if (not th) then
                th=kMax ;
            end

-- 4.0.15 -- Begin new hotkey support
            -- Get the label
            local label = GetTag(button,"label");

            local defflags = kPushButtonAlignment ;
            if (GetTag(button,"type")==kToggle) then
                defflags = kToggleButtonAlignment ;
            elseif (GetTag(button,"type")==kRadio) then
                defflags = kRadioButtonAlignment ;
            end

            local textfactory = GetTag(button,"textfactory");
            if (not textfactory) then
                textfactory = "_text"
            end
            table.insert(button,
                SelectLayer( kAllLayers )
            );
            -- Grow our window to encompass any children we've added
            -- already.
            table.insert(button,
                FitToChildren()
            );

            table.insert(button,
                AppendStyle{ font=button.font; flags=button.flags; }
            );
            
            if (button.font ~= nil) then
                table.insert(button,
                    Text
                    {
                        label="#"..GetString( "blue_outline", GetString(label) ),
                        x=tx+2, y=ty+2, w=tw, h=th,
                        name='shadow_label',
                        font = {button.font[1], button.font[2], BlueOutline},
                        defflags =defflags
                    }
                )
            end;

            table.insert(button,
                Text
                {
                    label="#"..GetString( "blue_outline", GetString(label) ),
                    x=tx, y=ty, w=tw, h=th,
                    name='label',
                    defflags =defflags
                }
            )
        end

        -- Type name of window to create; in our case, "Button".
        -- If you want to create a derived button type, you can do that here.
        button.typename='Button_cr';
        DoWindow( button );
    end
end;

function PlayerButton( button )
    return function()
        -- Grab the label from the button or current style
        if (GetTag(button,"label")) then

            -- Look for text dimensions
            local tx = GetTag(button,"tx");
            local ty = GetTag(button,"ty");
            local tw = GetTag(button,"tw");
            local th = GetTag(button,"th");

            -- Set defaults
            if (not tx) then
                tx = 0;
            end
            if (not ty) then
                ty = 0;
            end
            if (not tw) then
                tw=kMax ;
            end
            if (not th) then
                th=kMax ;
            end

-- 4.0.15 -- Begin new hotkey support
            -- Get the label
            local label = GetTag(button,"label");

            local defflags = kPushButtonAlignment ;
            if (GetTag(button,"type")==kToggle) then
                defflags = kToggleButtonAlignment ;
            elseif (GetTag(button,"type")==kRadio) then
                defflags = kRadioButtonAlignment ;
            end

            local textfactory = GetTag(button,"textfactory");
            if (not textfactory) then
                textfactory = "_text"
            end
            table.insert(button,
                SelectLayer( kAllLayers )
            );
            -- Grow our window to encompass any children we've added
            -- already.
            table.insert(button,
                FitToChildren()
            );

            table.insert(button,
                AppendStyle{ font=button.font; flags=button.flags; }
            );
            
            if (button.font ~= nil) then
                table.insert(button,
                    Text
                    {
                        label="#"..GetString(label),
                        x=tx+2, y=ty+2, w=tw, h=th,
                        name='shadow_label',
                        font = {button.font[1], button.font[2], ShadowColor},
                        defflags =defflags
                    }
                )
            end;

            table.insert(button,
                Text
                {
                    label="#"..GetString(label),
                    x=tx, y=ty, w=tw, h=th,
                    name='label',
                    defflags =defflags
                }
            )
        end

        -- Type name of window to create; in our case, "Button".
        -- If you want to create a derived button type, you can do that here.
        button.typename='Button_cr';
        DoWindow( button );
    end
end;



function ButtonExt( button )
    return function()
        -- Grab the label from the button or current style
        if (GetTag(button,"label")) then

            -- Look for text dimensions
            local tx = GetTag(button,"tx");
            local ty = GetTag(button,"ty");
            local tw = GetTag(button,"tw");
            local th = GetTag(button,"th");

            -- Set defaults
            if (not tx) then
                tx = 0;
            end
            if (not ty) then
                ty = 0;
            end
            if (not tw) then
                tw=kMax ;
            end
            if (not th) then
                th=kMax ;
            end

            -- Get the label
            local label = GetTag(button,"label");

            local defflags = kPushButtonAlignment ;
            if (GetTag(button,"type")==kToggle) then
                defflags = kToggleButtonAlignment ;
            elseif (GetTag(button,"type")==kRadio) then
                defflags = kRadioButtonAlignment ;
            end

            local textfactory = GetTag(button,"textfactory");
            if (not textfactory) then
                textfactory = "_text"
            end
            table.insert(button,
                SelectLayer( kAllLayers )
            );
            if (button.font ~= nil) then
                table.insert(button,
                    Text
                    {
                        label="#"..GetString( "button_outline", GetString(label) ),
                        x=tx+2, y=ty+2, w=tw, h=th,
                        name='shadow_label',
                        font = {button.font[1], button.font[2], ButtonYellow},
                        defflags =defflags
                    }
                )
            end;

            table.insert(button,
                AppendStyle{ font=button.font; flags=button.flags; }
            );

            table.insert(button,
                Text
                {
                    label="#"..GetString( "button_outline", GetString(label) ),
                    x=tx, y=ty, w=tw, h=th,
                    name='label',
                    defflags =defflags
                }
            )
        end

        -- Type name of window to create; in our case, "Button".
        -- If you want to create a derived button type, you can do that here.
        button.typename='ButtonExt';
        DoWindow( button );
    end
end;

-- Create several bits of text that combine to make a blended shadow
function ColorShadowText( x,y,size, label, color )

    return Group(
        {
        -- Add these traits to the current style
            AppendStyle{ x=x, y=y, w=kMax, h=kMax, label=label },
            AppendStyle{ font = { standardFont, size, Color(0,0,0,40)} },
            AppendStyle{ flags = kHAlignCenter+kVAlignCenter },
            Text{ y=y+4 },
            AppendStyle{ font = { standardFont, size, Color(0,0,0,255)} },
            Text{ y=y+2 },
            AppendStyle{ font = { standardFont, size, color } } ,
            Text{},
        }
    )
end

kMenuButtonFontSize = 22;

function MenuButtonLabel( label, color, colorHighlight )

    if not color then
        color =WhiteColor;	-- the default colors
        colorHighlight=Color(255,255,255,255)
    end

    return Group{
        SelectLayer(0),
        ColorShadowText(0,0,kMenuButtonFontSize, label, color ),
        SelectLayer(1),
        ColorShadowText(0,0,kMenuButtonFontSize, label, colorHighlight ),
        SelectLayer(2),
        ColorShadowText(0,0,kMenuButtonFontSize, label, colorHighlight )
    }

end

-- Create a more limited-use button type here; the label is defined inline
-- with MenuButtonLabel above
function MenuButton( button )
    return function()
        -- Type name of window to create; in our case, "Button".
        -- If you want to create a derived button type, you can do that here.
        button.typename='Button';
        DoWindow( button )
       
    end
end

kIllegalNameChars = "!@#$%^&*()><\\\"\'[]{}|?/+=~`.,;:-";