FoldableWheelChock = {
	MOD_NAME = g_currentModName,
	SPEC_NAME = g_currentModName .. ".foldableWheelChock",
	SPEC_TBL_NAME = "spec_" .. g_currentModName .. ".foldableWheelChock",
	prerequisitesPresent = function (specializations)
		return SpecializationUtil.hasSpecialization(Foldable, specializations) or SpecializationUtil.hasSpecialization(Wheels, specializations)
	end,
	initSpecialization = function ()
	end,
	registerFunctions = function (vehicleType)
	end
}

function FoldableWheelChock.registerOverwrittenFunctions(vehicleType)
	SpecializationUtil.registerOverwrittenFunction(vehicleType, "onWheelChockI3DLoaded", FoldableWheelChock.onWheelChockI3DLoaded)
	SpecializationUtil.registerOverwrittenFunction(vehicleType, "updateWheelChockPosition", FoldableWheelChock.updateWheelChockPosition)
end

function FoldableWheelChock.registerEventListeners(vehicleType)
	SpecializationUtil.registerEventListener(vehicleType, "onFoldTimeChanged", FoldableWheelChock)
end

function FoldableWheelChock:onFoldTimeChanged(foldAnimTime)
	local spec = self.spec_wheels

	for _, wheelChock in pairs(spec.wheelChocks) do
		self:updateWheelChockPosition(wheelChock, nil)
	end
end

function FoldableWheelChock:onWheelChockI3DLoaded(superFunc, i3dNode, failedReason, args)
	local wheel = args.wheel
	local xmlFile = args.xmlFile
	local configKey = args.configKey
	local chockKey = args.chockKey
	local numBefore = #wheel.wheelChocks

	superFunc(self, i3dNode, failedReason, args)

	local numAfter = #wheel.wheelChocks

	if numBefore < numAfter then
		local wheelChock = wheel.wheelChocks[numAfter]
		wheelChock.foldMinLimit = self:getWheelConfigurationValue(xmlFile, wheel.configIndex, configKey, chockKey .. "#foldMinLimit", 0)
		wheelChock.foldMaxLimit = self:getWheelConfigurationValue(xmlFile, wheel.configIndex, configKey, chockKey .. "#foldMaxLimit", 1)

		if wheelChock.foldMinLimit == 0 and wheelChock.foldMaxLimit == 1 then
			wheelChock.foldMinLimit = nil
			wheelChock.foldMaxLimit = nil
		end
	end
end

function FoldableWheelChock:updateWheelChockPosition(superFunc, wheelChock, isInParkingPosition)
	if wheelChock.foldMinLimit ~= nil then
		local time = self:getFoldAnimTime()

		if time < wheelChock.foldMinLimit or wheelChock.foldMaxLimit < time then
			return superFunc(self, wheelChock, true)
		end
	end

	return superFunc(self, wheelChock, isInParkingPosition)
end

Wheels.registerWheelChockXMLPaths = Utils.overwrittenFunction(Wheels.registerWheelChockXMLPaths, function (schema, superFunc, key)
	superFunc(schema, key)
	schema:register(XMLValueType.FLOAT, key .. "#foldMinLimit", "Min. folding time to display the wheel chock", 0)
	schema:register(XMLValueType.FLOAT, key .. "#foldMaxLimit", "Max. folding time to display the wheel chock", 1)
end)
