local addonName, ns = ...
local L = ns.L or {}

local BROKER_NAME = addonName or "Vaultloom"
local BROKER_ICON = "Interface\\Icons\\Inv_10_gearupgrade_drakesshadowflameenhancedcrest"
local BROKER_ICON_COORDS = { 0.08, 0.92, 0.08, 0.92 }

local function addBrokerTooltipLines(tooltip)
    if type(tooltip) ~= "table" or type(tooltip.AddLine) ~= "function" then
        return
    end

    tooltip:AddLine(L.ADDON_TITLE or "Vaultloom", 0.39, 0.85, 1.00)
    tooltip:AddLine(L.MINIMAP_TOOLTIP_OPEN_SHORT or "Click: open", 0.90, 0.90, 0.90, true)
end

function ns:InitializeBroker()
    if self.brokerObject then
        return self.brokerObject
    end

    if type(LibStub) ~= "table" then
        return nil
    end

    local ldb = LibStub("LibDataBroker-1.1", true)
    if not ldb then
        return nil
    end

    local object = ldb:GetDataObjectByName(BROKER_NAME)
    if not object then
        object = ldb:NewDataObject(BROKER_NAME, {
            type = "launcher",
            label = L.ADDON_TITLE or "Vaultloom",
            text = L.ADDON_TITLE or "Vaultloom",
            icon = BROKER_ICON,
            iconCoords = BROKER_ICON_COORDS,
        })
    end

    if not object then
        return nil
    end

    object.type = "launcher"
    object.label = L.ADDON_TITLE or "Vaultloom"
    object.text = L.ADDON_TITLE or "Vaultloom"
    object.icon = BROKER_ICON
    object.iconCoords = BROKER_ICON_COORDS
    object.OnClick = function(_, button)
        if button ~= "LeftButton" then
            return
        end

        if ns.HandleSlashCommand then
            ns:HandleSlashCommand("")
            return
        end

        if ns.UI and ns.UI.Toggle then
            ns.UI:Toggle()
        end
    end
    object.OnTooltipShow = function(tooltip)
        addBrokerTooltipLines(tooltip)
    end
    object.OnEnter = function(frame)
        if not GameTooltip then
            return
        end

        GameTooltip:SetOwner(frame, "ANCHOR_NONE")
        GameTooltip:SetPoint("TOPLEFT", frame, "BOTTOMLEFT", 0, -2)
        addBrokerTooltipLines(GameTooltip)
        GameTooltip:Show()
    end
    object.OnLeave = function()
        if GameTooltip then
            GameTooltip:Hide()
        end
    end

    self.brokerObject = object
    return object
end

function ns:GetBrokerMinimapDB()
    if not self.db or not self.db.ui then
        return nil
    end

    self.db.ui.minimap = self.db.ui.minimap or {}
    local db = self.db.ui.minimap
    db.hide = db.hide == true or db.hidden == true
    db.hidden = db.hide == true
    db.minimapPos = tonumber(db.minimapPos or db.angle) or 225
    db.angle = tonumber(db.minimapPos) or 225
    return db
end

function ns:EnsureBrokerMinimapIcon()
    if type(LibStub) ~= "table" then
        return false
    end

    local dbIcon = LibStub("LibDBIcon-1.0", true)
    if not dbIcon then
        return false
    end

    local object = self:InitializeBroker()
    local db = self:GetBrokerMinimapDB()
    if not object or not db then
        return false
    end

    if not dbIcon.IsRegistered or not dbIcon:IsRegistered(BROKER_NAME) then
        local ok = pcall(function()
            dbIcon:Register(BROKER_NAME, object, db)
        end)
        if not ok then
            return false
        end
    end

    if dbIcon.Refresh then
        dbIcon:Refresh(BROKER_NAME, db)
    end

    self.brokerMinimapRegistered = true
    return true
end

function ns:RefreshBrokerMinimapIcon()
    if type(LibStub) ~= "table" then
        return false
    end

    local dbIcon = LibStub("LibDBIcon-1.0", true)
    if not dbIcon or not dbIcon.IsRegistered or not dbIcon:IsRegistered(BROKER_NAME) then
        return false
    end

    local db = self:GetBrokerMinimapDB()
    if not db then
        return false
    end

    if dbIcon.Refresh then
        dbIcon:Refresh(BROKER_NAME, db)
    else
        if db.hide then
            dbIcon:Hide(BROKER_NAME)
        else
            dbIcon:Show(BROKER_NAME)
        end
    end

    return true
end
