
theme = {
    styles = {
        bg_color = "#151618",
        text_color = "#aaa",
        text_color_muted = "#777",
        heading_color = "#ddb969",
        link_color = "#68b",
        link_color_hovered = "#9be",
        code_color = "#da8",
        
        syntax_keyword = "#d8d",
        syntax_keyword_value = "#79e",
        syntax_identifier = "#adf",
        syntax_operator = "#f66",
        syntax_string = "#da8",
        syntax_number = "#cdb",
        syntax_comment = "#777",
        syntax_placeholder = "#ccc",
        
        container = {
            border_color = "#292d2fff",
            border_shape = menupath.."textures/container_border_shape.png",
            bg_color = "#403e39ff",
            bg_color_alt = "#373530ff",
            bg_shape = menupath.."textures/container_bg_shape.png",
            border_width = "8",
        },
        field = {
            border_color = "#263038ff",
            border_shape = menupath.."textures/field_border_shape.png",
            bg_color = "#393f40",
            bg_shape = menupath.."textures/field_bg_shape.png",
            border_width = "8",
        },
        button = {
            text_color = "#ccc",
            border_color = "#292d2fff",
            border_color_hovered = "#252829ff",
            border_color_active = "#252829ff",
            border_shape = menupath.."textures/button_border_shape.png",
            bg_color = "#403e39ff",
            bg_color_hovered = "#373633ff",
            bg_color_active = "#373633ff",
            bg_shape = menupath.."textures/button_bg_shape.png",
            border_width = "8",
        },
        tab = {
            text_color = "#ccc",
            border_color = "#292d2fff",
            border_color_hovered = "#252829ff",
            border_color_active = "#252829ff",
            border_shape = menupath.."textures/tab_border_shape.png",
            bg_color = "#403e39ff",
            bg_color_hovered = "#373633ff",
            bg_color_active = "#373633ff",
            bg_shape = menupath.."textures/tab_bg_shape.png",
            border_width = "8",
        },
        checkbox = {
            border_color = "#252829ff",
            border_shape = menupath.."textures/checkbox_border_shape.png",
            bg_color = "#3d464dff",
            bg_shape = menupath.."textures/checkbox_bg_shape.png",
            check_color = "#252829ff",
            check_shape = menupath.."textures/checkbox_check_shape.png",
        },
        scrollbar = {
            border_color = "#292d2fff",
            border_shape = menupath.."textures/scrollbar_border_shape.png",
            bg_color = "#1c2023ff",
            bg_shape = menupath.."textures/scrollbar_bg_shape.png",
            thumb_color = "#fff",
            thumb_shape = menupath.."textures/scrollbar_thumb_shape.png",
            border_width = "8",
        },
        
        icons = {
            menu_content = {
                {shape = menupath.."textures/menu_content.png", color = "#fff"}
            }
        },
    }
}

--[[ RGT theme.
theme = {
    styles = {
        bg_color = "#151618",
        text_color = "#aaa",
        text_color_muted = "#777",
        heading_color = "#ddb969",
        link_color = "#68b",
        link_color_hovered = "#9be",
        code_color = "#da8",
        
        syntax_keyword = "#d8d",
        syntax_keyword_value = "#79e",
        syntax_identifier = "#adf",
        syntax_operator = "#f66",
        syntax_string = "#da8",
        syntax_number = "#cdb",
        syntax_comment = "#777",
        syntax_placeholder = "#ccc",
        
        container = {
            border_color = "#684f1dff",--"#292d2fff",
            border_shape = menupath.."textures/container_border_shape.png",
            bg_color = "#1c2023ff", --"#403e39ff",
            bg_color_alt = "#22272aff",--"#373530ff",
            bg_shape = menupath.."textures/container_bg_shape.png",
            border_width = "16",
        },
        field = {
            border_color = "#4d4d4dff",
            border_shape = menupath.."textures/field_border_shape.png",
            bg_color = "#5c5c5c",
            bg_shape = menupath.."textures/field_bg_shape.png",
            border_width = "8",
        },
        button = {
            text_color = "#ccc",
            border_color = "#4d4d4dff",
            border_color_hovered = "#3c3c3cff",
            border_color_active = "#2b2b2bff",
            border_shape = menupath.."textures/button_border_shape.png",
            bg_color = "#5c5c5c",
            bg_color_hovered = "#4b4b4bff",
            bg_color_active = "#3a3a3aff",
            bg_shape = menupath.."textures/button_bg_shape.png",
            border_width = "8",
        },
        icons = {
            
        },
    }
}
]]

