--========================================================
-- RE Health HUD — IconBG / Slot Theme System
-- Added: Gold / Silver / Green (SAFE MINIMAL PATCH)
--========================================================

REH       = REH or {}
REH.Slots = REH.Slots or {}

----------------------------------------------------------
-- 1. Themes  (ADDED: gold, green, silver)
----------------------------------------------------------

REH.Slots.IconThemes = {
    "black",
    "blue",
    "gold",
    "green",
    "red",
    "silver",
}

REH.Slots.IconSuffix = {
    black  = "",
    blue   = "_Blue",
    gold   = "_Gold",    -- NEW
    green  = "_Green",   -- NEW
    red    = "_Red",
    silver = "_Silver",  -- NEW
}

REH.Slots.CurrentTheme = REH.Slots.CurrentTheme or "blue"

REH.Slots.BaseBrightness = 0.40

function REH.Slots.getFadeBrightness()
    local cfg = _G.REHUD_cfg
    local pct = cfg and cfg.slotFade or 1.0
    if pct < 0.25 then pct = 0.25 end
    if pct > 1.00 then pct = 1.00 end
    return REH.Slots.BaseBrightness * pct
end

function REH.Slots.applySavedTheme(theme)
    if theme and REH.Slots.IconSuffix[theme] then
        REH.Slots.CurrentTheme = theme
    end
end

function REH.Slots.getTheme()
    return REH.Slots.CurrentTheme
end

function REH.Slots.getSuffix()
    return REH.Slots.IconSuffix[REH.Slots.CurrentTheme] or ""
end

----------------------------------------------------------
-- 2. Universal Inventory Background (unchanged)
----------------------------------------------------------

local UNIVERSAL_ICON_BG =
    getTexture("media/ui/equipment/equipment_slot_bg.png") or
    getTexture("media/ui/equipment/slot_bg.png") or
    getTexture("media/ui/inventory/ItemBg.png")

if not UNIVERSAL_ICON_BG then
    UNIVERSAL_ICON_BG = getTexture("media/ui/black.png")
end

local function drawUniversalBG(self)
    if not UNIVERSAL_ICON_BG then return end
    local alpha = math.max(0.20, math.min(1.0, REHUD_cfg.slotFade or 1.0))
    self:drawTextureScaled(UNIVERSAL_ICON_BG, 0, 0, self.width, self.height, alpha)
end

----------------------------------------------------------
-- 3. Inject inventory slot BG (unchanged)
----------------------------------------------------------

local function injectInventorySlotBG(class)
    if class.REH_BG_Patched then return end
    if not class.prerender then return end
    class.REH_BG_Patched = true

    local old = class.prerender

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

        if not self.items then return end

        for i,item in ipairs(self.items) do
            local x, y, w, h = item.x, item.y, item.w, item.h
            if w and h and w > 8 and h > 8 then
                self:drawTextureScaled(
                    UNIVERSAL_ICON_BG, x, y, w, h,
                    math.max(0.20, math.min(1.0, REHUD_cfg.slotFade or 1))
                )
            end
        end
    end
end

Events.OnGameStart.Add(function()
    injectInventorySlotBG(ISInventoryPane)
end)

----------------------------------------------------------
-- 4. Existing Tetris slot theme code (unchanged except new suffix removal)
----------------------------------------------------------

function REH.Slots.getFadeBrightness(base)
    base = base or 0.4
    local cfg = _G.REHUD_cfg
    local pct = cfg and cfg.slotFade or 1.0
    if pct < 0.25 then pct = 0.25 end
    if pct > 1.0 then pct = 1.0 end
    local brightness = 1 - pct * (1 - base)
    return brightness
end

local function applySuffixToTexture(tex, suffix)
    if not tex or not tex.getName then return tex end

    local name = tex:getName() or ""

    -- updated: remove ALL old suffixes before applying new
    name = name:gsub("_Red%.png$", ".png")
    name = name:gsub("_Black%.png$", ".png")
    name = name:gsub("_Gold%.png$", ".png")
    name = name:gsub("_Green%.png$", ".png")
    name = name:gsub("_Silver%.png$", ".png")

    local newName = name:gsub("%.png$", suffix .. ".png")
    return getTexture(newName) or tex
end

function REH.Slots.patchAll()
    local suffix = REH.Slots.getSuffix()

    local Drag = _G.EquipmentDragItemRenderer
    if Drag and Drag.BG_TEXTURE then
        Drag.BG_TEXTURE = applySuffixToTexture(Drag.BG_TEXTURE, suffix)
    end

    local Super = _G.EquipmentSuperSlot
    if Super and Super.BG_TEXTURE then
        Super.BG_TEXTURE = applySuffixToTexture(Super.BG_TEXTURE, suffix)
    end

    local Hotbar = _G.REHotbarSlot or _G.HotbarSlot
    if Hotbar and Hotbar.BG_TEXTURE then
        Hotbar.BG_TEXTURE = applySuffixToTexture(Hotbar.BG_TEXTURE, suffix)
    end

    if REH_RefreshTetrisTextures then
        REH_RefreshTetrisTextures()
    end
end

function REH.Slots._tickPatch()
    if REH.Slots._patched then return end

    if (_G.EquipmentDragItemRenderer
        and _G.EquipmentSuperSlot
        and (_G.REHotbarSlot or _G.HotbarSlot)) then

        REH.Slots.patchAll()
        REH.Slots._patched = true
        Events.OnTick.Remove(REH.Slots._tickPatch)
    end
end

Events.OnTick.Add(REH.Slots._tickPatch)
