-- =========================================================
-- Speed Camera BR — Counter Sync Event (Server -> Clients)
-- =========================================================
-- Sincroniza a posição do contador global de reincidência.

SC_CounterSyncEvent = {}
SC_CounterSyncEvent_mt = Class(SC_CounterSyncEvent, Event)

InitEventClass(SC_CounterSyncEvent, "SC_CounterSyncEvent")

function SC_CounterSyncEvent.emptyNew()
    return Event.new(SC_CounterSyncEvent_mt)
end

function SC_CounterSyncEvent.new()
    return SC_CounterSyncEvent.emptyNew()
end

function SC_CounterSyncEvent:writeStream(streamId, connection)
    local pos = 1
    if SpeedCameraCounter ~= nil then pos = SpeedCameraCounter.getPosition() end
    streamWriteInt8(streamId, pos)
end

function SC_CounterSyncEvent:readStream(streamId, connection)
    local pos = streamReadInt8(streamId)
    if SpeedCameraCounter ~= nil then
        SpeedCameraCounter.setPosition(pos)
    end
    self:run(connection)
end

function SC_CounterSyncEvent:run(connection)
    -- No cliente, já aplicado no readStream
end

function SC_CounterSyncEvent.broadcast()
    if not g_server then return end
    g_server:broadcastEvent(SC_CounterSyncEvent.new())
end

function SC_CounterSyncEvent.sendToClient(connection)
    if not g_server or connection == nil then return end
    connection:sendEvent(SC_CounterSyncEvent.new())
end