function theme.get_background_image(bg, state)
    local bg_color
    local border_color
    
    if state == "hovered" then
        bg_color = theme.styles[bg].bg_color_hovered
        border_color = theme.styles[bg].border_color_hovered
    elseif state == "active" then
        bg_color = theme.styles[bg].bg_color_active
        border_color = theme.styles[bg].border_color_active
    else
        bg_color = theme.styles[bg].bg_color
        border_color = theme.styles[bg].border_color
    end
    
    return "("..theme.styles[bg].bg_shape.."^[multiply:"..bg_color..")^("..theme.styles[bg].border_shape.."^[multiply:"..border_color..")", theme.styles[bg].border_width
end

function theme.get_image(element, prop)
    return "("..theme.styles[element][prop.."_shape"].."^[multiply:"..theme.styles[element][prop.."_color"]..")"
end

local assets = os.getenv("HOME").."/eclipse-workspace/mods/mtmenu/"
function theme.icon(name)
    local icon = theme.styles.icons[name]
    if icon then
        if type(icon) == "table" then
            local out = {}
            for i = 1, #icon do
                out[#out + 1] = "("..icon[i].shape.."^[multiply:"..icon[i].color..")"
            end
            return table.concat(out, "^")
        else
            return icon
        end
    end
    
    local path = os.getenv("HOME").."/eclipse-workspace/mtmenu/textures/"..name..".png"
    return file_exists(path) and path or assets..name..".png"
end

function theme.field(x, y, w, h, label, value)
    if value then
        h = h + 0.3
    end
    imfs.group(x, y, w, h)
        if value then
            imfs.label(0.2, 0.125, label)
        end
        imfs.image(0, value and 0.3 or 0, "100%", value and "100% - 0.3" or "100%", theme.get_background_image("field"), theme.styles.field.border_width)
        local field = imfs.field(0.1, value and 0.3 or 0, "100% - 0.2", value and "100% - 0.3" or "100%", value or label)
    imfs.group_end()
    return field
end

function theme.number_field(x, y, w, h, value, step)
    step = step or 1
    imfs.group(x, y, w, h)
        imfs.button(0, "50% - 0.25", 0.5, 0.5, "-")
            :onclick(function()
                value(tonumber(value()) - step)
            end)
        imfs.image(0.6, 0, "100% - 1.2", "100%", theme.get_background_image("field"), theme.styles.field.border_width)
        local field = imfs.field(0.7, 0, "100% - 1.4", "100%", value())
        imfs.button("100% - 0.5", "50% - 0.25", 0.5, 0.5, "+")
            :onclick(function()
                value(tonumber(value()) + step)
            end)
    imfs.group_end()
    return field
end

function theme.checkbox(x, y, label, value, tooltip)
    imfs.group(x, y, 5, 0.75)
        local box
        box = imfs.button(0, 0, 0.75, "100%")
            :image(theme.get_background_image("checkbox")..(value() and "^"..theme.get_image("checkbox", "check") or ""))
            :onclick(function()
                value(not value())
                if box._onchange then
                    box._onchange(value._val)
                end
            end)
            :tooltip(tooltip, not not tooltip)
        box.onchange = function(e, fn)
            e._onchange = fn
        end
        imfs.label(0.85, 0.375, label)
    imfs.group_end()
    return box
end

local open_dropdown = imfs.state()
function theme.dropdown(x, y, w, h, options, value)
    local id = imfs._new_id()
    local el
    local open = open_dropdown() == id
    local border_width = pixel_to_formspec(theme.styles.container.border_width)
    local box_height = math.min(#options, 5) * 0.5
    imfs.group(x, y, w, open and h + box_height or h)
        imfs.group(0, 0, "100%", h)
            el = imfs.button(0, 0, "100%", "100%", value())
                :onclick(function()
                    if open_dropdown() == id then
                        open_dropdown(false)
                    else
                        open_dropdown(id)
                    end
                end)
            imfs.label("100% - 0.6", "50%", open and "^" or "v")
        imfs.group_end()

        if open then
            imfs.image(0, h, "100%", box_height + border_width * 2, theme.get_background_image("container"))
            imfs.scroll_container(border_width, h + border_width, "100% - "..(border_width * 2), box_height)
                for i = 0, #options - 1 do
                    local option = options[i + 1]
                    imfs.button(0, i * 0.5, "100%", 0.5, option)
                        :style({
                            border = false,
                            bgimg = assets.."white.png",
                            bgimg_middle = 0,
                            bgcolor = i % 2 == 1 and theme.styles.container.bg_color or theme.styles.container.bg_color_alt,
                        })
                        :style("hovered", {
                            border = false,
                            bgimg = assets.."white.png",
                            bgimg_middle = 0,
                        })
                        :style("pressed", {
                            border = false,
                            bgimg = assets.."white.png",
                            bgimg_middle = 0,
                        })
                        :onclick(function()
                            value(option)
                            open_dropdown(false)
                        end)
                end
            imfs.scroll_container_end()
        end
    imfs.group_end()
    return el
end

