--[[ 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

-- The drag stores angle AND distance, so the button sits wherever the player
-- drops it. A fixed radius assumed Blizzard's round minimap; the first user
-- request after launch was a square/oversized minimap where that lands wrong.
local MIN_RADIUS, MAX_RADIUS = 70, 160

local button

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

local function savePosition(angle, radius)
  ns.db.settings.minimap = ns.db.settings.minimap or {}
  ns.db.settings.minimap.angle = angle
  ns.db.settings.minimap.radius = radius
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))
  local dist = math.sqrt((px - mx) ^ 2 + (py - my) ^ 2)
  local radius = math.max(MIN_RADIUS, math.min(MAX_RADIUS, dist))
  positionAt(angle, radius)
  savePosition(angle, radius)
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, saved.radius)
  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
