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

	--Backup original functions
	local override = {
		ISButton = { onMouseUp = ISButton.onMouseUp },
		TCRWMPower = { toggleOnOff = TCRWMPower.toggleOnOff },
		TCRWMGridPower = { toggleOnOff = TCRWMGridPower.toggleOnOff },
		TCRWMMedia = { togglePlayMedia = TCRWMMedia.togglePlayMedia },
		TCRWMVolume = { onSpeakerButton = TCRWMVolume.onSpeakerButton } }
	local customSoundPlayed = false

	--Play toggle sounds for different devices and vehicle parts
	local function playDeviceSFX(player, deviceData, action, isVehicle)
		local deviceName = deviceData:getDeviceName()
		local sounds = {
			["ValuTech Boombox"] = { on = "SSsfx_TurnOnRadio", off = "SSsfx_TurnOffRadio", play = "SSsfx_PlayRadio", stop = "SSsfx_StopRadio", mute = "SSsfx_MuteRadioVolume", unmute = "SSsfx_UnmuteRadioVolume" },
			["ValuTech Walkman"] = { on = "SSsfx_TurnOnRadio", off = "SSsfx_TurnOffRadio", play = "SSsfx_PlayRadio", stop = "SSsfx_StopRadio", mute = "SSsfx_MutePortaDiscVolume", unmute = "SSsfx_UnmutePortaDiscVolume" },
			["ValuTech Record Player"] = { on = "SSsfx_TurnOnRecordPlayer", off = "SSsfx_TurnOffRecordPlayer", play = "SSsfx_PlayRecordPlayer", stop = "SSsfx_StopRecordPlayer", mute = "SSsfx_MuteHamRadioVolume", unmute = "SSsfx_UnmuteHamRadioVolume" },
			["ValuTech Radio"] = { on = isVehicle and "SSsfx_TurnOnVehicleRadio" or "SSsfx_TurnOnRadio", off = isVehicle and "SSsfx_TurnOffVehicleRadio" or "SSsfx_TurnOffRadio", play = isVehicle and "SSsfx_PlayVehicleRadio" or "SSsfx_PlayRadio", stop = isVehicle and "SSsfx_StopVehicleRadio" or "SSsfx_StopRadio", mute = isVehicle and "SSsfx_MuteVehicleRadioVolume" or "SSsfx_MuteRadioVolume", unmute = isVehicle and "SSsfx_UnmuteVehicleRadioVolume" or "SSsfx_UnmuteRadioVolume" },
			["Premium Technologies Radio"] = { on = isVehicle and "SSsfx_TurnOnVehicleRadio" or "SSsfx_TurnOnRadio", off = isVehicle and "SSsfx_TurnOffVehicleRadio" or "SSsfx_TurnOffRadio", play = isVehicle and "SSsfx_PlayVehicleRadio" or "SSsfx_PlayRadio", stop = isVehicle and "SSsfx_StopVehicleRadio" or "SSsfx_StopRadio", mute = isVehicle and "SSsfx_MuteVehicleRadioVolume" or "SSsfx_MuteRadioVolume", unmute = isVehicle and "SSsfx_UnmuteVehicleRadioVolume" or "SSsfx_UnmuteRadioVolume" },
			["Makeshift Radio"] = { on = isVehicle and "SSsfx_TurnOnVehicleRadio" or "SSsfx_TurnOnRadio", off = isVehicle and "SSsfx_TurnOffVehicleRadio" or "SSsfx_TurnOffRadio", play = isVehicle and "SSsfx_PlayVehicleRadio" or "SSsfx_PlayRadio", stop = isVehicle and "SSsfx_StopVehicleRadio" or "SSsfx_StopRadio", mute = isVehicle and "SSsfx_MuteVehicleRadioVolume" or "SSsfx_MuteRadioVolume", unmute = isVehicle and "SSsfx_UnmuteVehicleRadioVolume" or "SSsfx_UnmuteRadioVolume" },
			["Premium Technologies Ham Radio"] = { on = isVehicle and "SSsfx_TurnOnHamRadio" or "SSsfx_TurnOnHamRadio", off = isVehicle and "SSsfx_TurnOffHamRadio" or "SSsfx_TurnOffHamRadio", play = isVehicle and "SSsfx_PlayHamRadio" or "SSsfx_PlayHamRadio", stop = isVehicle and "SSsfx_StopHamRadio" or "SSsfx_StopHamRadio", mute = isVehicle and "SSsfx_MuteHamRadioVolume" or "SSsfx_MuteHamRadioVolume", unmute = isVehicle and "SSsfx_UnmuteHamRadioVolume" or "SSsfx_UnmuteHamRadioVolume" },
			["US Army Ham Radio"] = { on = isVehicle and "SSsfx_TurnOnHamRadio" or "SSsfx_TurnOnHamRadio", off = isVehicle and "SSsfx_TurnOffHamRadio" or "SSsfx_TurnOffHamRadio", play = isVehicle and "SSsfx_PlayHamRadio" or "SSsfx_PlayHamRadio", stop = isVehicle and "SSsfx_StopHamRadio" or "SSsfx_StopHamRadio", mute = isVehicle and "SSsfx_MuteHamRadioVolume" or "SSsfx_MuteHamRadioVolume", unmute = isVehicle and "SSsfx_UnmuteHamRadioVolume" or "SSsfx_UnmuteHamRadioVolume" },
			["Makeshift Ham Radio"] = { on = isVehicle and "SSsfx_TurnOnHamRadio" or "SSsfx_TurnOnHamRadio", off = isVehicle and "SSsfx_TurnOffHamRadio" or "SSsfx_TurnOffHamRadio", play = isVehicle and "SSsfx_PlayHamRadio" or "SSsfx_PlayHamRadio", stop = isVehicle and "SSsfx_StopHamRadio" or "SSsfx_StopHamRadio", mute = isVehicle and "SSsfx_MuteHamRadioVolume" or "SSsfx_MuteHamRadioVolume", unmute = isVehicle and "SSsfx_UnmuteHamRadioVolume" or "SSsfx_UnmuteHamRadioVolume" } }
		local sound = sounds[deviceName] and sounds[deviceName][action]
		if sound then
			customSoundPlayed = true
			player:getEmitter():playSound(sound)
		end
	end

	--Disable default UI SFX for devices with custom SFX
	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 toggleOnOff for boombox and walkman with custom SFX
	function TCRWMPower:toggleOnOff()
		if self:doWalkTo() then
			local deviceData = self.deviceData
			local deviceName = deviceData:getDeviceName()
			if deviceName == "ValuTech Boombox" or deviceName == "ValuTech Walkman" then
				playDeviceSFX(self.player, deviceData, deviceData:getIsTurnedOn() and "off" or "on", false)
			end
			ISTimedActionQueue.add(ISTCBoomboxAction:new("ToggleOnOff", self.player, self.device))
		end
	end

	--Override toggleOnOff for record player and vehicle radios with custom SFX
	function TCRWMGridPower:toggleOnOff()
		if self:doWalkTo() then
			local deviceData = self.deviceData
			local deviceName = deviceData:getDeviceName()
			local isVehicleRadio = (self.deviceType == "VehiclePart")
			if deviceName == "ValuTech Record Player" then
				playDeviceSFX(self.player, deviceData, deviceData:getIsTurnedOn() and "off" or "on", false)
			elseif isVehicleRadio and (deviceName == "ValuTech Radio" or deviceName == "Premium Technologies Radio" or deviceName == "Makeshift Radio" or deviceName == "Premium Technologies Ham Radio" or deviceName == "US Army Ham Radio" or deviceName == "Makeshift Ham Radio") then
				playDeviceSFX(self.player, deviceData, deviceData:getIsTurnedOn() and "off" or "on", true)
			end
			ISTimedActionQueue.add(ISTCBoomboxAction:new("ToggleOnOff", self.player, self.device))
		end
	end

	--Override togglePlayMedia for different media devices with custom SFX
	function TCRWMMedia:togglePlayMedia()
		if self:doWalkTo() then
			local deviceData = self.deviceData
			local deviceName = deviceData:getDeviceName()
			local isPlaying = self.device:getModData().tcmusic.isPlaying
			local isVehicleRadio = (self.deviceType == "VehiclePart")
			if deviceName == "ValuTech Boombox" or deviceName == "ValuTech Walkman" or deviceName == "ValuTech Record Player" then
				playDeviceSFX(self.player, deviceData, isPlaying and "stop" or "play", false)
			elseif isVehicleRadio and (deviceName == "ValuTech Radio" or deviceName == "Premium Technologies Radio" or deviceName == "Makeshift Radio" or deviceName == "Premium Technologies Ham Radio" or deviceName == "US Army Ham Radio" or deviceName == "Makeshift Ham Radio") then
				playDeviceSFX(self.player, deviceData, isPlaying and "stop" or "play", true)
			end
			ISTimedActionQueue.add(ISTCBoomboxAction:new("TogglePlayMedia", self.player, self.device))
		end
	end
	
	--Volume mute and unmute actions
	function TCRWMVolume:onSpeakerButton(_ismute)
		local deviceData = self.deviceData
		local isVehicleRadio = (self.deviceType == "VehiclePart")
		playDeviceSFX(self.player, deviceData, _ismute and "mute" or "unmute", isVehicleRadio)
		override.TCRWMVolume.onSpeakerButton(self, _ismute)
	end
end )