-------------------------------
-- Map Marker Dragging Frame --
-------------------------------
local addon, ns = ...
local ss = ns.SS

local CreateFrame, Minimap, GetCursorPosition, UIParent = CreateFrame, Minimap, GetCursorPosition, UIParent
local math = math

local mtDraggingFrame = {};

--- Creates a new dragging frame.
-- @param parent The parent container.
--
function mtDraggingFrame:New(parent)
  -- Add the dragingFrame
  local dragingFrame = CreateFrame("Button", ss._f.."MinimapButton_DraggingFrame", parent)

  dragingFrame:SetScript("OnUpdate", function(self)
    if self:IsVisible() then
      local xpos,ypos = GetCursorPosition()
      local xmin,ymin = Minimap:GetLeft(), Minimap:GetBottom()
  
      xpos = xmin-xpos/UIParent:GetScale()+70 -- get coordinates as differences from the center of the minimap
      ypos = ypos/UIParent:GetScale()-ymin-70
  
      local locationAngle = math.deg(math.atan2(ypos,xpos)) -- save the degrees we are relative to the minimap center
  
      ss.MapMarker.MapButton:Reposition(locationAngle)
    end
  end)
  dragingFrame:Hide()

  return dragingFrame
end

ss.Utils.addModule("MapMarkerDraggingFrame", mtDraggingFrame)
