local ldb = LibStub("LibDataBroker-1.1", true)
local dbicon = LibStub("LibDBIcon-1.0", true)

local ICON_PATH = "Interface\\AddOns\\CritMatic\\Media\\Textures\\critmatic-medallion.tga"

local function TopRecords()
    local bestCrit, bestCritName = 0, nil
    local bestHit = 0
    local bestHeal = 0
    for spellID, d in pairs(CritMaticData or {}) do
        if d.highestCrit and d.highestCrit > bestCrit then
            bestCrit = d.highestCrit
            bestCritName = GetSpellInfo(spellID)
        end
        if d.highestNormal and d.highestNormal > bestHit then
            bestHit = d.highestNormal
        end
        if d.highestHealCrit and d.highestHealCrit > bestHeal then
            bestHeal = d.highestHealCrit
        end
    end
    return bestCrit, bestCritName, bestHit, bestHeal
end

if ldb then
    Critmatic.brokerObject = ldb:NewDataObject("CritMatic", {
        type = "data source",
        text = "CritMatic",
        icon = ICON_PATH,
        OnClick = function(_, button)
            if button == "RightButton" then
                Critmatic:OpenOptions()
            else
                Critmatic:OpenCritLog()
            end
        end,
        OnTooltipShow = function(tooltip)
            local bestCrit, bestCritName, bestHit, bestHeal = TopRecords()
            tooltip:AddLine("|cffffd700CritMatic|r")
            if bestCrit > 0 then
                tooltip:AddDoubleLine("Top Crit", (bestCritName and (bestCrit .. "  " .. bestCritName)) or tostring(bestCrit))
            end
            if bestHit > 0 then
                tooltip:AddDoubleLine("Top Hit", tostring(bestHit))
            end
            if bestHeal > 0 then
                tooltip:AddDoubleLine("Top Crit Heal", tostring(bestHeal))
            end
            tooltip:AddLine("Left-click: Crit Log", 0.8, 0.8, 0.8)
            tooltip:AddLine("Right-click: Options", 0.8, 0.8, 0.8)
        end,
    })
end

function Critmatic.UpdateBroker()
    if not Critmatic.brokerObject then return end
    local bestCrit = TopRecords()
    if bestCrit and bestCrit > 0 then
        Critmatic.brokerObject.text = "Top Crit: " .. bestCrit
    else
        Critmatic.brokerObject.text = "CritMatic"
    end
end

function Critmatic.SetupMinimapIcon()
    if not dbicon or not Critmatic.brokerObject then return end
    if not Critmatic.db or not Critmatic.db.profile then return end
    Critmatic.db.profile.minimap = Critmatic.db.profile.minimap or { hide = false }
    if not dbicon:IsRegistered("CritMatic") then
        dbicon:Register("CritMatic", Critmatic.brokerObject, Critmatic.db.profile.minimap)
    end
    if Critmatic.db.profile.minimap.hide then
        dbicon:Hide("CritMatic")
    else
        dbicon:Show("CritMatic")
    end
end

function Critmatic.SetMinimapHidden(hide)
    if not Critmatic.db or not Critmatic.db.profile then return end
    Critmatic.db.profile.minimap = Critmatic.db.profile.minimap or { hide = false }
    Critmatic.db.profile.minimap.hide = hide and true or false
    if not dbicon then return end
    if hide then
        dbicon:Hide("CritMatic")
    else
        dbicon:Show("CritMatic")
    end
end
