Events.OnGameStart.Add(function()
	if not getActivatedMods():contains("truemusic") then return end
	require "RadioCom/RadioWindowModules/TCRWMMedia"
	require "RadioCom/RadioWindowModules/TCRWMPower"
	require "RadioCom/RadioWindowModules/TCRWMVolume"
	require "ISUI/ISButton"

	--Backup original functions
	local override = {
		TCRWMMedia = { addMediaAux = TCRWMMedia.addMediaAux, removeMedia = TCRWMMedia.removeMedia },
		TCRWMPower = { addBattery = TCRWMPower.addBattery, removeBattery = TCRWMPower.removeBattery },
		TCRWMVolume = { addHeadphone = TCRWMVolume.addHeadphone, removeHeadphone = TCRWMVolume.removeHeadphone },
		ISButton = { onMouseUp = ISButton.onMouseUp }
	}
	local customSoundPlayed = false

	--Play SFX for media actions with local and world sound handling
	local function playSound(localSound, worldSound, player, square)
		local soundEmitter = player:getEmitter()
		if soundEmitter then soundEmitter:playSound(worldSound) end
		customSoundPlayed = true
	end

	--Disable UI SFX for devices with custom sounds
	function ISButton:onMouseUp(x, y)
		if self.onclick == TCRWMPower.toggleOnOff or self.onclick == TCRWMGridPower.toggleOnOff or self.onclick == TCRWMMedia.togglePlayMedia then
			local originalSound = self.sounds.activate
			self.sounds.activate = nil
			override.ISButton.onMouseUp(self, x, y)
			if not customSoundPlayed then
				getSoundManager():playUISound(originalSound)
			end
			customSoundPlayed = false
			self.sounds.activate = originalSound
		else
			override.ISButton.onMouseUp(self, x, y)
		end
	end

	--Override functions to play SFX when adding/removing media
	function TCRWMMedia:addMediaAux(item)
		local mediaType, vehicleInside = self.device:getDeviceData():getMediaType(), self.player:getVehicle()
		local isPortableCassettePlayer = (self.deviceData:getDeviceName() == "ValuTech Boombox" or self.deviceData:getDeviceName() == "ValuTech Walkman")
		local worldSound
		if mediaType == 0 then
			if vehicleInside and not isPortableCassettePlayer then
				worldSound = "SSsfx_InsertCassetteVehicle"
			else
				worldSound = "SSsfx_InsertCassette"
			end
		elseif mediaType == 1 then
			worldSound = "SSsfx_PlaceRecord"
		end
		if worldSound then playSound(nil, worldSound, self.player) end
		override.TCRWMMedia.addMediaAux(self, item)
	end

	function TCRWMMedia:removeMedia()
		local mediaType, vehicleInside = self.device:getDeviceData():getMediaType(), self.player:getVehicle()
		local isPortableCassettePlayer = (self.deviceData:getDeviceName() == "ValuTech Boombox" or self.deviceData:getDeviceName() == "ValuTech Walkman")
		local localSound, worldSound

		if mediaType == 0 then
			if vehicleInside and not isPortableCassettePlayer then
				worldSound = "SSsfx_EjectCassetteVehicle"
			else
				worldSound = "SSsfx_EjectCassette"
			end
		elseif mediaType == 1 then
			worldSound = "SSsfx_RemoveRecord"
		end
		if worldSound then playSound(nil, worldSound, self.player) end
		override.TCRWMMedia.removeMedia(self)
	end

	--Override functions to play SFX on battery insertion/removal
	function TCRWMPower:addBattery(_items)
		local highestDeltaItem = nil
		for _, item in ipairs(_items) do
			if not highestDeltaItem or item:getDelta() > highestDeltaItem:getDelta() then
				highestDeltaItem = item
			end
		end

		if highestDeltaItem and self:doWalkTo() then
			playSound(nil, "SSsfx_InsertBattery", self.player)
		end
		override.TCRWMPower.addBattery(self, { highestDeltaItem })
	end

	function TCRWMPower:removeBattery()
		if self:doWalkTo() then
			playSound(nil, "SSsfx_RemoveBattery", self.player)
		end
		override.TCRWMPower.removeBattery(self)
	end

	--Override functions to play SFX on headphone insertion/removal
	function TCRWMVolume:addHeadphone(_items)
		override.TCRWMVolume.addHeadphone(self, _items)
		if _items and #_items > 0 then
			getSoundManager():playUISound("SSsfx_InsertHeadphones")
			customSoundPlayed = true
		end
	end

	function TCRWMVolume:removeHeadphone()
		override.TCRWMVolume.removeHeadphone(self)
		getSoundManager():playUISound("SSsfx_RemoveHeadphones")
		customSoundPlayed = true
	end
end )