--author: igor29381 (Manuscript GT)
--v1.0
--date: 13.01.17

HydraulicSystem = {};

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

function HydraulicSystem:load(savegame)
    if self.powerConsumer then
        self.getDoConsumePtoPower = HydraulicSystem.getDoConsumePtoPower;
        self.getPtoRpm = HydraulicSystem.getPtoRpm;
        self.isFolding = false;
        self.hydraulicSystem = {};
        self.hydraulicSystem.lastFoldAnimTime = 0;
        self.hydraulicSystem.usePowerOnLiftingOnly = Utils.getNoNil(getXMLBool(self.xmlFile, "vehicle.powerConsumer#usePowerOnLiftingOnly"), false);
    else
        print("Warning: "..self.configFileName.."has no spec 'PowerConsumer'. HydraulicSystem can't work!");
    end;
end;

function HydraulicSystem:delete()
end;

function HydraulicSystem:readStream(streamId, connection)
end;

function HydraulicSystem:writeStream(streamId, connection)
end;

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

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

function HydraulicSystem:update(dt)
    if self.hydraulicSystem then
        local rootAttacherVehicle = self:getRootAttacherVehicle();
        if rootAttacherVehicle and rootAttacherVehicle.motor and rootAttacherVehicle:getIsMotorStarted() then
            self.isFolding = false;
            if self.foldAnimTime and self.foldAnimTime ~= self.hydraulicSystem.lastFoldAnimTime then
                if self.tensionBelts then
                    for i=1, #self.tensionBelts.sortedBelts do
                        local belt = self.tensionBelts.sortedBelts[i];
                        if belt.mesh then
                            self:setTensionBeltsActive(false, belt.id, false);
                        end;
                    end;
                end;
                self.isFolding = true;
                if self.hydraulicSystem.usePowerOnLiftingOnly and self.foldAnimTime < self.hydraulicSystem.lastFoldAnimTime then
                    self.isFolding = false;
                end;
                self.hydraulicSystem.lastFoldAnimTime = self.foldAnimTime;
            end;
            if not self.isFolding and self.movingTools then
                for i=1, #self.movingTools do
                    local tool = self.movingTools[i];
                    if tool.lastRotSpeed ~= 0 or tool.lastTransSpeed ~= 0 then
                        self.isFolding = true;
                        break;
                    end;
                end;
            end;
            if not self.isFolding and self.trailer then
                self.isFolding = self.tipState == Trailer.TIPSTATE_OPENING or
                (not self.hydraulicSystem.usePowerOnLiftingOnly and self.tipState == Trailer.TIPSTATE_CLOSING);
            end;
        end;
    end;
end;

function HydraulicSystem:updateTick(dt)
end;

function HydraulicSystem:draw()
end;

function HydraulicSystem:getDoConsumePtoPower()
    return (self.getIsTurnedOn ~= nil and self:getIsTurnedOn()) or self.isFolding;
end;

function HydraulicSystem:getPtoRpm()
    if (self.getIsTurnedOn ~= nil and self:getIsTurnedOn()) or self.isFolding then
        return self.powerConsumer.ptoRpm;
    end;
    return 0;
end;
