--[[ UI/Minimap.lua - hand-rolled minimap button (no LibDBIcon: spec 5.1 keeps
     libraries out). Draggable around the minimap edge, position persisted as
     an angle so it survives resizing and different minimap shapes.

     Left click  - toggle the library
     Right click - toggle the button off (with a chat line saying how to get
                   it back, so nobody loses the addon by accident) ]]

local _, ns = ...
ns.UI = ns.UI or {}
local UI = ns.UI

local RADIUS = 80          -- distance from minimap centre
local DEFAULT_ANGLE = 205  -- lower-left, usually free of Blizzard's own buttons

local button

local function positionAt(angle)
  local rad = math.rad(angle)
  button:SetPoint("CENTER", Minimap, "CENTER",
    math.cos(rad) * RADIUS, math.sin(rad) * RADIUS)
end

local function saveAngle(angle)
  ns.db.settings.minimap = ns.db.settings.minimap or {}
  ns.db.settings.minimap.angle = angle
end

local function onUpdateDrag(self)
  local mx, my = Minimap:GetCenter()
  local px, py = GetCursorPosition()
  local scale = Minimap:GetEffectiveScale()
  px, py = px / scale, py / scale
  local angle = math.deg(math.atan2(py - my, px - mx))
  positionAt(angle)
  saveAngle(angle)
end

function UI.BuildMinimapButton()
  if button then return end
  if not Minimap then return end

  button = CreateFrame("Button", "BlueprintBazaarMinimapButton", Minimap)
  button:SetSize(31, 31)
  button:SetFrameStrata("MEDIUM")
  button:SetFrameLevel(8)
  button:RegisterForClicks("LeftButtonUp", "RightButtonUp")
  button:RegisterForDrag("LeftButton")
  button:SetMovable(true)

  -- No Blizzard tracking-border ring: the icon is already round, and the ring
  -- only shrank it into an unreadable dot. The texture fills the button.
  local icon = button:CreateTexture(nil, "ARTWORK")
  icon:SetTexture("Interface\\AddOns\\BlueprintBazaar\\Media\\minimap")
  icon:SetAllPoints()
  button.icon = icon

  button:SetScript("OnMouseDown", function(self) self.icon:SetPoint("CENTER", 1, -1) end)
  button:SetScript("OnMouseUp", function(self)
    self.icon:ClearAllPoints()
    self.icon:SetAllPoints()
  end)

  button:SetScript("OnDragStart", function(self)
    self:SetScript("OnUpdate", onUpdateDrag)
  end)
  button:SetScript("OnDragStop", function(self)
    self:SetScript("OnUpdate", nil)
  end)

  button:SetScript("OnClick", function(self, mouseButton)
    if mouseButton == "RightButton" then
      ns.db.settings.minimap = ns.db.settings.minimap or {}
      ns.db.settings.minimap.hide = true
      self:Hide()
      print("|cffc9a227BlueprintBazaar|r minimap button hidden. "
        .. "Type |cffffd100/bb minimap|r to bring it back.")
      return
    end
    if UI.Toggle then UI.Toggle() end
  end)

  button:SetScript("OnEnter", function(self)
    GameTooltip:SetOwner(self, "ANCHOR_LEFT")
    GameTooltip:AddLine("BlueprintBazaar")
    GameTooltip:AddLine("Left click to open the library", 1, 1, 1)
    GameTooltip:AddLine("Drag to move, right click to hide", 0.7, 0.7, 0.7)
    GameTooltip:Show()
  end)
  button:SetScript("OnLeave", function() GameTooltip:Hide() end)

  local saved = ns.db.settings.minimap or {}
  positionAt(saved.angle or DEFAULT_ANGLE)
  if saved.hide then button:Hide() end
end

--- Toggle visibility from /bb minimap
function UI.ToggleMinimapButton()
  if not button then UI.BuildMinimapButton() end
  if not button then return end
  ns.db.settings.minimap = ns.db.settings.minimap or {}
  local hidden = not ns.db.settings.minimap.hide
  ns.db.settings.minimap.hide = hidden
  if hidden then button:Hide() else button:Show() end
  print("|cffc9a227BlueprintBazaar|r minimap button "
    .. (hidden and "hidden." or "shown."))
end
