PunctureSoundEvent = {}
local PunctureSoundEvent_mt = Class(PunctureSoundEvent, Event)
InitEventClass(PunctureSoundEvent, "MudSystemPhysics.PunctureSoundEvent")

function PunctureSoundEvent.emptyNew()
    return Event.new(PunctureSoundEvent_mt)
end

function PunctureSoundEvent.new(vehicle, wheelIndex, soundId)
    local self = PunctureSoundEvent.emptyNew()
    self.vehicle = vehicle
    self.wheelIndex = wheelIndex or 0
    self.soundId = soundId or 0
    return self
end

function PunctureSoundEvent:readStream(streamId, connection)
    self.vehicle = NetworkUtil.readNodeObject(streamId)
    self.wheelIndex = streamReadUInt8(streamId)
    self.soundId = streamReadUInt8(streamId)
    self:run(connection)
end

function PunctureSoundEvent:writeStream(streamId, connection)
    NetworkUtil.writeNodeObject(streamId, self.vehicle)
    streamWriteUInt8(streamId, self.wheelIndex or 0)
    streamWriteUInt8(streamId, self.soundId or 0)
end

function PunctureSoundEvent:run(connection)
    if PunctureSystem == nil or self.vehicle == nil then return end
    if connection ~= nil and not connection:getIsServer() and g_server ~= nil then
        PunctureSystem:handleClientSoundRequest(
            connection, self.vehicle, self.wheelIndex, self.soundId
        )
        return
    end
    PunctureSystem:applySoundAction(self.vehicle, self.wheelIndex, self.soundId)
end

function PunctureSoundEvent.send(vehicle, wheelIndex, soundId)
    if PunctureSystem == nil or vehicle == nil then return end
    PunctureSystem:applySoundAction(vehicle, wheelIndex, soundId)
    local event = PunctureSoundEvent.new(vehicle, wheelIndex, soundId)
    if g_server ~= nil then
        g_server:broadcastEvent(event, nil, nil, vehicle)
    elseif g_client ~= nil and g_client:getServerConnection() ~= nil then
        g_client:getServerConnection():sendEvent(event)
    end
end
