--- LibDBIcon-1.0 ---
-- Minimal implementation for minimap button support

local MAJOR, MINOR = "LibDBIcon-1.0", 47
local lib = LibStub:NewLibrary(MAJOR, MINOR)
if not lib then return end

lib.objects = lib.objects or {}
lib.callbackRegistered = lib.callbackRegistered or false
lib.callbacks = lib.callbacks or LibStub("CallbackHandler-1.0"):New(lib)
lib.notCreated = lib.notCreated or {}

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

local function GetCoord(angle)
    return math.cos(angle) * 80, math.sin(angle) * 80
end

local function UpdatePosition(button, db)
    local angle = math.rad(db and db.minimapPos or 225)
    local x, y = GetCoord(angle)
    button:SetPoint("CENTER", Minimap, "CENTER", x, y)
end

local function CreateButton(name, obj, db)
    local button = CreateFrame("Button", "LibDBIcon10_"..name, Minimap)
    button:SetFrameStrata("MEDIUM")
    button:SetSize(32, 32)
    button:SetFrameLevel(8)
    button:RegisterForClicks("anyUp")
    button:RegisterForDrag("LeftButton")
    button:SetHighlightTexture(136477) -- Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight

    local overlay = button:CreateTexture(nil, "OVERLAY")
    overlay:SetSize(56, 56)
    overlay:SetTexture(136430) -- Interface\\Minimap\\MiniMap-TrackingBorder
    overlay:SetPoint("TOPLEFT")

    local background = button:CreateTexture(nil, "BACKGROUND")
    background:SetSize(24, 24)
    background:SetTexture(136467) -- Interface\\Minimap\\UI-Minimap-Background
    background:SetPoint("CENTER")

    local icon = button:CreateTexture(nil, "ARTWORK")
    icon:SetSize(20, 20)
    icon:SetPoint("CENTER")
    if obj.icon then
        icon:SetTexture(obj.icon)
    end
    button.icon = icon

    button:SetScript("OnClick", function(self, btn)
        if obj.OnClick then
            obj.OnClick(self, btn)
        end
    end)

    button:SetScript("OnEnter", function(self)
        if obj.OnTooltipShow then
            GameTooltip:SetOwner(self, "ANCHOR_LEFT")
            obj.OnTooltipShow(GameTooltip)
            GameTooltip:Show()
        end
    end)

    button:SetScript("OnLeave", function()
        GameTooltip:Hide()
    end)

    button:SetScript("OnDragStart", function(self)
        isDragging = true
        self:LockHighlight()
        self:SetScript("OnUpdate", function(self)
            local mx, my = Minimap:GetCenter()
            local cx, cy = GetCursorPosition()
            local scale = UIParent:GetEffectiveScale()
            cx, cy = cx / scale, cy / scale
            db.minimapPos = math.deg(math.atan2(cy - my, cx - mx)) % 360
            UpdatePosition(self, db)
        end)
    end)

    button:SetScript("OnDragStop", function(self)
        isDragging = false
        self:SetScript("OnUpdate", nil)
        self:UnlockHighlight()
    end)

    lib.objects[name] = button
    if db and db.hide then
        button:Hide()
    end
    UpdatePosition(button, db)
end

function lib:Register(name, obj, db)
    if lib.objects[name] then return end
    db = db or {}
    if not db.minimapPos then db.minimapPos = 225 end
    CreateButton(name, obj, db)
end

function lib:Show(name)
    if lib.objects[name] then
        lib.objects[name]:Show()
    end
end

function lib:Hide(name)
    if lib.objects[name] then
        lib.objects[name]:Hide()
    end
end

function lib:IsRegistered(name)
    return lib.objects[name] ~= nil
end

function lib:GetMinimapButton(name)
    return lib.objects[name]
end
