local modName = g_currentModName

local function getSpecTable(self)
	return self["spec_" .. modName .. ".speedSupportAnimation"]
end

SpeedSupportAnimation = {}

function SpeedSupportAnimation.prerequisitesPresent(specializations)
	return true
end

function SpeedSupportAnimation.registerOverwrittenFunctions(vehicleType)
end

function SpeedSupportAnimation.registerEventListeners(vehicleType)
	SpecializationUtil.registerEventListener(vehicleType, "onLoad", SpeedSupportAnimation)
	SpecializationUtil.registerEventListener(vehicleType, "onPostLoad", SpeedSupportAnimation)
	SpecializationUtil.registerEventListener(vehicleType, "onUpdate", SpeedSupportAnimation)
end

function SpeedSupportAnimation:onLoad(savegame)
	if self.isServer then
		local spec = getSpecTable(self)
		spec.animationName = getXMLString(self.xmlFile, "vehicle.speedSupportAnimation#name")
		spec.animationSpeed = getXMLFloat(self.xmlFile, "vehicle.speedSupportAnimation#speed") or 1
		spec.speedThreshold = getXMLFloat(self.xmlFile, "vehicle.speedSupportAnimation#speedThreshold") or 0.1
		spec.startLowered = Utils.getNoNil(getXMLBool(self.xmlFile, "vehicle.speedSupportAnimation#startLowered"), true)
		spec.animationState = false
	end
end
	
function SpeedSupportAnimation:onPostLoad(savegame)
	if self.isServer then
		local spec = getSpecTable(self)

		if spec.animationName ~= nil and spec.startLowered then
			self:playAnimation(spec.animationName, spec.animationSpeed, self:getAnimationTime(spec.animationName), true)
			AnimatedVehicle.updateAnimationByName(self, spec.animationName, 9999999, false)

			spec.animationState = true
		end

		spec.spawnTime = g_time
	end
end

function SpeedSupportAnimation:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
	if self.isServer then
		local spec = getSpecTable(self)

		if spec.animationName ~= nil and spec.spawnTime + 2000 < g_time then
			local newState = self:getLastSpeed() < spec.speedThreshold

			if newState ~= spec.animationState then
				if newState then
					self:playAnimation(spec.animationName, spec.animationSpeed, self:getAnimationTime(spec.animationName), true)
				else
					self:playAnimation(spec.animationName, -spec.animationSpeed, self:getAnimationTime(spec.animationName), true)
					end
				spec.animationState = newState
			end
		end
	end
end