SpawnPalletsAtSiloEvent = {}
local SpawnPalletsAtSiloEvent_mt = Class(SpawnPalletsAtSiloEvent, Event)
InitEventClass(SpawnPalletsAtSiloEvent, "SpawnPalletsAtSiloEvent")

---
function SpawnPalletsAtSiloEvent.emptyNew()
    local self = Event.new(SpawnPalletsAtSiloEvent_mt)
    return self
end

---
function SpawnPalletsAtSiloEvent.new(aPalletSilo, ownerFarmId, fillUnitIndex, pendingLiters, fillTypeIndex)
    local self = SpawnPalletsAtSiloEvent.emptyNew()
    
    self.aPalletSilo = aPalletSilo
    self.ownerFarmId = ownerFarmId
    self.fillUnitIndex = fillUnitIndex
    self.pendingLiters = pendingLiters
    self.fillTypeIndex = fillTypeIndex

    return self
end

---
function SpawnPalletsAtSiloEvent:readStream(streamId, connection)
    self.aPalletSilo = NetworkUtil.readNodeObject(streamId)
    self.ownerFarmId = streamReadInt32(streamId)
    self.fillUnitIndex = streamReadInt32(streamId)
    self.pendingLiters = streamReadFloat32(streamId)
    self.fillTypeIndex = streamReadInt32(streamId)
    
    self:run(connection)
end

---
function SpawnPalletsAtSiloEvent:writeStream(streamId, connection)
    NetworkUtil.writeNodeObject(streamId, self.aPalletSilo) 
    streamWriteInt32(streamId, self.ownerFarmId)
    streamWriteInt32(streamId, self.fillUnitIndex)
    streamWriteFloat32(streamId, self.pendingLiters)
    streamWriteInt32(streamId, self.fillTypeIndex)
end

---
function SpawnPalletsAtSiloEvent:run(connection)
    assert(not connection:getIsServer(), "SpawnPalletsAtSiloEvent is client to server only")

    -- eintragen was vom client gebraucht wird in die spec
    local spec = self.aPalletSilo.spec_aPalletSilo
    spec.fillUnitIndex = self.fillUnitIndex;
    spec.pendingLiters = self.pendingLiters;
    spec.fillTypeIndex = self.fillTypeIndex;

        -- local delta = pallet:addFillUnitFillLevel(self.placable.ownerFarmId, spec.fillUnitIndex, spec.pendingLiters, fillTypeIndex, ToolType.UNDEFINED)

    spec.palletSpawner:spawnPallet(self.ownerFarmId, self.fillTypeIndex, spec.activatable.getPalletCallback, spec.activatable)
end

function SpawnPalletsAtSiloEvent.sendEvent(aPalletSilo, ownerFarmId, fillUnitIndex, pendingLiters, fillTypeIndex, noEventSend)
    g_client:getServerConnection():sendEvent(SpawnPalletsAtSiloEvent.new(aPalletSilo, ownerFarmId, fillUnitIndex, pendingLiters, fillTypeIndex))
end