--
-- ExtendedWindrowerChangeWorkModeEvent
--
-- @author	Rockstar49
-- @date	02/06/2024
--


ExtendedWindrowerChangeWorkModeEvent = {}
ExtendedWindrowerChangeWorkModeEvent_mt = Class(ExtendedWindrowerChangeWorkModeEvent, Event)

InitEventClass(ExtendedWindrowerChangeWorkModeEvent, "ExtendedWindrowerChangeWorkModeEvent")

function ExtendedWindrowerChangeWorkModeEvent.emptyNew()
	local self = Event.new(ExtendedWindrowerChangeWorkModeEvent_mt)

	return self
end

function ExtendedWindrowerChangeWorkModeEvent.new(vehicle, mode)
	local self = ExtendedWindrowerChangeWorkModeEvent.emptyNew()

	self.vehicle = vehicle
	self.mode = mode

	return self
end

function ExtendedWindrowerChangeWorkModeEvent:readStream(streamId, connection)
	self.vehicle = NetworkUtil.readNodeObject(streamId)
	self.mode = streamReadUIntN(streamId, 2)

	self:run(connection)
end

function ExtendedWindrowerChangeWorkModeEvent:writeStream(streamId, connection)
	NetworkUtil.writeNodeObject(streamId, self.vehicle)
	streamWriteUIntN(streamId, self.mode, 2)
end

function ExtendedWindrowerChangeWorkModeEvent:run(connection)
	if self.vehicle ~= nil and self.vehicle:getIsSynchronized() then
		self.vehicle:changeWorkMode(self.mode, true)
	end

	if not connection:getIsServer() then
		g_server:broadcastEvent(ExtendedWindrowerChangeWorkModeEvent.new(self.vehicle, self.mode), nil, connection, self.vehicle)
	end
end

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