---@class SectionAutomaticModeEvent

SectionAutomaticModeEvent = {}
local SectionAutomaticModeEvent_mt = Class(SectionAutomaticModeEvent, Event)

InitEventClass(SectionAutomaticModeEvent, "SectionAutomaticModeEvent")

---@return SectionAutomaticModeEvent
function SectionAutomaticModeEvent.emptyNew()
    local self = Event.new(SectionAutomaticModeEvent_mt)
    return self
end

function SectionAutomaticModeEvent.new(vehicle, isAutomatic)
    local self = SectionAutomaticModeEvent.emptyNew()

    self.vehicle = vehicle
    self.isAutomatic = isAutomatic

    return self
end

function SectionAutomaticModeEvent:readStream(streamId, connection)
    self.vehicle = NetworkUtil.readNodeObject(streamId)
    self.isAutomatic = streamReadBool(streamId)
    self:run(connection)
end

function SectionAutomaticModeEvent:writeStream(streamId, connection)
    NetworkUtil.writeNodeObject(streamId, self.vehicle)
    streamWriteBool(streamId, self.isAutomatic)
end

function SectionAutomaticModeEvent:run(connection)
    if not connection:getIsServer() then
        g_server:broadcastEvent(self, false, connection, self.vehicle)
    end

    self.object:setSectionAutomaticMode(self.isAutomatic, true)
end

function SectionAutomaticModeEvent.sendEvent(vehicle, isAutomatic, noEventSend)
    if noEventSend == nil or noEventSend == false then
        if g_server ~= nil then
            g_server:broadcastEvent(SectionAutomaticModeEvent.new(vehicle, isAutomatic), nil, nil, vehicle)
        else
            g_client:getServerConnection():sendEvent(SectionAutomaticModeEvent.new(vehicle, isAutomatic))
        end
    end
end
