--
-- Liftachse
-- Specialization for Liftaxle
-- Spezialisierung für Liftachse
-- 
-- frei verwendbar - keine erlaubnis nötig  |  free for use - no permission needed.
-- Modifikationen erst nach Rücksprache!    |  modifications only with my permission.
liftachse = {};

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

function liftachse:load(xmlFile)
	self.isActor = false;
	self.setLiftachse = SpecializationUtil.callSpecializationsFunction("setLiftachse");
	local achse = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.achse#index"));
	if achse ~= nil then
		self.achse = {};
		self.achse.node = achse;
		local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.achse#minRot"));
		self.achse.minRot = {};
		self.achse.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
		self.achse.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
		self.achse.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));
		x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.achse#maxRot"));
		self.achse.maxRot = {};
		self.achse.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
		self.achse.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
		self.achse.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));
		self.achse.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.achse#rotTime"), 1)*1000;
		self.supportTrans1 = {};
		self.supportTrans1.node = achse;
		local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.achse#minTrans"));
		self.supportTrans1.minRot = {};
		self.supportTrans1.minRot[1] = x;
		self.supportTrans1.minRot[2] = y;
		self.supportTrans1.minRot[3] = z;
		x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.achse#maxTrans"));
		self.supportTrans1.maxRot = {};
		self.supportTrans1.maxRot[1] = x;
		self.supportTrans1.maxRot[2] = y;
		self.supportTrans1.maxRot[3] = z;
		self.supportTrans1.rotTime = self.achse.rotTime;
	end;
	self.liftAxisAutoDown = false;
	self.Liftachsedown = false;
end;



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

function liftachse:update(dt)
	local mustAxisDown = false;
	if InputBinding.hasEvent(InputBinding.liftachse) and self:getIsActiveForInput() then
		if self.Liftachsedown and not self.liftAxisAutoDown then
			self.Liftachsedown = false;
			liftachseEvent.sendEvent(self, self.Liftachsedown);
		else
			self.Liftachsedown = true;
			liftachseEvent.sendEvent(self, self.Liftachsedown);
		end;
	end;
	if self.liftAxisAutoDown then
		self.Liftachsedown = false;
		liftachseEvent.sendEvent(self, self.Liftachsedown);
	end;

end;

function liftachse:updateTick(dt)
	if self.Liftachsedown ~= nil then	
		if self.Liftachsedown then
			local x, y, z = getTranslation(self.supportTrans1.node);
			local trans = {x,y,z};
			local newTrans = Utils.getMovedLimitedValues(trans, self.supportTrans1.maxRot, self.supportTrans1.minRot, 3, self.supportTrans1.rotTime, dt, true);
			setTranslation(self.supportTrans1.node, unpack(newTrans));
			if math.abs(self.supportTrans1.minRot[1]-newTrans[1]) < 0.001 then
				local x, y, z = getRotation(self.achse.node);
				local rot = {x,y,z};
				local newRot = Utils.getMovedLimitedValues(rot, self.achse.maxRot, self.achse.minRot, 3, self.achse.rotTime, dt, true);
				setRotation(self.achse.node, unpack(newRot));
			end;
		else
			local x, y, z = getRotation(self.achse.node);
			local rot = {x,y,z};
			local newRot = Utils.getMovedLimitedValues(rot, self.achse.maxRot, self.achse.minRot, 3, self.achse.rotTime, dt, false);
			setRotation(self.achse.node, unpack(newRot));
			if math.abs(self.achse.maxRot[1]-newRot[1]) < 0.001 then
				local x, y, z = getTranslation(self.supportTrans1.node);
				local trans = {x,y,z};
				local newTrans = Utils.getMovedLimitedValues(trans, self.supportTrans1.maxRot, self.supportTrans1.minRot, 3, self.supportTrans1.rotTime, dt, false);
				setTranslation(self.supportTrans1.node, unpack(newTrans));
			end;
		end;
		for i, jointDesc in pairs(self.componentJoints) do
			setJointFrame(self.componentJoints[i].jointIndex, 0, self.componentJoints[i].jointNode);
		end;
	end;
end;

function liftachse:draw()
	if self.Liftachsedown then
		g_currentMission:addExtraPrintText(string.format(InputBinding.getKeyNamesOfDigitalAction(InputBinding.liftachse))..":                      " .." Liftachse Senken");
	else
		g_currentMission:addExtraPrintText(string.format(InputBinding.getKeyNamesOfDigitalAction(InputBinding.liftachse))..":                      " .." Liftachse Heben");
	end;
end;
function liftachse:readStream(streamId, connection)
	self.Liftachsedown = streamReadBool(streamId);
end;

function liftachse:writeStream(streamId, connection)
	streamWriteBool(streamId, self.Liftachsedown);
end;
function liftachse:mouseEvent(posX, posY, isDown, isUp, button)
end;

function liftachse:delete()
end;

function liftachse:onDetach()
	if not self.Liftachsedown then
		self.Liftachsedown = not self.Liftachsedown;
		liftachseEvent.sendEvent(self, self.Liftachsedown);
	end;
end;