Events.OnGameStart.Add(function()
	require "RadioCom/RadioWindowModules/RWMPower"
	require "RadioCom/RadioWindowModules/RWMGridPower"
	require "RadioCom/RadioWindowModules/RWMMedia"
	require "RadioCom/RadioWindowModules/RWMVolume"
	require "ISUI/ISButton"
	
	local override = {
		ISButton = { onMouseUp = ISButton.onMouseUp },
		RWMVolume = { onSpeakerButton = RWMVolume.onSpeakerButton } }
	local customSoundPlayed = false

	--Play SFX based on device and action
	local function playDeviceSFX(player, deviceData, action, isVehicle)
		local deviceName = deviceData:getDeviceName()
		local sounds = {
			["ValuTech Radio"] = { on = "SSsfx_TurnOnRadio", off = "SSsfx_TurnOffRadio", play = "SSsfx_PlayRadio", stop = "SSsfx_StopRadio", mute = "SSsfx_MuteRadioVolume", unmute = "SSsfx_UnmuteRadioVolume" },
			["Premium Technologies Radio"] = { on = "SSsfx_TurnOnRadio", off = "SSsfx_TurnOffRadio", play = "SSsfx_PlayRadio", stop = "SSsfx_StopRadio", mute = "SSsfx_MuteRadioVolume", unmute = "SSsfx_UnmuteRadioVolume" },
			["Makeshift Radio"] = { on = "SSsfx_TurnOnRadio", off = "SSsfx_TurnOffRadio", play = "SSsfx_PlayRadio", stop = "SSsfx_StopRadio", mute = "SSsfx_MuteRadioVolume", unmute = "SSsfx_UnmuteRadioVolume" },
			["ValuTech PortaDisc"] = { on = "SSsfx_TurnOnPortaDisc", off = "SSsfx_TurnOffPortaDisc", play = "SSsfx_TurnOnPortaDisc", stop = "SSsfx_TurnOffPortaDisc", mute = "SSsfx_MutePortaDiscVolume", unmute = "SSsfx_UnmutePortaDiscVolume" },
			["Premium Technologies Ham Radio"] = { on = "SSsfx_TurnOnHamRadio", off = "SSsfx_TurnOffHamRadio", play = "SSsfx_PlayHamRadio", stop = "SSsfx_StopHamRadio", mute = "SSsfx_MuteHamRadioVolume", unmute = "SSsfx_UnmuteHamRadioVolume" },
			["US Army Ham Radio"] = { on = "SSsfx_TurnOnHamRadio", off = "SSsfx_TurnOffHamRadio", play = "SSsfx_PlayHamRadio", stop = "SSsfx_StopHamRadio", mute = "SSsfx_MuteHamRadioVolume", unmute = "SSsfx_UnmuteHamRadioVolume" },
			["Makeshift Ham Radio"] = { on = "SSsfx_TurnOnHamRadio", off = "SSsfx_TurnOffHamRadio", play = "SSsfx_PlayHamRadio", stop = "SSsfx_StopHamRadio", mute = "SSsfx_MuteHamRadioVolume", unmute = "SSsfx_UnmuteHamRadioVolume" },
			["Antique Television"] = { on = "SSsfx_TurnOnAntiqueTV", off = "SSsfx_TurnOffAntiqueTV", play = "SSsfx_PlayAntiqueTV", stop = "SSsfx_StopAntiqueTV", mute = "SSsfx_MuteAntiqueTV", unmute = "SSsfx_UnmuteAntiqueTV" },
			["ValuTech Television"] = { on = "SSsfx_TurnOnValuTechTV", off = "SSsfx_TurnOffValuTechTV", play = "SSsfx_TurnOnValuTechTV", stop = "SSsfx_TurnOffValuTechTV", mute = "SSsfx_MuteValuTechTV", unmute = "SSsfx_UnmuteValuTechTV" },
			["Premium Technologies Television"] = { on = "SSsfx_TurnOnValuTechTV", off = "SSsfx_TurnOffValuTechTV", play = "SSsfx_TurnOnValuTechTV", stop = "SSsfx_TurnOffValuTechTV", mute = "SSsfx_MuteValuTechTV", unmute = "SSsfx_UnmuteValuTechTV" },
			["Toys-R-Mine Walkie Talkie"] = { on = "SSsfx_TurnOnWalkieTalkie", off = "SSsfx_TurnOffWalkieTalkie", mute = "SSsfx_MutePortaDiscVolume", unmute = "SSsfx_UnmutePortaDiscVolume" },
			["ValuTech Walkie Talkie"] = { on = "SSsfx_TurnOnWalkieTalkie", off = "SSsfx_TurnOffWalkieTalkie", mute = "SSsfx_MutePortaDiscVolume", unmute = "SSsfx_UnmutePortaDiscVolume" },
			["Premium Technologies Walkie Talkie"] = { on = "SSsfx_TurnOnWalkieTalkie", off = "SSsfx_TurnOffWalkieTalkie", mute = "SSsfx_MutePortaDiscVolume", unmute = "SSsfx_UnmutePortaDiscVolume" },
			["Tactical Walkie Talkie"] = { on = "SSsfx_TurnOnWalkieTalkie", off = "SSsfx_TurnOffWalkieTalkie", mute = "SSsfx_MutePortaDiscVolume", unmute = "SSsfx_UnmutePortaDiscVolume" },
			["US Army Walkie Talkie"] = { on = "SSsfx_TurnOnWalkieTalkie", off = "SSsfx_TurnOffWalkieTalkie", mute = "SSsfx_MutePortaDiscVolume", unmute = "SSsfx_UnmutePortaDiscVolume" },
			["Makeshift Walkie Talkie"] = { on = "SSsfx_TurnOnWalkieTalkie", off = "SSsfx_TurnOffWalkieTalkie", mute = "SSsfx_MutePortaDiscVolume", unmute = "SSsfx_UnmutePortaDiscVolume" }
		}
		--Vehicle device SFX
		if isVehicle then
			sounds["ValuTech Radio"].on = "SSsfx_TurnOnVehicleRadio"
			sounds["ValuTech Radio"].off = "SSsfx_TurnOffVehicleRadio"
			sounds["ValuTech Radio"].play = "SSsfx_PlayVehicleRadio"
			sounds["ValuTech Radio"].stop = "SSsfx_StopVehicleRadio"
			sounds["ValuTech Radio"].mute = "SSsfx_MuteVehicleRadioVolume"
			sounds["ValuTech Radio"].unmute = "SSsfx_UnmuteVehicleRadioVolume"
			sounds["Premium Technologies Radio"].on = "SSsfx_TurnOnVehicleRadio"
			sounds["Premium Technologies Radio"].off = "SSsfx_TurnOffVehicleRadio"
			sounds["Premium Technologies Radio"].play = "SSsfx_PlayVehicleRadio"
			sounds["Premium Technologies Radio"].stop = "SSsfx_StopVehicleRadio"
			sounds["Premium Technologies Radio"].mute = "SSsfx_MuteVehicleRadioVolume"
			sounds["Premium Technologies Radio"].unmute = "SSsfx_UnmuteVehicleRadioVolume"
			sounds["Makeshift Radio"].on = "SSsfx_TurnOnVehicleRadio"
			sounds["Makeshift Radio"].off = "SSsfx_TurnOffVehicleRadio"
			sounds["Makeshift Radio"].play = "SSsfx_PlayVehicleRadio"
			sounds["Makeshift Radio"].stop = "SSsfx_StopVehicleRadio"
			sounds["Makeshift Radio"].mute = "SSsfx_MuteVehicleRadioVolume"
			sounds["Makeshift Radio"].unmute = "SSsfx_UnmuteVehicleRadioVolume"
			sounds["Premium Technologies Ham Radio"].on = "SSsfx_TurnOnHamRadio"
			sounds["Premium Technologies Ham Radio"].off = "SSsfx_TurnOffHamRadio"
			sounds["Premium Technologies Ham Radio"].play = "SSsfx_PlayHamRadio"
			sounds["Premium Technologies Ham Radio"].stop = "SSsfx_StopHamRadio"
			sounds["Premium Technologies Ham Radio"].mute = "SSsfx_MuteHamRadioVolume"
			sounds["Premium Technologies Ham Radio"].unmute = "SSsfx_UnmuteHamRadioVolume"
			sounds["US Army Ham Radio"].on = "SSsfx_TurnOnHamRadio"
			sounds["US Army Ham Radio"].off = "SSsfx_TurnOffHamRadio"
			sounds["US Army Ham Radio"].play = "SSsfx_PlayHamRadio"
			sounds["US Army Ham Radio"].stop = "SSsfx_StopHamRadio"
			sounds["US Army Ham Radio"].mute = "SSsfx_MuteHamRadioVolume"
			sounds["US Army Ham Radio"].unmute = "SSsfx_UnmuteHamRadioVolume"
			sounds["Makeshift Ham Radio"].on = "SSsfx_TurnOnHamRadio"
			sounds["Makeshift Ham Radio"].off = "SSsfx_TurnOffHamRadio"
			sounds["Makeshift Ham Radio"].play = "SSsfx_PlayHamRadio"
			sounds["Makeshift Ham Radio"].stop = "SSsfx_StopHamRadio"
			sounds["Makeshift Ham Radio"].mute = "SSsfx_MuteHamRadioVolume"
			sounds["Makeshift Ham Radio"].unmute = "SSsfx_UnmuteHamRadioVolume"
		end
		--Trigger sound if it exists for the action
		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 == RWMPower.toggleOnOff or self.onclick == RWMGridPower.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
	
	--Power and media actions
	local function toggleDevice(self, actionType, soundAction, timedAction)
		if self:doWalkTo() then
			local isVehicleRadio = (self.deviceType == "VehiclePart")
			playDeviceSFX(self.player, self.deviceData, soundAction, isVehicleRadio)
			ISTimedActionQueue.add(ISRadioAction:new(timedAction, self.player, self.device))
		end
	end

	function RWMPower:toggleOnOff()
		toggleDevice(self, "toggleOnOff", self.deviceData:getIsTurnedOn() and "off" or "on", "ToggleOnOff")
	end

	function RWMGridPower:toggleOnOff()
		toggleDevice(self, "toggleOnOff", self.deviceData:getIsTurnedOn() and "off" or "on", "ToggleOnOff")
	end

	function RWMMedia:togglePlayMedia()
		toggleDevice(self, "togglePlayMedia", self.deviceData:isPlayingMedia() and "stop" or "play", "TogglePlayMedia")
	end
	
	--Volume mute and unmute actions
	function RWMVolume:onSpeakerButton(_ismute)
		local deviceData = self.deviceData
		local isVehicleRadio = (self.deviceType == "VehiclePart")
		playDeviceSFX(self.player, deviceData, _ismute and "mute" or "unmute", isVehicleRadio)
		override.RWMVolume.onSpeakerButton(self, _ismute)
	end
end )