------------------------------------------------------------------------------------------------
-- Please don't judge this code too heavily. It is a bit of a hack job but it works :)
------------------------------------------------------------------------------------------------


function uiDrawOptions()

    local w = UiWidth()
    local h = UiHeight()

    contW = w * 0.9
    contH = h * 0.9
    padH = 10
    padW = 10
    padW2 = padW * 2
    padH2 = padH * 2

    font_heading = 64
    font_normal = 28

    -- Base container.
    uiDrawBaseContainer()

    -- Title container.
    title_w = 128
    title_h = 128
    uiDrawTitleContainer()

    -- Options container.
    options_w = contW - (padW * 2)
    options_h = contH - title_h - (padH * 3)
    options_tabs = 4
    options_tabH = 72
    options_tabW = (options_w - padW2)/options_tabs
    options_tabNames = {'Tool', 'Slimes', 'Performance', 'Info'}
    options_tabSelected = options_tabSelected or options_tabNames[1]
    uiDrawOptionsContainer()

end

function uiDrawBaseContainer()
    ui.padding.create(UiWidth() * 0.1/2, UiHeight() * 0.1/2)
    ui.container.create(contW, contH, ui.colors.g1, 1)
end

function uiDrawTitleContainer()

    -- Title container
    ui.padding.create(padW, padH)
    ui.container.create(contW - title_w, title_h, ui.colors.g2, 1)


    -- Text: Title
    do UiPush()

        -- Mod Preview Image
        local imgW = title_w - padW2
        local imgH = title_h - padH2
        ui.padding.create(padW, padH)
        UiImageBox('img/slime.png', imgW, imgH, 1,1, 1,1,1,1)

        -- Mod Title
        ui.padding.create(padW + imgW, 0)
        local textHeight = 64
        UiColor(1,1,1, 1)
        UiFont('bold.ttf', textHeight)
        UiAlign('top left')
        UiText('Hungry Slimes')

        -- Mod Subtitle
        ui.padding.create(0, textHeight + padH/2)
        textHeight = 32
        UiFont('bold.ttf', textHeight)
        UiText('By: Geneosis')


    UiPop() end

    -- Button: Exit
    do UiPush()

        ui.padding.create(padW, 0)
        ui.padding.create(contW - title_w, 0)

        local btnW = 128 - (padW*3)
        local btnH = 128

        UiAlign('left top')

        -- Exit button
        UiColor(1,0,0,1)
        UiRect(btnW, btnH)

        UiButtonImageBox('ui/common/box-outline-6.png', 1,1, 0,0,0, a)
        if UiBlankButton(btnW, btnH) then
            if UI_GAME then
                UI_GAME = false
            elseif UI_OPTIONS then
                Menu()
            end
        end

        -- X text
        UiColor(1,1,1, 1)
        ui.padding.create(btnW/2, btnH/2)
        UiFont('bold.ttf', btnW/1.5)
        UiAlign('middle center')
        UiText('X')


    UiPop() end

    ui.padding.create(0, title_w)

end

function uiDrawOptionsContainer()

    -- Options container
    ui.padding.create(0, padH)
    ui.container.create(options_w, options_h, ui.colors.g2, 1)

    -- Text: Options
    ui.padding.create(padW, padH)
    do UiPush()
        UiColor(1,1,1, 1)
        UiFont('bold.ttf', font_heading)
        UiAlign('top left')
        UiText('Options')
    UiPop() end


    -- Button: Options reset
    do UiPush()
        ui.padding.create(contW - padW * 4, ((font_heading - font_normal * 1.5)/2))
        UiColor(1,1,1, 1)
        UiFont('bold.ttf', font_normal * 1.5)
        UiAlign('top right')

        local resetButton = UiTextButton('Reset Mod')
        if resetButton then
            modReset()
            beep()
        end

    UiPop() end


    -- Options tabs
    ui.padding.create(0, padH2 + font_heading)
    do UiPush()

        ui.tabView.tab.create("Tool")

        ui.padding.create(options_tabW + (padW/(options_tabs-1)), 0)
        ui.tabView.tab.create("Slimes")

        ui.padding.create(options_tabW + (padW/(options_tabs-1)), 0)
        ui.tabView.tab.create("Performance")

        ui.padding.create(options_tabW + (padW/(options_tabs-1)), 0)
        ui.tabView.tab.create("Info")

    UiPop() end


    -- Options tab content container
    local tabContentH = contH - title_h - options_tabH - options_tabH - padH * 6.5
    ui.padding.create(0, options_tabH)
    ui.container.create(contW - padW*4, tabContentH, ui.colors.g3, 1)


    -- Tab content
    ui.padding.create(padW, padH2)
    ui.container.create(contW - padW*6, tabContentH - padH*3, ui.colors.g1, 1)
    ui.padding.create(padW2*2, padH2*2)
    do UiPush()

        if options_tabSelected == 'Tool' then

            options_tabs_render.Tool()

        elseif options_tabSelected == 'Slimes' then

            options_tabs_render.Slimes()

        elseif options_tabSelected == 'Performance' then

            options_tabs_render.Performance()

        elseif options_tabSelected == 'Info' then

            options_tabs_render.Info()

        end

    UiPop() end

