local MOD_NAME = "LMD Timer Controller"

local rt = {
    target_value = 1.0, 
}

local function inject_time(total_seconds)
    local sg1 = sdk.get_managed_singleton("app.SubGame1Manager")
    if not sg1 then return end

    local tm = sg1:get_field("<Time>k__BackingField") or sg1:get_field("_Time_k__BackingField")
    if not tm then return end

    local ms_to_add = math.floor(total_seconds * 1000)

    pcall(function()
        local ext_list = tm:get_field("_AbilityExtendValue")
        if ext_list then
            ext_list:call("Add", ms_to_add)
        end
    end)
end

local function format_min_sec(mins)
    local m = math.floor(mins)
    local s = math.floor((mins - m) * 60)
    return string.format("%d:%02d", m, s)
end

re.on_draw_ui(function()
    if imgui.tree_node(MOD_NAME) then
        
        imgui.text("Select Time Amount:")
        local changed, new_val = imgui.slider_float("##time_val", rt.target_value, 0.167, 5.0, "%.2f min")
        if changed then rt.target_value = new_val end
        
        imgui.text_colored("Selected Amount: " .. format_min_sec(rt.target_value), 0xFFFFFF00)
        
        imgui.spacing()

        if imgui.button("Add", {120, 30}) then
            inject_time(rt.target_value * 60)
        end

        imgui.same_line()

        if imgui.button("Substract", {120, 30}) then
            inject_time(-(rt.target_value * 60))
        end

        imgui.tree_pop()
    end
end)