local _, Ns = ...

local f = CreateFrame("Frame")
f:SetScript("OnEvent", function(self, event, ...)

	if event == "PET_BATTLE_OPENING_START" then
		Ns.Profile.HideHUD = true
		SinStatsFrame.HUD:Hide()
	elseif event == "PET_BATTLE_OVER" then
		Ns.Profile.HideHUD = false
		SinStatsFrame.HUD:Show()
	end

	if event == "PLAYER_ENTERING_WORLD" then

--	local isInitialLogin, isReloadingUi = ...
--	if isInitialLogin then
--		C_Timer.After(1, Ns.ResizeBackground)
--	end

if Ns.Profile.Stats.EventEnable then
	local inInstance, instanceType = IsInInstance()
	
	if (Ns.Profile.Stats.EventDungeon and instanceType == "party" and not Ns.CharPanel and not Ns.Profile.Stats.EventCombat) then
		Ns.Profile.HideHUD = false
		SinStatsFrame.HUD:Show()
		Ns:InitialiseProfile(Ns.Profile)
	elseif (Ns.Profile.Stats.EventDungeon and not inInstance and not Ns.CharPanel and not Ns.Profile.Stats.EventCombat) then
		Ns.Profile.HideHUD = true
		SinStatsFrame.HUD:Hide()
		Ns:InitialiseProfile(Ns.Profile)
	elseif (not Ns.Profile.Stats.EventDungeon and Ns.eventChecked and instanceType == "party" and not Ns.CharPanel and not Ns.Profile.Stats.EventCombat) then
		Ns.Profile.HideHUD = true	
		SinStatsFrame.HUD:Hide()
		Ns:InitialiseProfile(Ns.Profile)
	end
	
	if (Ns.Profile.EventRaid and instanceType == "raids" and not Ns.CharPanel and not Ns.Profile.Stats.EventCombat) then
		Ns.Profile.HideHUD = false	
		SinStatsFrame.HUD:Show()
		Ns:InitialiseProfile(Ns.Profile)
	elseif (Ns.Profile.EventRaid and not inInstance and not Ns.CharPanel and not Ns.Profile.Stats.EventCombat) then
		Ns.Profile.HideHUD = true	
		SinStatsFrame.HUD:Hide()
		Ns:InitialiseProfile(Ns.Profile)
	elseif (not Ns.Profile.EventRaid and Ns.eventChecked and instanceType == "raids" and not Ns.CharPanel and not Ns.Profile.Stats.EventCombat) then
		Ns.Profile.HideHUD = true	
		SinStatsFrame.HUD:Hide()
		Ns:InitialiseProfile(Ns.Profile)
	end
	
	if (Ns.Profile.Stats.EventPvP and instanceType == "pvp" and not Ns.CharPanel and not Ns.Profile.Stats.EventCombat ) then
		Ns.Profile.HideHUD = false	
		SinStatsFrame.HUD:Show()
		Ns:InitialiseProfile(Ns.Profile)
	elseif (Ns.Profile.Stats.EventPvP and not inInstance and not Ns.CharPanel and not Ns.Profile.Stats.EventCombat) then
		Ns.Profile.HideHUD = true	
		SinStatsFrame.HUD:Hide()
		Ns:InitialiseProfile(Ns.Profile)
	elseif (not Ns.Profile.Stats.EventPvP and Ns.eventChecked and instanceType == "pvp" and not Ns.CharPanel and not Ns.Profile.Stats.EventCombat) then
		Ns.Profile.HideHUD = true	
		SinStatsFrame.HUD:Hide()
		Ns:InitialiseProfile(Ns.Profile)
	end	

	if (Ns.Profile.Stats.EventArena and instanceType == "arena" and not Ns.CharPanel and not Ns.Profile.Stats.EventCombat) then
		Ns.Profile.HideHUD = false	
		SinStatsFrame.HUD:Show()
		Ns:InitialiseProfile(Ns.Profile)
	elseif (Ns.Profile.Stats.EventArena and not inInstance and not Ns.CharPanel and not Ns.Profile.Stats.EventCombat) then
		Ns.Profile.HideHUD = true	
		SinStatsFrame.HUD:Hide()
		Ns:InitialiseProfile(Ns.Profile)
	elseif (not Ns.Profile.Stats.EventArena and Ns.eventChecked and instanceType == "arena" and not Ns.CharPanel and not Ns.Profile.Stats.EventCombat) then
		Ns.Profile.HideHUD = true	
		SinStatsFrame.HUD:Hide()
		Ns:InitialiseProfile(Ns.Profile)
	end
	
	if (Ns.Profile.Stats.EventWorld and instanceType == "none" and not Ns.CharPanel and not Ns.Profile.Stats.EventCombat) then
		Ns.Profile.HideHUD = false	
		SinStatsFrame.HUD:Show()
		Ns:InitialiseProfile(Ns.Profile)
	end
	
	if (Ns.Profile.Stats.EventCombat and not Ns.Profile.Stats.EventWorld and not Ns.Profile.Stats.EventArena and not Ns.Profile.Stats.EventPvP and not Ns.Profile.EventRaid and not Ns.Profile.Stats.EventDungeon) and (not UnitAffectingCombat('player') or not InCombatLockdown()) then
		Ns.Profile.HideHUD = true	
		SinStatsFrame.HUD:Hide()
		Ns:InitialiseProfile(Ns.Profile)
	end
end
elseif event == "PLAYER_REGEN_DISABLED" then
	if Ns.Profile.Stats.EventEnable then 
		if (Ns.Profile.Stats.EventCombat and not Ns.CharPanel and (UnitAffectingCombat('player') or InCombatLockdown())) then
			Ns.Profile.HideHUD = false	
			SinStatsFrame.HUD:Show()
			Ns:InitialiseProfile(Ns.Profile)
		end
	end
elseif event == "PLAYER_REGEN_ENABLED" then
	if Ns.Profile.Stats.EventEnable then 
		if (Ns.Profile.Stats.EventCombat and not Ns.CharPanel and (not UnitAffectingCombat('player') or not InCombatLockdown())) then
			Ns.Profile.HideHUD = true	
			SinStatsFrame.HUD:Hide()
			Ns:InitialiseProfile(Ns.Profile)
		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")