-- PowerTake.lua
-- sets an individual engine sound for engine configurations
--
-- Author GE Aluha74rus
-- 25.05.2020
--
-- convert FS19 Aluha74rus
PowerTake = {};


function PowerTake.prerequisitesPresent(specializations)
    return true;
end

-- function PowerTake.registerEventListeners(vehicleType)	
	-- SpecializationUtil.registerEventListener(vehicleType, "onLoad", PowerTake);
	-- SpecializationUtil.registerEventListener(vehicleType, "onDelete", PowerTake)
-- end
function PowerTake:onLoad(savegame)
    local spec = self.spec_powerTakeOffs
    spec.outputPowerTakeOffs = {}
    spec.inputPowerTakeOffs = {}
    spec.localPowerTakeOffs = {}
    spec.delayedPowerTakeOffsMountings = {}
    if self.isClient then
        self.vomAnimationNodes = g_animationManager:loadAnimations(self.xmlFile, "vehicle.turnOnVomK700.animationNodes", self.components, self, self.i3dMappings)
    end
end
PowerTakeOffs.onLoad = Utils.overwrittenFunction(PowerTakeOffs.onLoad, PowerTake.onLoad);
function PowerTake:onDelete()
    local spec = self.spec_powerTakeOffs
    -- for _,output in pairs(spec.outputPowerTakeOffs) do
        -- if output.rootNode ~= nil then
            -- delete(output.rootNode)
            -- delete(output.attachNode)
        -- end
    -- end
    -- for _,input in pairs(spec.inputPowerTakeOffs) do
        -- if input.rootNode ~= nil then
            -- delete(input.rootNode)
            -- delete(input.attachNode)
        -- end
        -- g_animationManager:deleteAnimations(input.animationNodes)
    -- end
    -- for _,localPto in pairs(spec.localPowerTakeOffs) do
        -- g_animationManager:deleteAnimations(localPto.animationNodes)
    -- end
-- else
    for _,output in pairs(spec.outputPowerTakeOffs) do
        if output.rootNode ~= nil then
            delete(output.rootNode)
            delete(output.attachNode)
        end
    end
    for _,input in pairs(spec.inputPowerTakeOffs) do
        if input.rootNode ~= nil then
            delete(input.rootNode)
            delete(input.attachNode)
        end
        g_animationManager:deleteAnimations(self.vomAnimationNodes)
        g_animationManager:deleteAnimations(input.animationNodes)
    end
    for _,localPto in pairs(spec.localPowerTakeOffs) do
        g_animationManager:deleteAnimations(localPto.animationNodes)
    end
end
PowerTakeOffs.onDelete = Utils.overwrittenFunction(PowerTakeOffs.onDelete, PowerTake.onDelete);
function PowerTake:onUpdateInterpolation(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
    if self.isClient then
        local spec = self.spec_powerTakeOffs
        for _, input in pairs(spec.inputPowerTakeOffs) do
            if input.connectedVehicle ~= nil then
                if self.updateLoopIndex == input.connectedVehicle.updateLoopIndex then
                    self:updatePowerTakeOff(input, dt)
                end
            end
        end
        if self.getAttachedImplements ~= nil then
            for _, implement in ipairs(self:getAttachedImplements()) do
                if implement.object.updateAttachedPowerTakeOffs ~= nil then
                    implement.object:updateAttachedPowerTakeOffs(dt, self)
                end
            end
        end
        local isPowerTakeOffActive = self:getIsPowerTakeOffActive()
        if spec.lastIsPowerTakeOffActive ~= isPowerTakeOffActive then
            for _, input in pairs(spec.inputPowerTakeOffs) do
                if isPowerTakeOffActive then
                    g_animationManager:startAnimations(input.animationNodes)
                else
                    g_animationManager:stopAnimations(input.animationNodes)
                end
            end
            for _, localPto in pairs(spec.localPowerTakeOffs) do
                if isPowerTakeOffActive then
                    g_animationManager:startAnimations(localPto.animationNodes)
                else
                    g_animationManager:stopAnimations(localPto.animationNodes)
                end
            end
            spec.lastIsPowerTakeOffActive = isPowerTakeOffActive
        end
    else
        for _, input in pairs(spec.inputPowerTakeOffs) do
            if input.connectedVehicle ~= nil then
                if self.updateLoopIndex == input.connectedVehicle.updateLoopIndex then
                    self:updatePowerTakeOff(input, dt)
                end
            end
        end
        if self.getAttachedImplements ~= nil then
            for _, implement in ipairs(self:getAttachedImplements()) do
                if implement.object.updateAttachedPowerTakeOffs ~= nil then
                    implement.object:updateAttachedPowerTakeOffs(dt, self)
                end
            end
        end
        local isPowerTakeOffActive = self:getIsPowerTakeOffActive()
        if spec.lastIsPowerTakeOffActive ~= isPowerTakeOffActive then
            for _, input in pairs(spec.inputPowerTakeOffs) do
                if isPowerTakeOffActive then
                    g_animationManager:startAnimations(self.vomAnimationNodes)
                    g_animationManager:startAnimations(input.animationNodes)
                else
                    g_animationManager:stopAnimations(self.vomAnimationNodes)
                    g_animationManager:stopAnimations(input.animationNodes)
                end
            end
            for _, localPto in pairs(spec.localPowerTakeOffs) do
                if isPowerTakeOffActive then
                    g_animationManager:startAnimations(localPto.animationNodes)
                else
                    g_animationManager:stopAnimations(localPto.animationNodes)
                end
            end
            spec.lastIsPowerTakeOffActive = isPowerTakeOffActive
        end
    end
end
PowerTakeOffs.onUpdateInterpolation = Utils.overwrittenFunction(PowerTakeOffs.onUpdateInterpolation, PowerTake.onUpdateInterpolation);