local addonName, ns = ...

------------------------------------------------------------
-- Chat output
------------------------------------------------------------

function ns.Print(msg)
    print((ns.Theme and ns.Theme.CHAT_PREFIX or "") .. tostring(msg))
end

------------------------------------------------------------
-- Event handling
------------------------------------------------------------

-- Events that can change owned/placed decor or bag contents; each is
-- pcall-registered because housing events don't exist on all clients.
local AVAILABILITY_EVENTS = {
    "HOUSING_DECOR_PLACE_SUCCESS",
    "HOUSING_DECOR_REMOVED",
    "HOUSE_DECOR_ADDED_TO_CHEST",
    "BAG_UPDATE_DELAYED",
}

local eventFrame = CreateFrame("Frame")
ns._eventFrame = eventFrame
eventFrame:RegisterEvent("ADDON_LOADED")

eventFrame:SetScript("OnEvent", function(self, event, ...)
    if event == "ADDON_LOADED" then
        local loaded = ...
        if loaded ~= addonName then return end

        if type(HousingHeraldDB) ~= "table" then
            HousingHeraldDB = {}
        end
        if type(HousingHeraldDB.shoppingList) ~= "table" then
            HousingHeraldDB.shoppingList = {}
        end
        if type(HousingHeraldDB.shoppingList.entries) ~= "table" then
            HousingHeraldDB.shoppingList.entries = {}
        end
        -- Drop any persisted entry that doesn't match the shape
        -- ns.FormatEntryLine expects; a corrupt SavedVariables entry
        -- (bad type, non-numeric id/qty) would otherwise throw on
        -- every render.
        do
            local entries = HousingHeraldDB.shoppingList.entries
            for i = #entries, 1, -1 do
                local entry = entries[i]
                if type(entry) ~= "table"
                    or (entry.type ~= "DECOR" and entry.type ~= "DYE")
                    or type(entry.id) ~= "number"
                    or type(entry.qty) ~= "number"
                then
                    table.remove(entries, i)
                end
            end
        end
        if HousingHeraldDB.minimapButtonAngle == nil then
            HousingHeraldDB.minimapButtonAngle = 225
        end
        -- Which tracker home to use. The standalone window is the default: it
        -- is styled to match the objective tracker anyway, and it cannot taint
        -- the secure UI. Users can opt into the Blizzard tracker, and the taint
        -- watcher tells them how to come back if it misbehaves.
        if HousingHeraldDB.trackerMode ~= "blizzard"
            and HousingHeraldDB.trackerMode ~= "standalone" then
            HousingHeraldDB.trackerMode = "standalone"
        end
        -- Boolean mirror of trackerMode backing the settings-panel checkbox;
        -- re-derived from the canonical string each load.
        HousingHeraldDB.useBlizzardTracker = HousingHeraldDB.trackerMode == "blizzard"
        ns.db = HousingHeraldDB

        for _, evt in ipairs(AVAILABILITY_EVENTS) do
            pcall(self.RegisterEvent, self, evt)
        end

        if ns.InitMinimapButton then ns:InitMinimapButton() end
        if ns.InitTracker then ns:InitTracker() end
        if ns.InitCatalogHook then ns:InitCatalogHook() end
        if ns.RegisterSettingsPanel then pcall(ns.RegisterSettingsPanel, ns) end

        self:UnregisterEvent("ADDON_LOADED")
    else
        if ns.Availability and ns.Availability.HandleEvent then
            ns.Availability.HandleEvent(event)
        end
    end
end)
