-- TetrisFPSFix.lua
-- Medium Optimization Layer for Inventory Tetris – Build 42
-- Does NOT modify your base mod. Hooks and throttles expensive rendering.

local renderThrottle = 0.06       -- redraw every 60 ms (~16 FPS internally)
local logicThrottle  = 0.12       -- heavy logic every 120 ms
local lastRender     = 0
local lastLogic      = 0

local function now()
    return getTimestampMs() / 1000
end

--------------------------------------------------------------------
-- Patch: ItemGridUI (base inventory window)
--------------------------------------------------------------------
if ItemGridUI and not ItemGridUI._fpsPatched then
    ItemGridUI._fpsPatched = true

    local old_prerender = ItemGridUI.prerender
    local old_render    = ItemGridUI.render

    ItemGridUI._gridCache = nil
    ItemGridUI._gridDirty = true

    function ItemGridUI:markDirty()
        self._gridDirty = true
    end

    -- Throttle prerender (layout + slot logic)
    function ItemGridUI:prerender()
        local t = now()

        if (t - lastLogic) >= logicThrottle then
            lastLogic = t
            old_prerender(self)
        end
    end

    -- Cache grid rendering
    function ItemGridUI:render()
        local t = now()

        -- Trigger redraw only when needed
        if (t - lastRender) >= renderThrottle or self._gridDirty then
            lastRender = t
            self._gridDirty = false

            old_render(self)                 -- draw normally
            self._gridCache = self:getTexture() -- snapshot grid
        else
            if self._gridCache then
                self:drawTexture(self._gridCache, 0, 0, 1, 1, 1, 1, 1)
            else
                old_render(self)
            end
        end
    end
end

--------------------------------------------------------------------
-- Patch: ItemGridContainerUI (loot window & equipped inventory)
--------------------------------------------------------------------
if ItemGridContainerUI and not ItemGridContainerUI._fpsPatched then
    ItemGridContainerUI._fpsPatched = true

    local old_prerender = ItemGridContainerUI.prerender
    local old_render    = ItemGridContainerUI.render

    ItemGridContainerUI._gridCache = nil
    ItemGridContainerUI._gridDirty = true

    function ItemGridContainerUI:markDirty()
        self._gridDirty = true
    end

    function ItemGridContainerUI:prerender()
        local t = now()
        if (t - lastLogic) >= logicThrottle then
            lastLogic = t
            old_prerender(self)
        end
    end

    function ItemGridContainerUI:render()
        local t = now()

        if (t - lastRender) >= renderThrottle or self._gridDirty then
            lastRender = t
            self._gridDirty = false

            old_render(self)
            self._gridCache = self:getTexture()
        else
            if self._gridCache then
                self:drawTexture(self._gridCache, 0, 0, 1, 1, 1, 1, 1)
            else
                old_render(self)
            end
        end
    end
end

--------------------------------------------------------------------
-- Patch: GridOverflowRenderer (optional but helps FPS)
--------------------------------------------------------------------
if GridOverflowRenderer and not GridOverflowRenderer._fpsPatched then
    GridOverflowRenderer._fpsPatched = true

    local old_render = GridOverflowRenderer.render

    function GridOverflowRenderer.render(...)
        local t = now()
        if (t - lastLogic) >= logicThrottle then
            lastLogic = t
            return old_render(...)
        end
        -- Skip most overflow renders between throttled frames
    end
end

print("[TetrisFPSFix] Active – medium performance mode enabled.")
