local _, Ns = ...

local INSTANCE_EVENTS = {
	{ type = "party",    key = "EventDungeon" },
	{ type = "raid",     key = "EventRaid" },
	{ type = "pvp",      key = "EventPvP" },
	{ type = "arena",    key = "EventArena" },
	{ type = "scenario", key = "EventDelves" },
}

---------------------------------------------------------------------------
-- single-point show / hide with profile refresh
---------------------------------------------------------------------------
local hideTimer

local function CancelHideTimer()
	if hideTimer then
		hideTimer:Cancel()
		hideTimer = nil
	end
end

local function ShowHUD()
	CancelHideTimer()
	Ns.Profile.HideHUD = false
	SinStatsFrame.HUD:Show()
end

local function HideHUD()
	local stats = Ns.Profile.Stats
	if stats and stats.EventTimer and stats.EventTime then
		CancelHideTimer()
		hideTimer = C_Timer.NewTimer(stats.EventTime, function()
			hideTimer = nil
			Ns.Profile.HideHUD = true
			SinStatsFrame.HUD:Hide()
		end)
	else
		CancelHideTimer()
		Ns.Profile.HideHUD = true
		SinStatsFrame.HUD:Hide()
	end
end

---------------------------------------------------------------------------
-- Event handler
---------------------------------------------------------------------------
local f = CreateFrame("Frame")
f:SetScript("OnEvent", function(self, event)

	-- Pet battles – always toggle regardless of settings
	if event == "PET_BATTLE_OPENING_START" then
		Ns.Profile.HideHUD = true
		SinStatsFrame.HUD:Hide()
		return
	elseif event == "PET_BATTLE_OVER" then
		Ns.Profile.HideHUD = false
		SinStatsFrame.HUD:Show()
		return
	end

	local stats = Ns.Profile.Stats
	if not stats.EventEnable then return end


	if event == "PLAYER_ENTERING_WORLD" then
		local inInstance, instanceType = IsInInstance()

		if not Ns.CharPanel and not stats.EventCombat then
			local shouldShow, shouldHide = false, false

			-- Evaluate each instance-type event
			for _, entry in ipairs(INSTANCE_EVENTS) do
				local enabled = stats[entry.key]
				if instanceType == entry.type then
					-- Inside this instance type
					if enabled then
						shouldShow = true
					elseif Ns.eventChecked then
						shouldHide = true
					end
				elseif enabled and not inInstance then
					-- Setting enabled but not in any instance
					shouldHide = true
				end
			end

			-- Open-world event (overrides instance-based hides)
			if stats.EventWorld and instanceType == "none" then
				shouldShow = true
				shouldHide = false
			end

			if shouldShow then
				ShowHUD()
			elseif shouldHide then
				HideHUD()
			end
		end

		-- Combat-only mode: hide when not in combat and no location event is on
		if stats.EventCombat
			and not stats.EventWorld
			and not stats.EventDungeon
			and not stats.EventRaid
			and not stats.EventPvP
			and not stats.EventArena
			and not stats.EventDelves
			and (not UnitAffectingCombat("player") or not InCombatLockdown())
		then
			HideHUD()
		end

	-----------------------------------------------------------------------
	-- Combat enter
	-----------------------------------------------------------------------
	elseif event == "PLAYER_REGEN_DISABLED" then
		if stats.EventCombat and not Ns.CharPanel
			and (UnitAffectingCombat("player") or InCombatLockdown())
		then
			ShowHUD()
		end

	-----------------------------------------------------------------------
	-- Combat leave
	-----------------------------------------------------------------------
	elseif event == "PLAYER_REGEN_ENABLED" then
		if stats.EventCombat and not Ns.CharPanel
			and (not UnitAffectingCombat("player") or not InCombatLockdown())
		then
			HideHUD()
		end
	end

	-----------------------------------------------------------------------
	-- Auto-Hide: Resting & Mounted
	-- These run after all other logic so they can override show decisions
	-----------------------------------------------------------------------
	if stats.EventEnable and not Ns.CharPanel then
		-- Resting: hide in inns/cities
		if stats.EventResting then
			if IsResting() then
				HideHUD()
				return
			elseif event == "PLAYER_UPDATE_RESTING" then
				ShowHUD()
				return
			end
		end
		-- Mounted: hide while on a mount
		if stats.EventMounted then
			if IsMounted() then
				HideHUD()
				return
			elseif event == "PLAYER_MOUNT_DISPLAY_CHANGED" then
				ShowHUD()
				return
			end
		end
	end
end)

f:RegisterEvent("PLAYER_ENTERING_WORLD")
f:RegisterEvent("PLAYER_REGEN_DISABLED")
f:RegisterEvent("PLAYER_REGEN_ENABLED")
f:RegisterEvent("PET_BATTLE_OPENING_START")
f:RegisterEvent("PET_BATTLE_OVER")
f:RegisterEvent("PLAYER_UPDATE_RESTING")
f:RegisterEvent("PLAYER_MOUNT_DISPLAY_CHANGED")