--========================================================
-- RE Health HUD — UI Sounds / OGG Playback
--========================================================

----------------------------------------------------------
-- Vanilla UI click helper
----------------------------------------------------------

function playVanillaClick(self)
    if not self then return end
    if self.playSound then
        self:playSound("UIActivateButton")
    elseif UIManager and UIManager.PlaySound then
        UIManager.PlaySound(UIManager.instance, "UIActivateButton")
    end
end

----------------------------------------------------------
-- Direct OGG playback
----------------------------------------------------------

local SOUND_PATH = "42/media/sound/"

local SOUND_FILES = {
    Accept = {
        [25] = "25Accept.ogg",
        [50] = "50Accept.ogg",
        [75] = "75Accept.ogg",
        [100] = "100Accept.ogg",
    },
    Cancel = {
        [25] = "25Cancel.ogg",
        [50] = "50Cancel.ogg",
        [75] = "75Cancel.ogg",
        [100] = "100Cancel.ogg",
    },
    Cursor = {
        [25] = "25Cursor.ogg",
        [50] = "50Cursor.ogg",
        [75] = "75Cursor.ogg",
        [100] = "100Cursor.ogg",
    },
    Click = {
        [25] = "25Click.ogg",
        [50] = "50Click.ogg",
        [75] = "75Click.ogg",
        [100] = "100Click.ogg",
    },
    Click2 = {
        [25] = "25Click2.ogg",
        [50] = "50Click2.ogg",
        [75] = "75Click2.ogg",
        [100] = "100Click2.ogg",
    },
    Item = {
        [25] = "25Item.ogg",
        [50] = "50Item.ogg",
        [75] = "75Item.ogg",
        [100] = "100Item.ogg",
    },
}

----------------------------------------------------------
-- playOgg() – UI-safe version (plays even when paused)
----------------------------------------------------------

function playOgg(name)
    if not name or name == "" then return end
    local vol = tonumber(REHUD_cfg and REHUD_cfg.uiVolume or 100)
    if vol ~= 25 and vol ~= 50 and vol ~= 75 and vol ~= 100 then vol = 100 end

    local file = SOUND_FILES[name] and SOUND_FILES[name][vol]
    if not file then
        print("[REHealthHUD] Missing sound entry for " .. tostring(name))
        return
    end

    -- If paused, use UIManager path
    local spdCtrl = UIManager.getSpeedControls()
    local paused = spdCtrl and spdCtrl:getCurrentGameSpeed() == 0

    if paused then
        local sm = getSoundManager()
        if sm and sm.PlaySoundUI then
            local baseName = file:gsub("%.ogg$", ""):gsub("%.wav$", "")
            sm:PlaySoundUI(baseName)
            return
        elseif UIManager.PlaySound then
            UIManager.PlaySound(UIManager.instance, file)
            return
        end
    end

    -- Normal game-time playback
    local sm = getSoundManager()
    local p = getSpecificPlayer(0) or getPlayer()

    if p and p.playSound then
        pcall(function()
            local baseName = file:gsub("%.ogg$", ""):gsub("%.wav$", "")
            p:playSound(baseName)
        end)
        return
    end

    if sm then
        local paths = { "42/media/sound/", "media/sound/" }
        for _, base in ipairs(paths) do
            for _, ext in ipairs({".ogg", ".wav"}) do
                local full = base .. file:gsub("%.ogg$", ext)
                if sm.PlaySoundThenForget and sm:PlaySoundThenForget(full, 1.0) then return end
                if sm.PlaySound then
                    sm:PlaySound(full, false, 1.0)
                    return
                end
            end
        end
    end
end

_G.playOgg = playOgg
_G.REHUD_cfg = REHUD_cfg

----------------------------------------------------------
-- Click Right (inventory context)
----------------------------------------------------------

safeAdd(Events.OnPreFillInventoryObjectContextMenu, function(_player, _ctx, _items)
    if REHUD_cfg.uiClickSounds then
        playOgg("Click2")
    end
end)
