OutdoorEntryLightsEvent = {}
local OutdoorEntryLightsEvent_mt = Class(OutdoorEntryLightsEvent, Event)

InitEventClass(OutdoorEntryLightsEvent, "OutdoorEntryLightsEvent")

function OutdoorEntryLightsEvent.emptyNew()
    return Event.new(OutdoorEntryLightsEvent_mt)
end

function OutdoorEntryLightsEvent.new(vehicle, state)
    local self = OutdoorEntryLightsEvent.emptyNew()
    self.vehicle = vehicle
    self.state = state
    return self
end

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

function OutdoorEntryLightsEvent:writeStream(streamId, connection)
    NetworkUtil.writeNodeObject(streamId, self.vehicle)
    streamWriteBool(streamId, self.state or false)
end

function OutdoorEntryLightsEvent:run(connection)
    if self.vehicle ~= nil and self.vehicle.setOutdoorEntryLightsState ~= nil then
        self.vehicle:setOutdoorEntryLightsState(self.state, true)
    end

    if g_server ~= nil and connection ~= nil and not connection:getIsServer() then
        g_server:broadcastEvent(
            OutdoorEntryLightsEvent.new(self.vehicle, self.state),
            nil,
            connection,
            self.vehicle
        )
    end
end

function OutdoorEntryLightsEvent.sendEvent(vehicle, state, noEventSend)
    if noEventSend == true then
        return
    end

    if g_client ~= nil and g_client:getServerConnection() ~= nil then
        g_client:getServerConnection():sendEvent(
            OutdoorEntryLightsEvent.new(vehicle, state)
        )
        return
    end

    if g_server ~= nil then
        g_server:broadcastEvent(
            OutdoorEntryLightsEvent.new(vehicle, state),
            nil,
            nil,
            vehicle
        )
    end
end


-- ########################################################
-- Blinker Sync Event
-- ########################################################

OutdoorEntryLightsBlinkEvent = {}
local OutdoorEntryLightsBlinkEvent_mt = Class(OutdoorEntryLightsBlinkEvent, Event)

InitEventClass(OutdoorEntryLightsBlinkEvent, "OutdoorEntryLightsBlinkEvent")

function OutdoorEntryLightsBlinkEvent.emptyNew()
    return Event.new(OutdoorEntryLightsBlinkEvent_mt)
end

function OutdoorEntryLightsBlinkEvent.new(vehicle)
    local self = OutdoorEntryLightsBlinkEvent.emptyNew()
    self.vehicle = vehicle
    return self
end

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

function OutdoorEntryLightsBlinkEvent:writeStream(streamId, connection)
    NetworkUtil.writeNodeObject(streamId, self.vehicle)
end

function OutdoorEntryLightsBlinkEvent:run(connection)
    if self.vehicle ~= nil and self.vehicle.startOutdoorEntryLightsBlink ~= nil then
        self.vehicle:startOutdoorEntryLightsBlink(true)
    end

    if g_server ~= nil and connection ~= nil and not connection:getIsServer() then
        g_server:broadcastEvent(
            OutdoorEntryLightsBlinkEvent.new(self.vehicle),
            nil,
            connection,
            self.vehicle
        )
    end
end

function OutdoorEntryLightsBlinkEvent.sendEvent(vehicle, noEventSend)
    if noEventSend == true then
        return
    end

    if g_client ~= nil and g_client:getServerConnection() ~= nil then
        g_client:getServerConnection():sendEvent(
            OutdoorEntryLightsBlinkEvent.new(vehicle)
        )
        return
    end

    if g_server ~= nil then
        g_server:broadcastEvent(
            OutdoorEntryLightsBlinkEvent.new(vehicle),
            nil,
            nil,
            vehicle
        )
    end
end


-- ########################################################
-- Horn Sync Event
-- ########################################################

OutdoorEntryLightsHornEvent = {}
local OutdoorEntryLightsHornEvent_mt = Class(OutdoorEntryLightsHornEvent, Event)

InitEventClass(OutdoorEntryLightsHornEvent, "OutdoorEntryLightsHornEvent")

function OutdoorEntryLightsHornEvent.emptyNew()
    return Event.new(OutdoorEntryLightsHornEvent_mt)
end

function OutdoorEntryLightsHornEvent.new(vehicle, times)
    local self = OutdoorEntryLightsHornEvent.emptyNew()
    self.vehicle = vehicle
    self.times = times or 1
    return self
end

function OutdoorEntryLightsHornEvent:readStream(streamId, connection)
    self.vehicle = NetworkUtil.readNodeObject(streamId)
    self.times = streamReadUInt8(streamId)
    self:run(connection)
end

function OutdoorEntryLightsHornEvent:writeStream(streamId, connection)
    NetworkUtil.writeNodeObject(streamId, self.vehicle)
    streamWriteUInt8(streamId, self.times or 1)
