--========================================================
-- RE Health HUD — Main bootstrap / Events (PATCHED)
--========================================================

require "ISUI/ISUIElement"
require "ISUI/ISPanel"
require "ISUI/ISCollapsableWindow"
require "ISUI/ISContextMenu"
require "ISUI/ISButton"

-- Our modules
require "REInterface_Config"
require "REInterface_Themes"
require "REInterface_Sounds"
require "REInterface_Status"
require "REInterface_Button"

REHUD_HUD = REHUD_HUD or nil
REHUD_BTN = REHUD_BTN or nil

------------------------------------------------------------
-- Spawn HUD safely
------------------------------------------------------------

function spawnHUD()
    if REHUD_HUD then return end

    -- ensure player exists
    local p = getSpecificPlayer(0) or getPlayer()
    if not p or (p.isAlive and not p:isAlive()) then return end

    -- if class definitions not ready yet, retry next frame
    if not REHealthHUD or not REHealthHUD_ToggleBtn then
        local function wrapper()
            spawnHUD()
            Events.OnTick.Remove(wrapper)
        end
        Events.OnTick.Add(wrapper)
        return
    end

    local ok, err = pcall(function()
        REHUD_HUD = REHealthHUD:new(
            REHUD_cfg.winX or 142,
            REHUD_cfg.winY or 20,
            REHUD_cfg.winW or 350,
            REHUD_cfg.winH or 150
        )
        REHUD_HUD:setVisible(REHUD_cfg.winVisible ~= false)
        REHUD_HUD:setAlwaysOnTop(false)

        REHUD_BTN = REHealthHUD_ToggleBtn:new(
            REHUD_cfg.btnX or 70,
            REHUD_cfg.btnY or 120
        )
        if ensureButtonSize then
            ensureButtonSize(REHUD_BTN)
        end
    end)

    if not ok then
        print("[REHealthHUD] spawnHUD error: " .. tostring(err))
    end
end

_G.spawnHUD = spawnHUD

------------------------------------------------------------
-- Apply settings once config is loaded
------------------------------------------------------------

function REHUD_ApplySettings()
    if applyBtnTheme then
        applyBtnTheme()
    end

    if REHUD_BTN and ensureButtonSize then
        ensureButtonSize(REHUD_BTN)
    end

    if REHUD_HUD then
        REHUD_HUD:setVisible(REHUD_cfg.winVisible ~= false)
    end

    print("[REHealthHUD] Settings applied.")
end

------------------------------------------------------------
-- Boot + Save Hooks
------------------------------------------------------------

safeAdd(Events.OnCreatePlayer, function(playerIndex, player)
    if playerIndex ~= 0 then return end
    if not player or (player.isAlive and not player:isAlive()) then return end

    -- Load INI *now* (safe: game is fully booted, file IO is ready)
    REHUD_ReadConfig()

    if REHUD_cfg.uiTheme and REHUD_SetBackgroundTheme then
        REHUD_SetBackgroundTheme(REHUD_cfg.uiTheme)
    end
	
    if REHUD_cfg.slotTheme and REH.Slots and REH.Slots.applySavedTheme then
        REH.Slots.applySavedTheme(REHUD_cfg.slotTheme)
    end

    if REHUD_cfg.tetrisTheme and REHUD_SetTetrisTheme then
        REHUD_SetTetrisTheme(REHUD_cfg.tetrisTheme)
    end

    -- Apply button / UI settings
    if REHUD_ApplySettings then REHUD_ApplySettings() end
    if REHUD_ApplyIconColor then REHUD_ApplyIconColor() end

    -- Spawn HUD
    if spawnHUD then spawnHUD() end

    -- Save persistence hooks
    safeAdd(Events.OnSave, REHUD_WriteConfig)
    safeAdd(Events.OnGameExit, REHUD_WriteConfig)

    if REH.Slots and REH.Slots.patchAll then
        REH.Slots.patchAll()
    end

    print("[REHealthHUD] Persistence ready (INI).")
end)
