-- LongChat minimap launcher.

local LC = _G.LongChat
local UI = LC and LC.UI
if not UI then
  return
end

local LDB = LibStub and LibStub("LibDataBroker-1.1", true)
local LDBIcon = LibStub and LibStub("LibDBIcon-1.0", true)

function UI:RegisterMinimap()
  if self.minimapRegistered or not LDB or not LDBIcon then
    return
  end

  local launcher = LDB:NewDataObject("LongChat", {
    type = "launcher",
    text = "LongChat",
    icon = "Interface\\Icons\\INV_Scroll_03",
    OnClick = function(_, buttonName)
      if buttonName == "LeftButton" then
        LC.ToggleComposer()
      else
        LC.SafePrint("Left-click toggles the LongChat composer.")
      end
    end,
    OnTooltipShow = function(launcherTooltip)
      launcherTooltip:AddLine("LongChat")
      launcherTooltip:AddLine("Left-click: toggle composer", 0.8, 0.8, 0.8)
      launcherTooltip:AddLine("Built-in spellcheck and long-message splitting stay active in normal chat.", 0.8, 0.8, 0.8)
    end,
  })

  LDBIcon:Register("LongChat", launcher, self:Editor().minimap)
  if self:Editor().minimap.hide then
    LDBIcon:Hide("LongChat")
  end
  self.minimapRegistered = true
end

function LC.SetMinimapHidden(hidden)
  UI:Editor().minimap.hide = hidden == true
  if not LDBIcon then
    return
  end
  if hidden then
    LDBIcon:Hide("LongChat")
  else
    LDBIcon:Show("LongChat")
  end
end

hooksecurefunc(LC, "OnAddonReady", function()
  UI:RegisterMinimap()
end)
