LSFMBaleStorageBuyEvent = {}

LSFMBaleStorageBuyEvent.BUY_SUCCESS = 0
LSFMBaleStorageBuyEvent.BUY_ERROR_NO_PERMISSION = 1
LSFMBaleStorageBuyEvent.BUY_ERROR_NOT_ENOUGH_MONEY = 2
LSFMBaleStorageBuyEvent.BUY_ERROR_NOT_ENOUGH_SPACE = 3
LSFMBaleStorageBuyEvent.BUY_ERROR_NOT_SUPPORTED = 4
LSFMBaleStorageBuyEvent.BUY_ERROR_STACK_MISMATCH = 5
LSFMBaleStorageBuyEvent.BUY_ERROR_OBJECT_DOES_NOT_EXIST = 6

local LSFMBaleStorageBuyEvent_mt = Class(LSFMBaleStorageBuyEvent, Event)

InitEventClass(LSFMBaleStorageBuyEvent, "LSFMBaleStorageBuyEvent")

function LSFMBaleStorageBuyEvent.emptyNew()
    return Event.new(LSFMBaleStorageBuyEvent_mt)
end

function LSFMBaleStorageBuyEvent.new(placeable, farmId, xmlFilename, fillTypeIndex, quantity, wrappingColor)
    local self = LSFMBaleStorageBuyEvent.emptyNew()
    self.placeable = placeable
    self.farmId = farmId
    self.xmlFilename = xmlFilename
    self.fillTypeIndex = fillTypeIndex
    self.quantity = quantity
    self.wrappingColor = wrappingColor or { 1, 1, 1 }

    return self
end

function LSFMBaleStorageBuyEvent.newServerToClient(errorCode, numBoughtBales)
    local self = LSFMBaleStorageBuyEvent.emptyNew()
    self.errorCode = errorCode
    self.numBoughtBales = numBoughtBales or 0

    return self
end

function LSFMBaleStorageBuyEvent:readStream(streamId, connection)
    if connection:getIsServer() then
        self.errorCode = streamReadUIntN(streamId, 3)
        self.numBoughtBales = streamReadUInt8(streamId)
    else
        self.placeable = NetworkUtil.readNodeObject(streamId)
        self.farmId = streamReadUIntN(streamId, FarmManager.FARM_ID_SEND_NUM_BITS)
        self.xmlFilename = NetworkUtil.convertFromNetworkFilename(streamReadString(streamId))
        self.fillTypeIndex = streamReadUIntN(streamId, FillTypeManager.SEND_NUM_BITS)
        self.quantity = streamReadUInt8(streamId)
        local r, g, b = Color.readStreamRGB(streamId)
        self.wrappingColor = { r, g, b }
    end

    self:run(connection)
end

function LSFMBaleStorageBuyEvent:writeStream(streamId, connection)
    if connection:getIsServer() then
        NetworkUtil.writeNodeObject(streamId, self.placeable)
        streamWriteUIntN(streamId, self.farmId, FarmManager.FARM_ID_SEND_NUM_BITS)
        streamWriteString(streamId, NetworkUtil.convertToNetworkFilename(self.xmlFilename))
        streamWriteUIntN(streamId, self.fillTypeIndex, FillTypeManager.SEND_NUM_BITS)
        streamWriteUInt8(streamId, self.quantity)
        Color.writeStreamRGB(streamId, self.wrappingColor[1], self.wrappingColor[2], self.wrappingColor[3])
    else
        streamWriteUIntN(streamId, self.errorCode, 3)
        streamWriteUInt8(streamId, self.numBoughtBales or 0)
    end
end

function LSFMBaleStorageBuyEvent:run(connection)
    if connection:getIsServer() then
        g_messageCenter:publish(LSFMBaleStorageBuyEvent, self.errorCode, self.numBoughtBales)

        return
    end

    local placeable = self.placeable

    if placeable == nil or placeable.getLSFMBalePurchaseValidation == nil or placeable.buyLSFMBales == nil then
        connection:sendEvent(LSFMBaleStorageBuyEvent.newServerToClient(LSFMBaleStorageBuyEvent.BUY_ERROR_OBJECT_DOES_NOT_EXIST, 0))

        return
    end

    local ownerFarmId = placeable:getOwnerFarmId()

    if self.farmId ~= ownerFarmId or not g_currentMission:getHasPlayerPermission(Farm.PERMISSION.BUY_VEHICLE, connection, ownerFarmId) then
        connection:sendEvent(LSFMBaleStorageBuyEvent.newServerToClient(LSFMBaleStorageBuyEvent.BUY_ERROR_NO_PERMISSION, 0))

        return
    end

    local errorCode, numBought = placeable:buyLSFMBales(self.farmId, self.xmlFilename, self.fillTypeIndex, self.quantity, self.wrappingColor)
    connection:sendEvent(LSFMBaleStorageBuyEvent.newServerToClient(errorCode, numBought))
end
