RotationNodes = {}

function RotationNodes.prerequisitesPresent(specializations)
	return SpecializationUtil.hasSpecialization(Motorized, specializations)
end
function RotationNodes:load(xmlFile)
	self.ProtationNodes = {}
	local i = 0
	while true do
		local key = string.format("vehicle.ProtationNodes.rotationNode(%d)", i)
		if not hasXMLProperty(xmlFile, key) then
			break
		end
		local Pnode = Utils.indexToObject(self.components, getXMLString(xmlFile, key .. "#index"))
		local rotSpeed = math.rad(Utils.getNoNil(getXMLFloat(xmlFile, key .. "#rotSpeed"), 1) * 0.001)
		if Pnode ~= nil then
			table.insert(self.ProtationNodes, {Pnode = Pnode, rotSpeed = rotSpeed, rotation = 0})
		end
		i = i + 1
	end
	self.UrotationNodes = {}
	local i = 0
	while true do
		local key = string.format("vehicle.UrotationNodes.rotationNode(%d)", i)
		if not hasXMLProperty(xmlFile, key) then
			break
		end
		local Unode = Utils.indexToObject(self.components, getXMLString(xmlFile, key .. "#index"))
		local rotSpeed = math.rad(Utils.getNoNil(getXMLFloat(xmlFile, key .. "#rotSpeed"), 1) * 0.001)
		if Unode ~= nil then
			table.insert(self.UrotationNodes, {Unode = Unode, rotSpeed = rotSpeed, rotation = 0})
		end
		i = i + 1
	end	
	self.WrotationNodes = {}
	local i = 0
	while true do
		local key = string.format("vehicle.WrotationNodes.rotationNode(%d)", i)
		if not hasXMLProperty(xmlFile, key) then
			break
		end
		local Wnode = Utils.indexToObject(self.components, getXMLString(xmlFile, key .. "#index"))
		local rotSpeed = math.rad(Utils.getNoNil(getXMLFloat(xmlFile, key .. "#rotSpeed"), 1) * 0.001)
		if Wnode ~= nil then
			table.insert(self.WrotationNodes, {Wnode = Wnode, rotSpeed = rotSpeed, rotation = 0})
		end
		i = i + 1
	end

-- motor

    self.motor1Active = false;
    self.motor2Active = false;		
    self.delayOnLeft = 0;
    self.delayOffLeft = 0;
    self.delayOnRight = 0;
    self.delayOffRight = 0;
    self.delayKurs1 = 0;
    self.delayKurs2 = 0;
    self.delayOn = 0;
    self.delayOff = 0;
    self.deltafo = 0;

  local motor1Node = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.motor1#index"));
    if motor1Node ~= nil then
        self.motor1 = {};
        self.motor1.node = motor1Node;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.motor1#minRot"));
        self.motor1.minRot = {};
        self.motor1.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.motor1.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.motor1.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));
        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.motor1#maxRot"));
        self.motor1.maxRot = {};
        self.motor1.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.motor1.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.motor1.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));
        self.motor1.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.motor1#rotTime"), 2)*70;
        self.motor1.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.motor1#touchRotLimit"), 10));
    end;

  local motor2Node = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.motor2#index"));
    if motor2Node ~= nil then
        self.motor2 = {};
        self.motor2.node = motor2Node;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.motor2#minRot"));
        self.motor2.minRot = {};
        self.motor2.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.motor2.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.motor2.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));
        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.motor2#maxRot"));
        self.motor2.maxRot = {};
        self.motor2.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.motor2.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.motor2.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));
        self.motor2.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.motor2#rotTime"), 2)*70;
        self.motor2.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.motor2#touchRotLimit"), 10));
    end;		
end

function RotationNodes:delete()
end

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

function RotationNodes:keyEvent(unicode, sym, modifier, isDown)
end

function RotationNodes:update(dt)
end

function RotationNodes:updateTick(dt)
	if self.isMotorStarted then
		for _, Pnode in pairs(self.ProtationNodes) do
			Pnode.rotation = (Pnode.rotation + dt * Pnode.rotSpeed) % (2 * math.pi)
			setRotation(Pnode.Pnode, 0, 0, Pnode.rotation)  ---вращение (Pnode.Pnode, Х, Y, Z) можно менять вращение по осям
		end	
    end
	    if self.pipeIsUnloading then
			for _, Unode in pairs(self.UrotationNodes) do
				Unode.rotation = (Unode.rotation + dt * Unode.rotSpeed) % (2 * math.pi)
				setRotation(Unode.Unode, Unode.rotation, 0, 0)  ---вращение по умолчанию Х (Unode.Unode, Х, Y, Z) можно менять вращение по осям (пример для Y -- setRotation(Unode.Unode, 0, Unode.rotation, 0)
			end	
	end				
	    if self:getIsActive() and self.isTurnedOn then
			for _, Wnode in pairs(self.WrotationNodes) do
				Wnode.rotation = (Wnode.rotation + dt * Wnode.rotSpeed) % (2 * math.pi)
				setRotation(Wnode.Wnode, Wnode.rotation, 0, 0)  ---вращение по умолчанию Х (Wnode.Wnode, Х, Y, Z) можно менять вращение по осям (пример для Y -- setRotation(Wnode.Wnode, 0, Wnode.rotation, 0)
			end	
      end

-- motor

        if self.motor1Active ~= nil and self.isMotorStarted then
			   if self.delayKurs1 >= 0 then
				self.delayKurs1 = self.delayKurs1 -dt;
			end;
			   if self.delayKurs2 >= 0 then
				self.delayKurs2 = self.delayKurs2 -dt;
			end;
			     if not self.motor1Max then
				   if self.delayKurs1 < 0 then
					self.delayKurs2 = 70;
					self.motor1Max = true;
					self.motor2Max = true;
				end;
			end;
			         if self.motor1Max then
				   if self.delayKurs2 < 0 then
					self.delayKurs1 = 70;
					self.motor1Max = false;
					self.motor2Max = false;
				end;
			end;
		else
			self.motor1Max = false;
			self.motor2Max = false;
		end;

      if self.motor1 ~= nil then
        	local x, y, z = getRotation(self.motor1.node);
        	local rot = {x,y,z};
        	local newRot = Utils.getMovedLimitedValues(rot, self.motor1.maxRot, self.motor1.minRot, 3, self.motor1.rotTime, dt, not self.motor1Max);
        	setRotation(self.motor1.node, unpack(newRot));
    	end;

      if self.motor2 ~= nil then
        	local x, y, z = getRotation(self.motor2.node);
        	local rot = {x,y,z};
        	local newRot = Utils.getMovedLimitedValues(rot, self.motor2.maxRot, self.motor2.minRot, 3, self.motor2.rotTime, dt, not self.motor2Max);
        	setRotation(self.motor2.node, unpack(newRot));
    	end;	
end

function RotationNodes:draw()
end
