--========================================================
-- RE Health HUD — Themes (Equipment + Tetris UI Themes)
--========================================================

RE_UI_THEME = RE_UI_THEME or "default"
RE_TETRIS_THEME = RE_TETRIS_THEME or "default"

local REUI_DefaultColor = { r=0, g=0, b=0, a=0.80 }

local function tex(path)
    return getTexture("42/media/ui/background/" .. path)
        or getTexture("media/ui/background/" .. path)
end

----------------------------------------------------------
-- PNG TEXTURES (Equipment + Tetris windows only)
----------------------------------------------------------
local THEME_TEXTURES = {
    default  = false,
    gold     = tex("Gold.png"),
    gunmetal = tex("Gunmetal.png"),
    silver   = tex("Silver.png"),
    sapphire = tex("Sapphire.png"),
    crimson  = tex("Crimson.png"),
    onyx     = tex("Onyx.png"),
}

function REHUD_SetBackgroundTheme(name)
    if name == "default" or THEME_TEXTURES[name] ~= nil then
        RE_UI_THEME = name
    end
end

function REUI_GetThemeTexture()
    return THEME_TEXTURES[RE_UI_THEME]
end

function REHUD_SetTetrisTheme(name)
    if name == "default" or THEME_TEXTURES[name] ~= nil then
        RE_TETRIS_THEME = name
    end
end

function REUI_GetTetrisTexture()
    return THEME_TEXTURES[RE_TETRIS_THEME]
end

----------------------------------------------------------
-- EQUIPMENT UI THEME PATCH
----------------------------------------------------------
local function injectBackground_Equipment(class)
    if not class or class.REUI_BackgroundInjected then return end
    if not class.prerender then return end
    class.REUI_BackgroundInjected = true

    local old_pre = class.prerender

    class.prerender = function(self, ...)
        if old_pre then old_pre(self, ...) end

        local tex = REUI_GetThemeTexture()
        if not tex then return end

        local padT, padB, padL, padR = 18, 12, 8, 8
        local w = self.width  - (padL + padR)
        local h = self.height - (padT + padB)
        local alpha = math.max(0.20, math.min(1.0, REHUD_cfg.equipAlpha or 1.0))

        self:drawTextureScaled(tex, padL, padT, w, h, alpha)
    end
end

----------------------------------------------------------
-- TETRIS WINDOW BACKGROUND (NOT PREVIEW AREA)
----------------------------------------------------------
local function injectBackground_Tetris(class)
    if not class or class.REUI_TetrisInjected then return end
    if not class.prerender then return end
    class.REUI_TetrisInjected = true

    local old_pre = class.prerender

    class.prerender = function(self, ...)
        if old_pre then old_pre(self, ...) end

        -- SKIP ItemGridContainerUI completely
        if self.isItemGridContainerUI then return end

        local tex = REUI_GetTetrisTexture()
        if not tex then return end

        local alpha = math.max(0.20, math.min(1.0, REHUD_cfg.tetrisAlpha or 1.0))
        local titleH = (self.titleBarHeight and self:titleBarHeight()) or 20

        self:drawTextureScaled(
            tex,
            0,
            titleH,
            self:getWidth(),
            self:getHeight() - titleH,
            alpha
        )
    end
end

----------------------------------------------------------
-- LOAD TETRIS CLASSES & PATCH
----------------------------------------------------------
Events.OnInitGlobalModData.Add(function()

    ------------------------------------------------------
    -- Load Grid Core
    ------------------------------------------------------
    local ok, ItemGridUIref = pcall(require, "InventoryTetris/UI/Grid/ItemGridUI")
    if ok and ItemGridUIref then
        ItemGridUI = ItemGridUIref
    end

    ------------------------------------------------------
    -- POPUP WINDOW CLASS (Keyring, Rifle Case…)
    ------------------------------------------------------
    local okW, Win = pcall(require, "InventoryTetris/UI/Windows/ItemGridWindow")
    if okW and Win then
        ItemGridWindow = Win

        -- Patch CLASS
        pcall(injectBackground_Tetris, Win)

        -- Patch instances
        local old_new = ItemGridWindow.new
        ItemGridWindow.new = function(...)
            local win = old_new(...)
            pcall(injectBackground_Tetris, win)
            return win
        end
    end
end)

----------------------------------------------------------
-- APPLY FINAL PATCH TARGETS
----------------------------------------------------------
Events.OnGameStart.Add(function()

    if EquipmentUIWindow then
        pcall(injectBackground_Equipment, EquipmentUIWindow)
    end

    -- Only theme real windows (NOT preview UI)
    local tetrisTargets = {
        ISInventoryPage,
        ItemGridWindow,
    }

    for _, cls in ipairs(tetrisTargets) do
        pcall(injectBackground_Tetris, cls)
    end
end)
