GuildHelper = GuildHelper or {}

local addon = GuildHelper

addon.MinimapButton = addon.MinimapButton or {}

local ICON_PATH = "Interface\\AddOns\\GH3\\Assets\\MinimapIcon"

local function getSettings()
    local profile = addon.Store:GetProfile()
    local settings = profile.minimap

    if type(settings) ~= "table" then
        settings = {}
        profile.minimap = settings
    end

    if type(settings.hidden) ~= "boolean" then
        settings.hidden = false
    end

    return settings
end

local function updatePosition(button)
    if not button or not Minimap then
        return
    end

        local function getAddonCompartmentAnchor(anchor)
            if type(anchor) == "table" and anchor.GetObjectType then
                return anchor
            end

            if _G.AddonCompartmentFrame then
                return _G.AddonCompartmentFrame
            end

            return UIParent
        end


    local settings = getSettings()

    button:ClearAllPoints()
    if type(settings.position) == "table" and settings.position[1] then
        button:SetPoint(unpack(settings.position))
    else
        button:SetPoint("TOPLEFT", Minimap, "TOPLEFT", 0, 0)
    end
end

local function showTooltip(button)
    GameTooltip:SetOwner(button, "ANCHOR_LEFT")
    GameTooltip:AddLine("GuildHelper v3")
    GameTooltip:AddLine("Left-click: Toggle GuildHelper", 1, 1, 1)
    GameTooltip:AddLine("Right-click: Quick links", 1, 1, 1)
    GameTooltip:AddLine("Left-drag: Move button", 1, 1, 1)
    GameTooltip:AddLine("/gh3 minimap: Show or hide button", 0.85, 0.82, 0.7)
    GameTooltip:Show()
end

function addon.MinimapButton:RefreshVisibility()
    if not self.button then
        return
    end

    local settings = getSettings()

    if settings.hidden then
        self.button:Hide()
        return
    end

    self.button:Show()
    updatePosition(self.button)
end

function addon.MinimapButton:ToggleVisibility()
    local settings = getSettings()
    settings.hidden = not settings.hidden
    self:RefreshVisibility()
    return settings.hidden
end

function addon.MinimapButton:Initialize()
    if self.button or not Minimap then
        return
    end

    local button = CreateFrame("Button", "GH3MinimapButton", Minimap)
    button:SetSize(31, 31)
    button:SetFrameStrata("MEDIUM")
    button:SetFrameLevel((Minimap:GetFrameLevel() or 0) + 8)
    button:RegisterForClicks("LeftButtonUp", "RightButtonUp")
    button:RegisterForDrag("LeftButton")
    button:SetMovable(true)
    button:EnableMouse(true)
    button:SetClampedToScreen(true)

        function addon.MinimapButton:HandleAddonCompartmentClick()
            if addon.MainFrame and addon.MainFrame.Toggle then
                addon.MainFrame:Toggle()
            end
        end

        function addon.MinimapButton:HandleAddonCompartmentEnter(anchor)
            local owner = getAddonCompartmentAnchor(anchor)
            GameTooltip:SetOwner(owner, "ANCHOR_LEFT")
            GameTooltip:AddLine("GuildHelper v3")
            GameTooltip:AddLine("Click: Toggle GuildHelper", 1, 1, 1)
            GameTooltip:AddLine("/gh3 minimap: Show or hide minimap button", 0.85, 0.82, 0.7)
            GameTooltip:Show()
        end

        function addon.MinimapButton:HandleAddonCompartmentLeave()
            GameTooltip:Hide()
        end

        function GH3_AddonCompartment_Click(...)
            addon.MinimapButton:HandleAddonCompartmentClick(...)
        end

        function GH3_AddonCompartment_Enter(...)
            addon.MinimapButton:HandleAddonCompartmentEnter(...)
        end

        function GH3_AddonCompartment_Leave(...)
            addon.MinimapButton:HandleAddonCompartmentLeave(...)
        end

    local icon = button:CreateTexture(nil, "BACKGROUND")
    icon:SetTexture(ICON_PATH)
    icon:SetSize(20, 20)
    icon:SetPoint("TOPLEFT", button, "TOPLEFT", 7, -7)
    icon:SetTexCoord(0, 1, 0, 1)
    button.icon = icon

    local border = button:CreateTexture(nil, "OVERLAY")
    border:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder")
    border:SetSize(53, 53)
    border:SetPoint("TOPLEFT", button, "TOPLEFT", 0, 0)

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

    button:SetScript("OnClick", function(_, mouseButton)
        if mouseButton == "RightButton" then
            if addon.MainFrame and addon.MainFrame.ShowMinimapQuickLinkMenu then
                addon.MainFrame:ShowMinimapQuickLinkMenu(button)
            end
            return
        end

        addon.MainFrame:Toggle()
    end)

    button:SetScript("OnEnter", function(self)
        showTooltip(self)
    end)

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

    button:SetScript("OnDragStart", function(self)
        self:StartMoving()
    end)

    button:SetScript("OnDragStop", function(self)
        self:StopMovingOrSizing()
        getSettings().position = { self:GetPoint() }
    end)

    self.button = button
    self:RefreshVisibility()
end
