Events.OnGameStart.Add(function()
	require "RadioCom/RadioWindowModules/RWMMedia"
	require "RadioCom/RadioWindowModules/RWMPower"
	require "RadioCom/RadioWindowModules/RWMVolume"
	require "ISUI/ISButton"

	--Backup original functions
	local override = {
		RWMMedia = { addMediaAux = RWMMedia.addMediaAux, removeMedia = RWMMedia.removeMedia },
		RWMPower = { addBattery = RWMPower.addBattery, removeBattery = RWMPower.removeBattery },
		RWMVolume = { addHeadphone = RWMVolume.addHeadphone, removeHeadphone = RWMVolume.removeHeadphone },
		ISButton = { onMouseUp = ISButton.onMouseUp }
	}
	local customSoundPlayed = false

	--Play SFX
	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 SFX
	function ISButton:onMouseUp(x, y)
		if self.onclick == RWMPower.toggleOnOff or self.onclick == RWMMedia.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

	--Play SFX on media insertion
	function RWMMedia:addMediaAux(item)
		local mediaType, vehicleInside = self.deviceData:getMediaType(), self.player:getVehicle()
		local deviceName = self.deviceData:getDeviceName()
		local isVehiclePart = (self.deviceType == "VehiclePart")
		local worldSound
		if mediaType == 0 then
			if (deviceName == "Premium Technologies Radio" or deviceName == "ValuTech Radio" or deviceName == "Makeshift Radio" or deviceName == "Premium Technologies Ham Radio" or deviceName == "US Army Ham Radio" or deviceName == "Makeshift Ham Radio" ) and isVehiclePart then
				worldSound = "SSsfx_InsertCDVehicle"
			else
				worldSound = "SSsfx_InsertCD"
			end
		elseif mediaType == 1 then
			worldSound = "SSsfx_InsertVHS"
		end
		if worldSound then playSound(nil, worldSound, self.player) end
		override.RWMMedia.addMediaAux(self, item)
	end

	--Play SFX on media removal
	function RWMMedia:removeMedia()
		local mediaType, vehicleInside = self.deviceData:getMediaType(), self.player:getVehicle()
		local deviceName = self.deviceData:getDeviceName()
		local isVehiclePart = (self.deviceType == "VehiclePart")
		local worldSound
		if mediaType == 0 then
			if (deviceName == "Premium Technologies Radio" or deviceName == "ValuTech Radio" or deviceName == "Makeshift Radio" or deviceName == "Premium Technologies Ham Radio" or deviceName == "US Army Ham Radio" or deviceName == "Makeshift Ham Radio" ) and isVehiclePart then
				worldSound = "SSsfx_RemoveCDVehicle"
			else
				worldSound = "SSsfx_RemoveCD"
			end
		elseif mediaType == 1 then
			worldSound = "SSsfx_EjectVHS"
		end
		if worldSound then playSound(nil, worldSound, self.player) end
		override.RWMMedia.removeMedia(self)
	end

	--Play SFX on battery insertion/removal
	function RWMPower: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.RWMPower.addBattery(self, { highestDeltaItem })
	end

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

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

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