local _, Orbit = ...
local L = Orbit.L
local OrbitEngine = Orbit.Engine

-- [ ORBIT OPTIONS BUTTON ]---------------------------------------------------------------------------
local BUTTON_WIDTH = 200
local BUTTON_HEIGHT = 40
local BUTTON_PADDING = 20

-- Create the button
local Button = CreateFrame("Button", "OrbitOptionsButton", UIParent, "BackdropTemplate")
Button:SetSize(BUTTON_WIDTH, BUTTON_HEIGHT)
Button:SetPoint("TOPLEFT", UIParent, "TOPLEFT", BUTTON_PADDING, -BUTTON_PADDING)
Button:SetFrameStrata(Orbit.Constants.Strata.Topmost)
Button:SetFrameLevel(500)
Button:Hide()

-- Backdrop (background + border unified)
Button:SetBackdrop({
    bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
    edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
    tile = true,
    tileSize = 16,
    edgeSize = 16,
    insets = { left = 4, right = 4, top = 4, bottom = 4 },
})
Button:SetBackdropColor(0.1, 0.1, 0.12, 0.95)
Button:SetBackdropBorderColor(0.4, 0.4, 0.45, 1)

-- Highlight on hover
Button.highlight = Button:CreateTexture(nil, "HIGHLIGHT")
Button.highlight:SetPoint("TOPLEFT", 4, -4)
Button.highlight:SetPoint("BOTTOMRIGHT", -4, 4)
Button.highlight:SetColorTexture(0.2, 0.4, 0.6, 0.3)

-- Label
Button.label = Button:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
Button.label:SetPoint("CENTER", Button, "CENTER", 0, 0)
Button.label:SetText(L.CMD_OPTIONS_BUTTON_LABEL)
Button.label:SetTextColor(0.9, 0.9, 0.95)

-- Tooltip
Button:SetScript("OnEnter", function(self)
    GameTooltip:SetOwner(self, "ANCHOR_CURSOR_RIGHT")
    GameTooltip:AddLine(L.CMD_OPTIONS_BUTTON_LABEL, 1, 1, 1)
    GameTooltip:AddLine(" ")
    GameTooltip:AddLine(L.CFG_OPTIONS_TT_LEFT, 0.8, 0.8, 0.8)
    GameTooltip:AddLine(L.CFG_OPTIONS_TT_RIGHT, 0.8, 0.8, 0.8)
    GameTooltip:Show()
end)

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

-- Register for both mouse buttons
Button:RegisterForClicks("LeftButtonUp", "RightButtonUp")

-- Click Handler
Button:SetScript("OnClick", function(self, button)
    if InCombatLockdown() then
        return
    end

    if button == "LeftButton" then
        Orbit.OptionsPanel:Open(L.CFG_TAB_GLOBAL)
    elseif button == "RightButton" then
        Orbit.OptionsPanel:Open(L.CFG_TAB_TEXTURES)
    end
end)

-- [ COMBAT INDICATOR ]-------------------------------------------------------------------------------
local PULSE_SPEED = 3

local function OnUpdate_Pulse(self, elapsed)
    self.pulseTimer = (self.pulseTimer or 0) + elapsed
    -- Pulse alpha between 0.3 and 1.0
    local alpha = 0.3 + 0.7 * math.abs(math.sin(self.pulseTimer * PULSE_SPEED))
    self:SetBackdropBorderColor(1, 0.2, 0.2, alpha)
    -- Also pulse text slightly for effect? No, just border per request.
end

Button:RegisterEvent("PLAYER_REGEN_DISABLED")
Button:RegisterEvent("PLAYER_REGEN_ENABLED")

Button:SetScript("OnEvent", function(self, event)
    if event == "PLAYER_REGEN_DISABLED" then
        -- Enter Combat State
        self.label:SetText(L.CMN_IN_COMBAT)
        self.label:SetTextColor(1, 0.2, 0.2)

        self:Disable() -- Disable clicks

        self.pulseTimer = 0
        self:SetScript("OnUpdate", OnUpdate_Pulse)
    elseif event == "PLAYER_REGEN_ENABLED" then
        -- Exit Combat State
        self.label:SetText(L.CMD_OPTIONS_BUTTON_LABEL)
        self.label:SetTextColor(0.9, 0.9, 0.95)

        self:Enable() -- Re-enable clicks

        self:SetScript("OnUpdate", nil)
        self:SetBackdropBorderColor(0.4, 0.4, 0.45, 1) -- Restore default border
    end
end)

-- Initial check
if InCombatLockdown() then
    Button.label:SetText(L.CMN_IN_COMBAT)
    Button.label:SetTextColor(1, 0.2, 0.2)
    Button:Disable()
    Button.pulseTimer = 0
    Button:SetScript("OnUpdate", OnUpdate_Pulse)
end

-- [ EDIT MODE LIFECYCLE ]----------------------------------------------------------------------------
if EventRegistry then
    EventRegistry:RegisterCallback("EditMode.Enter", function() Button:Show() end, Button)
    EventRegistry:RegisterCallback("EditMode.Exit", function()
        Button:Hide()
        if Orbit.OptionsPanel and Orbit.OptionsPanel.Hide then
            Orbit.OptionsPanel:Hide()
        end
    end, Button)
end

-- Export
Orbit.OptionsButton = Button
