local _, Ns = ...

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

---------------------------------------------------------------------------
-- single-point show / hide with profile refresh
---------------------------------------------------------------------------
local function ShowHUD()
	Ns.Profile.HideHUD = false
	SinStatsFrame.HUD:Show()
	Ns:InitialiseProfile(Ns.Profile)
end

local function HideHUD()
	Ns.Profile.HideHUD = true
	SinStatsFrame.HUD:Hide()
	Ns:InitialiseProfile(Ns.Profile)
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 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
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")