end

options_tabs_render = {

    Tool = function()

        do UiPush()

            ui.padding.create(100, 20)

            do UiPush()

                ui.slider.create('Throw Gravity', 'tool.pour.gravity', nil, 0, 0.1)
                ui.padding.create(0, 96)

                ui.slider.create('Throw RPM (Slimes per second)', 'tool.pour.rate', 'RPM', 120, 2400)
                ui.padding.create(0, 96)

                ui.slider.create('Throw Velocity', 'tool.pour.velocity', 'm/s', 0.01, 1)
                ui.padding.create(0, 96)

                ui.slider.create('Throw Spread', 'tool.pour.spread', nil, 0, 100)
                ui.padding.create(0, 96)

            UiPop() end

            do UiPush()

                ui.padding.create(800, 0)

                ui.checkBox.create('Debug Mode', 'tool.debugMode')
                ui.padding.create(0, 96)

            UiPop() end

        UiPop() end

    end,

    Slimes = function()

        do UiPush()

            ui.padding.create(100, 20)

            do UiPush()

                ui.slider.create('Eat Delay', 'tool.slime.eatDelay', 'Seconds', 0, 5)
                ui.padding.create(0, 96)

                ui.slider.create('Eat Radius', 'tool.slime.eatRadius', 'Meters', 0.1, 4)
                ui.padding.create(0, 96)

                ui.slider.create('Food for Duplication', 'tool.slime.duplicationRate', 'Food eaten', 1, 100)
                ui.padding.create(0, 96)

                ui.slider.create('Jump Distance', 'tool.slime.jumpDistance', 'Meters', 0, 10)
                ui.padding.create(0, 96)

            UiPop() end

            do UiPush()

                ui.padding.create(800, 0)

                ui.checkBox.create('Sound', 'tool.soundOn')
                ui.padding.create(0, 96)

            UiPop() end

        UiPop() end

    end,

    Performance = function()

        UiColor(1,1,1, 1)
        UiFont('bold.ttf', 32)
        -- UiAlign('left middle')
        -- UiText('(Performance options coming soon)')


        ui.padding.create(100, 20)

        ui.checkBox.create('Render Slime Particles', 'tool.slime.renderSlimeParticles')
        ui.padding.create(0, 64)

        ui.slider.create('Auto-kill slimes to maintain FPS (Due to API limitation, no effect if your default FPS is <= 30)', 'tool.slime.killToMaintainFPS', 'FPS', 0, 120)
        ui.padding.create(0, 30)
        local fps = math.floor(1 / GetTimeStep())
        UiAlign('left middle')
        UiText('FPS: '..fps)
        ui.padding.create(0, 64)

        ui.slider.create('Slime max population (0 = infinite)', 'tool.slime.slimesMaxPopulation', 'Slimes', 0, 3000)
        ui.padding.create(0, 30)
        UiAlign('left middle')
        UiText('Population: '..#Slime.slimesList..' Slimes')
        ui.padding.create(0, 64)

    end,

    Info = function()

        UiColor(1,1,1, 1)
        UiFont('bold.ttf', 32)
        UiAlign('left middle')
        UiText('Thanks a lot to Cheejins for his "Gas Can" mod, which was used as a base for this mod!')
        ui.padding.create(0, 50)

        UiText('CONTROLS')
        ui.padding.create(0, 50)
        UiText('[Left Click]: Spawm Slimes')
        ui.padding.create(0, 50)
        UiText('[L]: Kill half of the slimes')
        ui.padding.create(0, 50)
        UiText('[K]: Kill all slimes')
        ui.padding.create(0, 50)

        UiText('NOTES')
        ui.padding.create(0, 50)
        UiText('- Slimes can be seen as better fire (that can burn anything), or sticky acid.')
        ui.padding.create(0, 50)
        UiText('- Slimes will attempt to duplicate after eating if they have enough food.')
        ui.padding.create(0, 50)
        UiText('- Playing with the options to make the slimes dupliate faster will slow down your game.')
        ui.padding.create(0, 50)
        UiText('- Slimes seems to lag a lot more when eating large free-floating objects (eg: boats).')
        ui.padding.create(0, 50)
        UiText('- I may try to add options to set the slime particle properties (color, type, size).')

    end,

}



--- Draw options shorcut key hint above tool name.
function uiDrawToolNameOptionsHint(addHeight)

    UiPush()

        UiTranslate(UiCenter(), UiHeight() - (addHeight or 60))

        UiAlign("center middle")
        UiFont("bold.ttf", 32)
        UiTextShadow(0,0,0, 1, 0.3, 0.5)
        UiColor(1,1,1, 1)

        UiText('[O]: Options')

    UiPop()

end

--- Manage when to open and close the options menu.
function uiManageGameOptions()

    if tool.tool.active() then

        if InputPressed('o') then UI_GAME = not UI_GAME end

        if UI_GAME then
            UiMakeInteractive()
            uiDrawOptions()
        end

    end

end



ui = {}

ui.colors = {
    white = Vec(1,1,1),
    g3 = Vec(0.5,0.5,0.5),
    g2 = Vec(0.35,0.35,0.35),
    g1 = Vec(0.2,0.2,0.2),
    black = Vec(0,0,0),
}



ui.container = {}

function ui.container.create(w, h, c, a)
    if not c then c = Vec(0.5,0.5,0.5) end
    UiColor(c[1], c[2], c[3], a or 0.5)
    UiRect(w, h)
end



ui.padding = {}

function ui.padding.create(w, h)
    UiTranslate(w or 10, h or 10)
end



ui.tabView = {}

ui.tabView.tab = {}

function ui.tabView.tab.create(text)

    do UiPush()

        UiAlign('top left')

        ui.container.create(options_tabW - padW, options_tabH, ui.colors.g1, 1)

        if options_tabSelected == text then
            ui.container.create(options_tabW - padW, options_tabH, ui.colors.g3, 1)
        end

        UiButtonImageBox('ui/common/box-outline-6.png', 1,1, 0,0,0, a)
        if UiBlankButton(options_tabW, options_tabH) then
            if options_tabSelected ~= text then
                PlaySound(LoadSound('clickdown.ogg'), GetCameraTransform().pos, 1)
            end
            options_tabSelected = text
        end

        ui.padding.create(options_tabW/2, options_tabH/2)

        UiColor(1,1,1, 1)
        UiFont('bold.ttf', font_normal*1.25)
        UiAlign('center middle')
        UiText(text)

    UiPop() end

end



ui.slider = {}

function ui.slider.create(title, registryPath, valueText, min, max, w, h, fontSize, axis)

    local value = GetFloat('savegame.mod.' .. registryPath)

    min = min or 0
    max = max or 300

    UiAlign('left middle')

    -- Text header
    UiColor(1,1,1, 1)
    UiFont('regular.ttf', fontSize or font_normal)
    UiText(title)
    ui.padding.create(0, fontSize or font_normal)

    -- Slider BG
    UiColor(0.4,0.4,0.4, 1)
    local slW = w or 400
    UiRect(slW, h or 10)

    -- Convert to slider scale.
    value = ((value-min) / (max-min)) * slW

    -- Slider dot
    UiColor(1,1,1, 1)
    UiAlign('center middle')
    value, done = UiSlider("ui/common/dot.png", "x", value, 0, slW)
    if done then
        local val = (value/slW) * (max-min) + min -- Convert to true scale.
        SetFloat('savegame.mod.' .. registryPath, val)
    end

    -- Slider value
    do UiPush()
        UiAlign('left middle')
        ui.padding.create(slW + 20, 0)
        local decimals = ternary((value/slW) * (max-min) + min < 100, 3, 1)
        UiText(sfn((value/slW) * (max-min) + min, decimals) .. ' ' .. (valueText or ''))
    UiPop() end

end


ui.checkBox = {}

function ui.checkBox.create(title, registryPath)

    local value = GetBool('savegame.mod.' .. registryPath)

    UiAlign('left middle')

    -- Text header
    UiColor(1,1,1, 1)
    UiFont('regular.ttf', fontSize or font_normal)
    UiText(title)
    ui.padding.create(0, fontSize or font_normal)

    -- Toggle BG
    UiAlign('left top')
    UiColor(0.4,0.4,0.4, 1)
    local tglW = w or 140
    local tglH = h or 40
    UiRect(tglW, h or tglH)

    -- Render toggle
    do UiPush()

        local toggleText = 'ON'

        if value then
            ui.padding.create(tglW/2, 0)
            UiColor(0,0.8,0, 1)
        else
            toggleText = 'OFF'
            UiColor(0.8,0,0, 1)
        end

        UiRect(tglW/2, tglH)

        do UiPush()
            ui.padding.create(tglW/4, tglH/2)
            UiColor(1,1,1, 1)
            UiFont('bold.ttf', font_normal)
            UiAlign('center middle')
            UiText(toggleText)
        UiPop() end

    UiPop() end

    UiButtonImageBox('ui/common/box-outline-6.png', 10,10, 0,0,0, a)
    if UiBlankButton(tglW, tglH) then
        SetBool('savegame.mod.' .. registryPath, not value)
        PlaySound(LoadSound('clickdown.ogg'), GetCameraTransform().pos, 1)
    end

end