local MinimapShapes = {
	["ROUND"] = {true, true, true, true},
	["SQUARE"] = {false, false, false, false},
	["CORNER-TOPLEFT"] = {true, false, false, false},
	["CORNER-TOPRIGHT"] = {false, false, true, false},
	["CORNER-BOTTOMLEFT"] = {false, true, false, false},
	["CORNER-BOTTOMRIGHT"] = {false, false, false, true},
	["SIDE-LEFT"] = {true, true, false, false},
	["SIDE-RIGHT"] = {false, false, true, true},
	["SIDE-TOP"] = {true, false, true, false},
	["SIDE-BOTTOM"] = {false, true, false, true},
	["TRICORNER-TOPLEFT"] = {true, true, true, false},
	["TRICORNER-TOPRIGHT"] = {true, false, true, true},
	["TRICORNER-BOTTOMLEFT"] = {true, true, false, true},
	["TRICORNER-BOTTOMRIGHT"] = {false, true, true, true},
}

local _G = _G
local GetCursorPosition = _G.GetCursorPosition
local deg = _G.math.deg
local atan2 = _G.math.atan2
local rad = _G.math.rad
local sin = _G.math.sin
local cos = _G.math.cos
local max = _G.math.max
local min = _G.math.min
local sqrt = _G.math.sqrt
local mx, my, mz, cx, cy, cz, y, rangle, x, y, q, dradius

local DropMenu = LibStub("LibDropMenu-1.0")

function BlueItemInfo2:CreateMinimapButton()
	local f = CreateFrame("Button", "BlueItemInfo2MiniMapButton", Minimap)
	f:SetFrameStrata("LOW")
	f:SetWidth(33)
	f:SetHeight(33)
	f:SetPoint("CENTER")
	f:EnableMouse(true)
	f:RegisterForClicks("AnyUp")
	f:RegisterForDrag("LeftButton", "RightButton")
	f:SetNormalTexture(self.icon)
	f:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")
	local s = f:GetNormalTexture()
	s:SetWidth(18)
	s:SetHeight(18)
	s:ClearAllPoints()
	s:SetPoint("CENTER", f, "CENTER")
	local t = f:CreateTexture("BlueItemInfo2MiniMapButtonBorder", "OVERLAY")
	t:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder")
	t:SetWidth(52)
	t:SetHeight(52)
	t:SetPoint("TOPLEFT", f, "TOPLEFT")
	f.SetMinimapButtonPosition = function(self, angle, radius, rounding)
		BlueItemInfo2DB.minimapButton.angle = angle or BlueItemInfo2DB.minimapButton.angle
		BlueItemInfo2DB.minimapButton.radius = radius or BlueItemInfo2DB.minimapButton.radius
		BlueItemInfo2DB.minimapButton.rounding = rounding or BlueItemInfo2DB.minimapButton.rounding
		self:UpdatePosition()
	end
	f.UpdatePosition = function(self)
		GameTooltip:Hide()
		rangle = rad(BlueItemInfo2DB.minimapButton.angle)
		x = cos(rangle)
		y = sin(rangle)
		q = 1
		if x < 0 then
			q = q + 1
		end
		if y > 0 then
			q = q + 2
		end
		if MinimapShapes[GetMinimapShape and GetMinimapShape() or "ROUND"][q] then
			x = x * BlueItemInfo2DB.minimapButton.radius
			y = y * BlueItemInfo2DB.minimapButton.radius
		else
			dradius = sqrt(2 * (BlueItemInfo2DB.minimapButton.radius) ^ 2) - BlueItemInfo2DB.minimapButton.rounding
			x = max(-BlueItemInfo2DB.minimapButton.radius, min(x * dradius, BlueItemInfo2DB.minimapButton.radius))
			y = max(-BlueItemInfo2DB.minimapButton.radius, min(y * dradius, BlueItemInfo2DB.minimapButton.radius))
		end
		self:SetPoint("CENTER", Minimap, "CENTER", x, y - 1)
	end
	f.Toggle = function(self)
		if BlueItemInfo2DB.minimapButton.show then
			self:Show()
		else
			self:Hide()
		end
	end
	f:SetScript("OnDragStart", function(self)
		if BlueItemInfo2DB.minimapButton.dragable then
			self.dragme = true
			self:LockHighlight()
		end
	end)
	f:SetScript("OnDragStop", function(self)
		self.dragme = nil
		self:UnlockHighlight()
	end)
	f:SetScript("OnUpdate", function(self)
		if self.dragme then
			mx, my = Minimap:GetCenter()
			mz = MinimapCluster:GetScale()
			cx, cy = GetCursorPosition(UIParent)
			cz = UIParent:GetEffectiveScale()
			v = deg(atan2(cy / cz - my * mz, cx / cz - mx * mz))
			if v < 0 then
				v = v + 360
			elseif v > 360 then
				v = v - 360
			end
			self:SetMinimapButtonPosition(v)
		end
	end)
	f:SetScript("OnEnter", function(self)
		GameTooltip:SetOwner(self, "ANCHOR_LEFT")
		GameTooltip:AddLine(BlueItemInfo2.title.." v"..BlueItemInfo2.version)
		GameTooltip:AddLine(BlueItemInfo2.website, 1, 1, 1)
		GameTooltip:AddLine("좌클릭: GUI 열기", 1, 1, 0)
		GameTooltip:AddLine("우클릭: 메뉴 열기", 1, 1, 0)
		GameTooltip:Show()
	end)
	f:SetScript("OnLeave", function()
		GameTooltip:Hide()
	end)
	f:SetScript("OnClick", function(self, button)
		if button == "RightButton" then
			if DropMenu:IsOpen("BlueItemInfo2") then
				BlueItemInfo2:CloseDropMenu()
			else
				DropMenu:OpenMenu("BlueItemInfo2", "TOPRIGHT", self, "CENTER")
			end
		else
			BlueItemInfo2:OnClick()
		end
	end)
	f:UpdatePosition()
	f:Toggle()
end