Baler2 = {};

function Baler2.prerequisitesPresent(specializations)
    return true;
end;

function Baler2:load(savegame)
    Utils.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.fillScale#value", "vehicle.baler#fillScale")
    Utils.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.baleAnimation", "vehicle.baler.baleAnimation")
    Utils.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.baleAnimation", "vehicle.baler.baleAnimation")
    Utils.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.balerUVScrollParts.balerUVScrollPart", "vehicle.baler.uvScrollParts.uvScrollPart")
    Utils.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.knotingAnimation", "vehicle.baler.knotingAnimation")
    Utils.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.balingAnimation", "vehicle.baler.balingAnimation")
    Utils.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.baleUnloading#allowed", "vehicle.baler.baleUnloading#allowed")
    Utils.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.baleUnloading#time", "vehicle.baler.baleUnloading#time")
    Utils.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.baleUnloading#foldThreshold", "vehicle.baler.baleUnloading#foldThreshold")
    self.doCheckSpeedLimit = Utils.overwrittenFunction(self.doCheckSpeedLimit, Baler.doCheckSpeedLimit)
    self.getIsSpeedRotatingPartActive = Utils.overwrittenFunction(self.getIsSpeedRotatingPartActive, Baler.getIsSpeedRotatingPartActive)
    self.allowPickingUp = Utils.overwrittenFunction(self.allowPickingUp, Baler.allowPickingUp)
    self.getIsTurnedOnAllowed = Utils.overwrittenFunction(self.getIsTurnedOnAllowed, Baler.getIsTurnedOnAllowed)
    self.getIsFoldAllowed = Utils.overwrittenFunction(self.getIsFoldAllowed, Baler.getIsFoldAllowed)
    self.setUnitFillLevel = Utils.appendedFunction(self.setUnitFillLevel, Baler.setUnitFillLevel)
    self.isUnloadingAllowed = Baler.isUnloadingAllowed
    self.getTimeFromLevel = Baler.getTimeFromLevel
    self.moveBales = SpecializationUtil.callSpecializationsFunction("moveBales")
    self.moveBale = SpecializationUtil.callSpecializationsFunction("moveBale")
    self.allowFillType = Baler.allowFillType
    self.setIsUnloadingBale = Baler.setIsUnloadingBale
    self.dropBale = Baler.dropBale
    self.createBale = Baler.createBale
    self.setBaleTime = Baler.setBaleTime
    self.processBalerAreas = Baler.processBalerAreas
    self.baler = {}
    self.baler.fillScale = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.fillScale#value"), 1)
    self.baler.fillUnitIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.baler#fillUnitIndex"), 1)
    self.baler.unloadInfoIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.baler#unloadInfoIndex"), 1)
    self.baler.loadInfoIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.baler#loadInfoIndex"), 1)
    self.baler.dischargeInfoIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.baler#dischargeInfoIndex"), 1)
    local firstBaleMarker = getXMLFloat(self.xmlFile, "vehicle.baler.baleAnimation2#firstBaleMarker")
    if firstBaleMarker ~= nil then
        local baleAnimCurve = AnimCurve:new(linearInterpolatorN)
        local keyI = 0
        while true do
            local key = string.format("vehicle.baler.baleAnimation2.key(%d)", keyI)
            local t = getXMLFloat(self.xmlFile, key.."#time")
            local x,y,z = Utils.getVectorFromString(getXMLString(self.xmlFile, key.."#pos"))
            if x == nil or y == nil or z == nil then
                break
            end
            local rx, ry, rz = Utils.getVectorFromString(getXMLString(self.xmlFile, key.."#rot"))
            rx = math.rad(Utils.getNoNil(rx, 0))
            ry = math.rad(Utils.getNoNil(ry, 0))
            rz = math.rad(Utils.getNoNil(rz, 0))
            baleAnimCurve:addKeyframe({ v={x, y, z, rx, ry, rz}, time = t})
            keyI = keyI +1
        end
        if keyI > 0 then
            self.baler.baleAnimCurve = baleAnimCurve
            self.baler.firstBaleMarker = firstBaleMarker
        end
    end
    self.baler.baleAnimRoot, self.baler.baleAnimRootComponent = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.baler.baleAnimation2#node"))
    if self.baler.baleAnimRoot == nil then
        self.baler.baleAnimRoot = self.components[1].node
        self.baler.baleAnimRootComponent = self.components[1].node
    end
    -- there is no standard bale animation, load the unload animation (for round baler)
    if self.baler.firstBaleMarker == nil then
        local unloadAnimationName = getXMLString(self.xmlFile, "vehicle.baler.baleAnimation2#unloadAnimationName")
        local closeAnimationName = getXMLString(self.xmlFile, "vehicle.baler.baleAnimation2#closeAnimationName")
        local unloadAnimationSpeed = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.baler.baleAnimation2#unloadAnimationSpeed"), 1)
        local closeAnimationSpeed = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.baler.baleAnimation2#closeAnimationSpeed"), 1)
        if unloadAnimationName ~= nil and closeAnimationName ~= nil then
            if self.playAnimation ~= nil and self.animations ~= nil then
                if self.animations[unloadAnimationName] ~= nil and self.animations[closeAnimationName] ~= nil then
                    --print("has unload animation")
                    self.baler.baleUnloadAnimationName = unloadAnimationName
                    self.baler.baleUnloadAnimationSpeed = unloadAnimationSpeed
                    self.baler.baleCloseAnimationName = closeAnimationName
                    self.baler.baleCloseAnimationSpeed = closeAnimationSpeed
                    self.baler.baleDropAnimTime = getXMLFloat(self.xmlFile, "vehicle.baler.baleAnimation2#baleDropAnimTime")
                    if self.baler.baleDropAnimTime == nil then
                        self.baler.baleDropAnimTime = self:getAnimationDuration(self.baler.baleUnloadAnimationName)
                    else
                        self.baler.baleDropAnimTime = self.baler.baleDropAnimTime * 1000
                    end
                else
                    print("Error: Failed to find unload animations '"..unloadAnimationName.."' and '"..closeAnimationName.."' in '"..self.configFileName.."'.")
                end
            else
                print("Error: There is an unload animation in '"..self.configFileName.."' but it is not a AnimatedVehicle. Change to a vehicle type which has the AnimatedVehicle specialization.")
            end
        end
    end
    self.baler.baleTypes = {}
    local i = 0
    while true do
        local key = string.format("vehicle.baler.baleTypes.baleType(%d)", i)
        if not hasXMLProperty(self.xmlFile, key) then
            break
        end
        local isRoundBale = Utils.getNoNil(getXMLBool(self.xmlFile, key.."#isRoundBale"), false)
        local width = Utils.round(Utils.getNoNil(getXMLFloat(self.xmlFile, key.."#width"), 1.2), 2)
        local height = Utils.round(Utils.getNoNil(getXMLFloat(self.xmlFile, key.."#height"), 0.9), 2)
        local length = Utils.round(Utils.getNoNil(getXMLFloat(self.xmlFile, key.."#length"), 2.4), 2)
        local diameter = Utils.round(Utils.getNoNil(getXMLFloat(self.xmlFile, key.."#diameter"), 1.8), 2)
        table.insert(self.baler.baleTypes, {isRoundBale=isRoundBale, width=width, height=height, length=length, diameter=diameter})
        i = i + 1
    end
    self.baler.currentBaleTypeId = 1
    if table.getn(self.baler.baleTypes) == 0 then
        self.baler.baleTypes = nil
    end
    if self.isClient then
        self.baler.sampleBaler = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.baler.balerSound", nil, self.baseDirectory)
        self.baler.sampleBalerAlarm = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.baler.balerAlarm", nil, self.baseDirectory)
        self.baler.sampleBalerEject = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.baler.balerBaleEject", nil, self.baseDirectory)
        self.baler.sampleBalerDoor = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.baler.balerDoor", nil, self.baseDirectory)
        self.baler.sampleBalerKnotCleaning = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.baler.balerKnotCleaning", nil, self.baseDirectory)
        self.baler.knotCleaningTime = 10000
        self.baler.uvScrollParts = Utils.loadScrollers(self.components, self.xmlFile, "vehicle.baler.uvScrollParts.uvScrollPart", {}, false)
        self.baler.turnedOnRotationNodes = Utils.loadRotationNodes(self.xmlFile, {}, "vehicle.turnedOnRotationNodes.turnedOnRotationNode", "baler", self.components)
        self.baler.fillEffects = EffectManager:loadEffect(self.xmlFile, "vehicle.baler.fillEffect", self.components, self)
        self.baler.knotingAnimation = getXMLString(self.xmlFile, "vehicle.baler.knotingAnimation#name")
        self.baler.knotingAnimationSpeed = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.knotingAnimation#speed"), 1)
    end
    self.baler.balingAnimationName = Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.baler.balingAnimation#name"), "")
    if self.playAnimation == nil or self.getIsAnimationPlaying == nil then
        self.baler.balingAnimationName = ""
    end
    self.baler.lastAreaBiggerZero = false
    self.baler.lastAreaBiggerZeroSent = false
    self.baler.lastAreaBiggerZeroTime = 0
    self.baler.unloadingState = Baler.UNLOADING_CLOSED
    self.baler.pickupFillTypes = {}
    self.baler.bales = {}
    self.baler.hasBaler = true
    self.baler.dummyBale = {}
    self.baler.dummyBale.scaleNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.baler.baleAnimation2#scaleNode"))
    self.baler.dummyBale.baleNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.baler.baleAnimation2#baleNode"))
    self.baler.dummyBale.currentBaleFillType = FillUtil.FILLTYPE_UNKNOWN
    self.baler.dummyBale.currentBale = nil
    self.baler.allowsBaleUnloading = Utils.getNoNil(getXMLBool(self.xmlFile, "vehicle.baler.baleUnloading#allowed"), false)
    self.baler.baleUnloadingTime = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.baler.baleUnloading#time"), 4) * 1000
    self.baler.baleFoldThreshold = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.baler.baleUnloading#foldThreshold"), 0.25) * self:getCapacity()
    self.baler.isBaleUnloading = false
    self.baler.isSpeedLimitActive = false
    self.baler.dirtyFlag = self:getNextDirtyFlag()
end

function Baler2:delete()
end

function Baler2:readStream(streamId, connection)
end

function Baler2:wriBaler2ream(streamId, connection)
end

function Baler2:mouseEvent(posX, posY, isDown, isUp, button)
end;

function Baler2:keyEvent(unicode, sym, modifier, isDown)
end;

function Baler2:update(dt)
end

function Baler2:draw()
end;
