// Custom Invincibility / Grow Jingle by Pontrol

// =======================
// CHARACTER THEME TABLE
// =======================
local characterThemes = {

	eggmannega = { inv = "tunes NEGA", grow = "tunes NEGA" },
	zazz = { inv = "tunes ZAZZ", grow = "tunes ZAZZ" },
	eggpawn = { inv = "tunes PAWN", grow = "tunes PAWN" },
	merlina = { inv = "tunes MERLINA", grow = "tunes MERLINA" },
	wreckitralph = { inv = "tunes RALPH", grow = "tunes RALPH" },
	vpnine = { inv = "tunes VPNINE", grow = "tunes VPNINE" },
	witchcart = { inv = "tunes WITCHC", grow = "tunes WITCHC" }
}

// =======================
// GLOBAL SETTINGS
// =======================
local defaulttheme = "tunes -default"
local tuned = 0   -- 0 = default, 1 = inv, 2 = grow

// =======================
// MAIN LOGIC
// =======================
addHook("ThinkFrame", function()
	if not consoleplayer or not consoleplayer.mo then return end

	if consoleplayer.exiting then
		if tuned ~= 0 then
			COM_BufInsertText(consoleplayer, defaulttheme)
			tuned = 0
		end
		return
	end

	local skin = consoleplayer.mo.skin
	local themes = characterThemes[skin]

	if not themes then
		if tuned ~= 0 then
			COM_BufInsertText(consoleplayer, defaulttheme)
			tuned = 0
		end
		return
	end

	if consoleplayer.invincibilitytimer and consoleplayer.invincibilitytimer > 0 then
		if tuned ~= 1 then
			COM_BufInsertText(consoleplayer, themes.inv)
			tuned = 1
		end
		return
	end

	if consoleplayer.growshrinktimer and consoleplayer.growshrinktimer > 0 then
		if tuned ~= 2 then
			COM_BufInsertText(consoleplayer, themes.grow)
			tuned = 2
		end
		return
	end

	if tuned ~= 0 then
		COM_BufInsertText(consoleplayer, defaulttheme)
		tuned = 0
	end
end)

// =======================
// GAME QUIT CLEANUP
// =======================
addHook("GameQuit", function()
	if tuned ~= 0 and consoleplayer then
		COM_BufInsertText(consoleplayer, defaulttheme)
		tuned = 0
	end
end)
