local T=Transmoggin

function T:CreateMinimapButton()
    if not Minimap then return end
    if self.minimapButton then return end

    local b=CreateFrame("Button","TransmogginMinimapButton",Minimap)
    self.minimapButton=b
    b:SetSize(34,34)
    b:SetFrameStrata("MEDIUM")
    b:SetFrameLevel((Minimap:GetFrameLevel() or 0)+8)
    b:RegisterForClicks("LeftButtonUp","RightButtonUp")
    b:RegisterForDrag("LeftButton")

    -- Keep every visible element centered on the same origin.  The previous
    -- build used Blizzard's TrackingBorder texture, whose artwork contains a
    -- built-in offset and appeared as a second circle on some Classic clients.
    local ring=b:CreateTexture(nil,"BACKGROUND")
    ring:SetTexture("Interface\\AddOns\\Transmoggin\\Media\\minimap_ring.tga")
    ring:SetSize(34,34)
    ring:SetPoint("CENTER",b,"CENTER",0,0)
    ring:SetTexCoord(0,1,0,1)
    b.ring=ring

    local icon=b:CreateTexture(nil,"ARTWORK")
    icon:SetTexture("Interface\\AddOns\\Transmoggin\\Media\\rooster_minimap.tga")
    icon:SetSize(28,28)
    icon:SetPoint("CENTER",b,"CENTER",0,0)
    icon:SetTexCoord(.04,.96,.04,.96)
    b.icon=icon

    local hi=b:CreateTexture(nil,"HIGHLIGHT")
    hi:SetTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")
    hi:SetBlendMode("ADD")
    hi:SetSize(32,32)
    hi:SetPoint("CENTER",b,"CENTER",0,0)

    local function position()
        local a=math.rad(T.db.profile.minimap.angle or 215)
        -- Place the button so its center rides the minimap rim rather than
        -- floating far outside it. This behaves better on round minimaps and
        -- common minimap replacement addons.
        local w=Minimap:GetWidth() or 140
        local h=Minimap:GetHeight() or w
        local rx=(w/2)+3
        local ry=(h/2)+3
        b:ClearAllPoints()
        b:SetPoint("CENTER",Minimap,"CENTER",math.cos(a)*rx,math.sin(a)*ry)
    end

    b:SetScript("OnClick",function(self,btn)
        if btn=="RightButton" then
            T:EnsureUI(); T:ToggleTracker()
        else
            T:EnsureUI(); T:ToggleMain()
        end
    end)
    b:SetScript("OnEnter",function(self)
        if not T:TooltipsEnabled() then return end
        GameTooltip:SetOwner(self,"ANCHOR_LEFT")
        GameTooltip:SetText("Transmoggin",1,.72,.25)
        GameTooltip:AddLine("Left-click: Open Transmoggin",1,1,1)
        GameTooltip:AddLine("Right-click: Missing-piece tracker",1,1,1)
        GameTooltip:AddLine("Drag: Move around the minimap",.7,.7,.7)
        GameTooltip:Show()
    end)
    b:SetScript("OnLeave",function() GameTooltip:Hide() end)
    b:SetScript("OnDragStart",function(self) self.drag=true end)
    b:SetScript("OnDragStop",function(self) self.drag=false end)
    b:SetScript("OnUpdate",function(self)
        if not self.drag then return end
        local mx,my=Minimap:GetCenter()
        local x,y=GetCursorPosition()
        local scale=UIParent:GetEffectiveScale()
        x=x/scale; y=y/scale
        T.db.profile.minimap.angle=math.deg(math.atan2(y-my,x-mx))
        position()
    end)

    position()
    b:SetShown(not self.db.profile.minimap.hide)
end