end

function OutdoorEntryLightsHornEvent:run(connection)
    if self.vehicle ~= nil and self.vehicle.playOutdoorEntryHorn ~= nil then
        self.vehicle:playOutdoorEntryHorn(self.times, true)
    end

    if g_server ~= nil and connection ~= nil and not connection:getIsServer() then
        g_server:broadcastEvent(
            OutdoorEntryLightsHornEvent.new(self.vehicle, self.times),
            nil,
            connection,
            self.vehicle
        )
    end
end

function OutdoorEntryLightsHornEvent.sendEvent(vehicle, times, noEventSend)
    if noEventSend == true then
        return
    end

    if g_client ~= nil and g_client:getServerConnection() ~= nil then
        g_client:getServerConnection():sendEvent(
            OutdoorEntryLightsHornEvent.new(vehicle, times)
        )
        return
    end

    if g_server ~= nil then
        g_server:broadcastEvent(
            OutdoorEntryLightsHornEvent.new(vehicle, times),
            nil,
            nil,
            vehicle
        )
    end
end


-- ########################################################
-- Proximity Event Client -> Server
-- ########################################################

OutdoorEntryLightsProximityEvent = {}
local OutdoorEntryLightsProximityEvent_mt = Class(OutdoorEntryLightsProximityEvent, Event)

InitEventClass(OutdoorEntryLightsProximityEvent, "OutdoorEntryLightsProximityEvent")

function OutdoorEntryLightsProximityEvent.emptyNew()
    return Event.new(OutdoorEntryLightsProximityEvent_mt)
end

function OutdoorEntryLightsProximityEvent.new(vehicle, inRange)
    local self = OutdoorEntryLightsProximityEvent.emptyNew()
    self.vehicle = vehicle
    self.inRange = inRange
    return self
end

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

function OutdoorEntryLightsProximityEvent:writeStream(streamId, connection)
    NetworkUtil.writeNodeObject(streamId, self.vehicle)
    streamWriteBool(streamId, self.inRange or false)
end

function OutdoorEntryLightsProximityEvent:run(connection)
    if self.vehicle ~= nil and self.vehicle.onOutdoorEntryLightsProximityChanged ~= nil then
        self.vehicle:onOutdoorEntryLightsProximityChanged(connection, self.inRange)
    end
end

function OutdoorEntryLightsProximityEvent.sendEvent(vehicle, inRange)
    if g_client ~= nil and g_client:getServerConnection() ~= nil then
        g_client:getServerConnection():sendEvent(
            OutdoorEntryLightsProximityEvent.new(vehicle, inRange)
        )
        return
    end

    if g_server ~= nil then
        if vehicle ~= nil and vehicle.onOutdoorEntryLightsProximityChanged ~= nil then
            vehicle:onOutdoorEntryLightsProximityChanged("localPlayer", inRange)
        end
    end
end


-- ########################################################
-- Occupied Event Client -> Server
-- ########################################################

OutdoorEntryLightsOccupiedEvent = {}
local OutdoorEntryLightsOccupiedEvent_mt = Class(OutdoorEntryLightsOccupiedEvent, Event)

InitEventClass(OutdoorEntryLightsOccupiedEvent, "OutdoorEntryLightsOccupiedEvent")

function OutdoorEntryLightsOccupiedEvent.emptyNew()
    return Event.new(OutdoorEntryLightsOccupiedEvent_mt)
end

function OutdoorEntryLightsOccupiedEvent.new(vehicle, occupied)
    local self = OutdoorEntryLightsOccupiedEvent.emptyNew()
    self.vehicle = vehicle
    self.occupied = occupied
    return self
end

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

function OutdoorEntryLightsOccupiedEvent:writeStream(streamId, connection)
    NetworkUtil.writeNodeObject(streamId, self.vehicle)
    streamWriteBool(streamId, self.occupied or false)
end

function OutdoorEntryLightsOccupiedEvent:run(connection)
    if self.vehicle ~= nil and self.vehicle.onOutdoorEntryLightsOccupiedChanged ~= nil then
        self.vehicle:onOutdoorEntryLightsOccupiedChanged(connection, self.occupied)
    end
end

function OutdoorEntryLightsOccupiedEvent.sendEvent(vehicle, occupied)
    if g_client ~= nil and g_client:getServerConnection() ~= nil then
        g_client:getServerConnection():sendEvent(
            OutdoorEntryLightsOccupiedEvent.new(vehicle, occupied)
        )
        return
    end

    if g_server ~= nil then
        if vehicle ~= nil and vehicle.onOutdoorEntryLightsOccupiedChanged ~= nil then
            vehicle:onOutdoorEntryLightsOccupiedChanged("localPlayer", occupied)
        end
    end
end