TirePressureEvent = {}
local TirePressureEvent_mt = Class(TirePressureEvent, Event)
InitEventClass(TirePressureEvent, "TirePressureEvent")

function TirePressureEvent.emptyNew()
    return Event.new(TirePressureEvent_mt)
end

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

function TirePressureEvent:readStream(streamId, connection)
    self.vehicle = NetworkUtil.readNodeObject(streamId)
    self.pressure = streamReadFloat32(streamId)
    self.wheelIndex = streamReadUInt8(streamId)
    self:run(connection)
end

function TirePressureEvent:writeStream(streamId, connection)
    NetworkUtil.writeNodeObject(streamId, self.vehicle)
    streamWriteFloat32(streamId, self.pressure)
    streamWriteUInt8(streamId, self.wheelIndex or 0)
end

function TirePressureEvent:run(connection)
    if TirePressureSystem == nil or self.vehicle == nil then return end
    TirePressureSystem:setVehicleTargetLocal(self.vehicle, self.pressure, self.wheelIndex)
    if connection ~= nil and not connection:getIsServer() and g_server ~= nil then
        g_server:broadcastEvent(TirePressureEvent.new(self.vehicle, self.pressure, self.wheelIndex), nil, connection, self.vehicle)
    end
end

function TirePressureEvent.send(vehicle, pressure, wheelIndex)
    if TirePressureSystem ~= nil then
        TirePressureSystem:setVehicleTargetLocal(vehicle, pressure, wheelIndex)
    end

    if g_server ~= nil then
        g_server:broadcastEvent(TirePressureEvent.new(vehicle, pressure, wheelIndex), nil, nil, vehicle)
    elseif g_client ~= nil and g_client:getServerConnection() ~= nil then
        g_client:getServerConnection():sendEvent(TirePressureEvent.new(vehicle, pressure, wheelIndex))
    end
end
