local addonName, LRH = ...

LRH = LRH or {}
local L = LRH.L or {}

local function CreateMinimapButton()
    if LeftRightMinimapButton then return end
    if not Minimap then return end

    local mini = CreateFrame("Button", "LeftRightMinimapButton", Minimap)
    mini:SetSize(31, 31)

    mini:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")
    mini:RegisterForClicks("LeftButtonUp", "RightButtonUp")

    local icon = mini:CreateTexture(nil, "OVERLAY")
    icon:SetAllPoints()
    icon:SetTexture(LRH.TEX_MINIMAP)

    mini:SetPoint("CENTER", Minimap, "CENTER", 80, 0)

    mini:SetScript("OnClick", function(_, btn)
        if btn == "LeftButton" then
            if LRH.SetDragMode then
                LRH:SetDragMode(not (LeftRightDB and LeftRightDB.dragEnabled))
            end

        elseif btn == "RightButton" then
            LeftRightDB.enabled = not LeftRightDB.enabled

            if LRH.UpdateVisibility then
                LRH:UpdateVisibility()
            end

            print("|cFFFFFF00"..(L.ADDON_NAME or "Left Right Helper")..": "..(LeftRightDB.enabled and (L.SHOWN or "shown") or (L.HIDDEN or "hidden")).."|r")
        end
    end)

    mini:SetScript("OnEnter", function(self)
        GameTooltip:SetOwner(self, "ANCHOR_LEFT")
        GameTooltip:AddLine(L.ADDON_NAME or "Left Right Helper", 1, 1, 0)
        GameTooltip:AddLine(L.MINIMAP_LEFT or "", 0.7, 0.7, 0.7)
        GameTooltip:AddLine(L.MINIMAP_RIGHT or "", 0.7, 0.7, 0.7)
        GameTooltip:Show()
    end)

    mini:SetScript("OnLeave", GameTooltip_Hide)
end

local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_LOGIN")
f:SetScript("OnEvent", CreateMinimapButton)