Klappen = {};

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

function Klappen:load(xmlFile)

self.setRotateDirection= SpecializationUtil.callSpecializationsFunction("setRotateDirection");
	self.haspel = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.haspel#index"));

local rotationPartNodeRotation1= Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.Rotation1#index"));
	if rotationPartNodeRotation1 ~= nil then
		self.Rotation1 = {};
		self.Rotation1.node = rotationPartNodeRotation1;

		local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.Rotation1#minRot"));
		self.Rotation1.minRot = {};
		self.Rotation1.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
		self.Rotation1.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
		self.Rotation1.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

		x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.Rotation1#maxRot"));
		self.Rotation1.maxRot = {};
		self.Rotation1.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
		self.Rotation1.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
		self.Rotation1.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

		self.Rotation1.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.Rotation1#rotTime"), 2)*1000;
	end;
local rotationPartNodeRotation5= Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.Rotation5#index"));
	if rotationPartNodeRotation5 ~= nil then
		self.Rotation5 = {};
		self.Rotation5.node = rotationPartNodeRotation5;

		local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.Rotation5#minRot"));
		self.Rotation5.minRot = {};
		self.Rotation5.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
		self.Rotation5.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
		self.Rotation5.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

		x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.Rotation5#maxRot"));
		self.Rotation5.maxRot = {};
		self.Rotation5.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
		self.Rotation5.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
		self.Rotation5.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

		self.Rotation5.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.Rotation5#rotTime"), 2)*1000;
	end;
local rotationPartNodeRotation6= Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.Rotation6#index"));
	if rotationPartNodeRotation6 ~= nil then
		self.Rotation6 = {};
		self.Rotation6.node = rotationPartNodeRotation6;

		local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.Rotation6#minRot"));
		self.Rotation6.minRot = {};
		self.Rotation6.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
		self.Rotation6.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
		self.Rotation6.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

		x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.Rotation6#maxRot"));
		self.Rotation6.maxRot = {};
		self.Rotation6.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
		self.Rotation6.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
		self.Rotation6.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

		self.Rotation6.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.Rotation6#rotTime"), 2)*1000;
	end;
local rotationPartNodeRotation3= Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.Rotation3#index"));
	if rotationPartNodeRotation3 ~= nil then
		self.Rotation3 = {};
		self.Rotation3.node = rotationPartNodeRotation3;

		local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.Rotation3#minRot"));
		self.Rotation3.minRot = {};
		self.Rotation3.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
		self.Rotation3.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
		self.Rotation3.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

		x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.Rotation3#maxRot"));
		self.Rotation3.maxRot = {};
		self.Rotation3.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
		self.Rotation3.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
		self.Rotation3.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

		self.Rotation3.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.Rotation3#rotTime"), 2)*1000;
	end;

  self.currentRotationDir= false;
end;

function Klappen:delete()
end;

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

function Klappen:keyEvent(unicode, sym, modifier, isDown)
	   if isDown and sym == Input.KEY_k then
			 self:setRotateDirection(not self.currentRotationDir);
	  end;
	 end;

function Klappen:readStream(streamId, connection)
	 self:setRotateDirection(streamReadBool(streamId), true);
end;

function Klappen:writeStream(streamId, connection)
	streamWriteBool(streamId, self.currentRotationDir);
end;

function Klappen:update(dt)
	if self.isThreshing and self.haspel ~= nil then
		rotate(self.haspel, dt*0.005, 0, 0);
	end;
end;


function Klappen:updateTick(dt)
	if self.Rotation1 ~= nil then
		local x, y, z = getRotation(self.Rotation1.node);
		local rot = {x,y,z};
		local newRot = Utils.getMovedLimitedValues(rot, self.Rotation1.maxRot, self.Rotation1.minRot, 3, self.Rotation1.rotTime, dt, not self.currentRotationDir);
		setRotation(self.Rotation1.node, unpack(newRot));
   end;
	if self.Rotation5 ~= nil then
		local x, y, z = getRotation(self.Rotation5.node);
		local rot = {x,y,z};
		local newRot = Utils.getMovedLimitedValues(rot, self.Rotation5.maxRot, self.Rotation5.minRot, 3, self.Rotation5.rotTime, dt, not self.currentRotationDir);
		setRotation(self.Rotation5.node, unpack(newRot));
   end;
	if self.Rotation6 ~= nil then
		local x, y, z = getRotation(self.Rotation6.node);
		local rot = {x,y,z};
		local newRot = Utils.getMovedLimitedValues(rot, self.Rotation6.maxRot, self.Rotation6.minRot, 3, self.Rotation6.rotTime, dt, not self.currentRotationDir);
		setRotation(self.Rotation6.node, unpack(newRot));
   end;
	if self.Rotation3 ~= nil then
		local x, y, z = getRotation(self.Rotation3.node);
		local rot = {x,y,z};
		local newRot = Utils.getMovedLimitedValues(rot, self.Rotation3.maxRot, self.Rotation3.minRot, 3, self.Rotation3.rotTime, dt, not self.currentRotationDir);
		setRotation(self.Rotation3.node, unpack(newRot));
   end;
end;

function Klappen:setRotateDirection(newDirection, noEventSend)
	RotationEvent.sendEvent(self, newDirection, noEventSend);
	self.currentRotationDir = newDirection;
end;

function Klappen:draw()
end; 
