--
-- LiftingAxles
-- Specialization for LiftingAxles
--
-- @author John Deere 6930
-- @date 11/10/2019
--

--[[XML
	<liftingAxles>
		<liftingAxle fillUnitIndex="" animationName="" liftAtFillLevel=""/>
		<liftingAxle fillUnitIndex="" animationName="" liftAtFillLevel=""/>
	</liftingAxles>
]]

LiftingAxles = {};
LiftingAxles.MOD_NAME = g_currentModName;
	
function LiftingAxles.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(FillUnit, specializations);
end;

function LiftingAxles.registerFunctions(vehicleType)
    SpecializationUtil.registerFunction(vehicleType, "loadLiftingAxle",          LiftingAxles.loadLiftingAxle);
end;

function LiftingAxles.registerEventListeners(vehicleType)
    SpecializationUtil.registerEventListener(vehicleType, "onLoad", 					LiftingAxles);
    SpecializationUtil.registerEventListener(vehicleType, "onPostLoad", 				LiftingAxles);
    SpecializationUtil.registerEventListener(vehicleType, "onFillUnitFillLevelChanged", LiftingAxles);
end

function LiftingAxles:onLoad(savegame)
    self.spec_LiftingAxles = self[string.format("spec_%s.liftingAxles", LiftingAxles.MOD_NAME)]
    local spec = self.spec_LiftingAxles
	
	spec.liftingAxles = {};
    local i = 0;
    while true do
        local key = string.format("vehicle.liftingAxles.liftingAxle(%d)", i);
        if not hasXMLProperty(self.xmlFile, key) then
            break;
        end;
        local liftingAxle = {}
        if self:loadLiftingAxle(self.xmlFile, key, liftingAxle) then
            table.insert(spec.liftingAxles, liftingAxle);
        end;
        i = i + 1;
    end;
end;

function LiftingAxles:onPostLoad(savegame)
    local spec = self.spec_LiftingAxles;
    for _, liftingAxle in ipairs(spec.liftingAxles) do
		self:playAnimation(liftingAxle.animationName, 1, nil, true);
		AnimatedVehicle.updateAnimationByName(self, liftingAxle.animationName, 9999999);
    end;
end;

function LiftingAxles:loadLiftingAxle(xmlFile, key, liftingAxle)
	local fillUnitIndex = getXMLInt(self.xmlFile, key.."#fillUnitIndex");
	if fillUnitIndex ~= nil then
		liftingAxle.fillUnitIndex = fillUnitIndex;
		liftingAxle.animationName = getXMLString(xmlFile, key.."#animationName");
		liftingAxle.liftAtFillLevel = Utils.getNoNil(getXMLFloat(xmlFile, key.."#liftAtFillLevel"), 1.0);
	end;
    return liftingAxle.animationName ~= nil;
end;

function LiftingAxles:onFillUnitFillLevelChanged(fillUnitIndex, fillLevelDelta, fillTypeIndex, toolType, fillPositionData, appliedDelta)
	local spec = self.spec_LiftingAxles;	
	for _, liftingAxle in pairs(spec.liftingAxles) do
		if liftingAxle.fillUnitIndex == fillUnitIndex then
			local fillLevel = self:getFillUnitFillLevel(fillUnitIndex);
			local direction = 0;
			local stopTime = 0.0;
			if fillLevel >= liftingAxle.liftAtFillLevel then
				direction = -1;
			else
				direction = 1;	
				stopTime = 1.0		
			end;
			if direction ~= 0 and not self:getIsAnimationPlaying(liftingAxle.animationName) then
				self:playAnimation(liftingAxle.animationName, direction, nil, true);
				self:setAnimationStopTime(liftingAxle.animationName, stopTime);
			end;
		end;
	end;
end;
