-- Minimap button for RC Party Orbs
RCPartyOrbsMiniMapButtonPosition = {
	locationAngle = 45, -- degrees
	x = 52-(80*math.cos(math.rad(45))),
	y = ((80*math.sin(math.rad(45)))-52)
}

-- Call this to move the minimap button to its saved position
function RCPartyOrbsMiniMapButton_Reposition()
	RCPartyOrbsMiniMapButtonPosition.x = 52-(80*math.cos(math.rad(RCPartyOrbsMiniMapButtonPosition.locationAngle)))
	RCPartyOrbsMiniMapButtonPosition.y = ((80*math.sin(math.rad(RCPartyOrbsMiniMapButtonPosition.locationAngle)))-52)
	RCPartyOrbsMiniMapButton:SetPoint("TOPLEFT","Minimap","TOPLEFT",RCPartyOrbsMiniMapButtonPosition.x,RCPartyOrbsMiniMapButtonPosition.y)
end

function RCPartyOrbsMiniMapButtonPosition_LoadFromDefaults()
	-- Load saved position if available
	if RCPartyOrbsDB and RCPartyOrbsDB.minimapButtonAngle then
		RCPartyOrbsMiniMapButtonPosition.locationAngle = RCPartyOrbsDB.minimapButtonAngle
	end
	RCPartyOrbsMiniMapButton_Reposition()
end

-- Only while the button is dragged this is called every frame
function RCPartyOrbs_Minimap_Update()
	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

	RCPartyOrbsMiniMapButtonPosition.locationAngle = math.deg(math.atan2(ypos,xpos)) -- save the degrees we are relative to the minimap center
	RCPartyOrbsMiniMapButton_Reposition() -- move the button
	
	-- Save position
	if not RCPartyOrbsDB then RCPartyOrbsDB = {} end
	RCPartyOrbsDB.minimapButtonAngle = RCPartyOrbsMiniMapButtonPosition.locationAngle
end

-- Put your code that you want on a minimap button click here.  arg1="LeftButton", "RightButton", etc
function RCPartyOrbsMiniMapButton_OnClick()
	if Handle_RCPartyOrbsGUI then
		Handle_RCPartyOrbsGUI()
	else
		-- Fallback: try to show the menu directly
		if RCPartyOrbsGUI then
			if RCPartyOrbsGUI:IsVisible() then
				RCPartyOrbsGUI:Hide()
				PlaySound(830)  -- SPELLBOOKCLOSE
			else
				RCPartyOrbsGUI:Show()
				PlaySound(829)  -- SPELLBOOKOPEN
				if RCPartyOrbs_UpdateMenu then
					RCPartyOrbs_UpdateMenu()
				end
			end
		end
	end
end
