--
-- Maehwerk
-- Specialization for mower
-- Spezialisierung für Maehwerke
-- @author  Geri-G
-- @date 13/06/2010
-- @HomePage LS-ModSource.com
--

eurohit810n = {};

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

function eurohit810n:load(xmlFile)
	--setJointRotationLimit(self.componentJoints[3].jointIndex, 0, true,math.rad(20) , math.rad(-1.08));
	self.componentJoints[3].oldRotLimit = {0};


	self.attacherVehicleJoint = nil;
	
	
end;

function eurohit810n:delete()
end;

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

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

function eurohit810n:update(dt)
end;
function eurohit810n:updateTick(dt)
	if self:getIsActive() then
		--self:setHydraulicDirection();
		--if self:getIsActiveForInput() then
			--if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA2) then
				--self.turnRoot = not self.turnRoot;
			--end;
		--end;
		
	
	if self.attacherVehicleJoint ~= nil then
		local joint = self.componentJoints[3];
		local newRot = Utils.getMovedLimitedValues(joint.oldRotLimit, {0}, {math.rad(21)}, 1, self.attacherVehicleJoint.moveTime , dt, self.attacherVehicleJoint.moveDown);
		if math.abs(newRot[1]) ~= 0 then
			setJointRotationLimit(joint.jointIndex, 1, true, -newRot[1] , newRot[1]);
		for _, part in pairs(self.movingTools) do
			part.isDirty = true;
	    end; 
		for _, part in pairs(self.movingParts) do
			part.isDirty = true;
		end;	
		else
			setJointRotationLimit(joint.jointIndex, 1, true, 0, 0);	
		end;
		joint.oldRotLimit = newRot;
	end;
	
		if self.mowerIsAttached then
			for i=1, table.getn(self.attacherVehicle.attachedImplements) do
				if self.attacherVehicle.attachedImplements[i].object == self then			
					local index = self.attacherVehicle.attachedImplements[i].jointDescIndex;
					self.attacherVehicleJoint = self.attacherVehicle.attacherJoints[index];	
					--[[
			local t = createTransformGroup("hi");
			link(getParent(self.attacherVehicleJoint.rotationNode),t);

			local ax, ay, az = getWorldTranslation(t);
			local bx, by, bz = getWorldTranslation(self.attacherJoint.node);
			by = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, bx, 0, bz)+0.5;
			
			local x, y, z = worldDirectionToLocal(getParent(t), bx-ax, by-ay, bz-az);
			setDirection(t, x, y, z, 0, -1, 0);
			
			local x,y,z = getRotation(t);
			delete(t);
			print(string.format("%0.3f %0.3f %0.3f",x,y,z));
			
					if self.attacherVehicleJoint.maxRot ~= nil then	
						self.Copy.maxRot = self.attacherVehicleJoint.maxRot;
						self.Copy.minRot = self.attacherVehicleJoint.minRot;
						
						self.attacherVehicleJoint.maxRot[1] = math.rad(x);
						--self.attacherVehicleJoint.minRot[1] = math.rad(13);
					end;
					if self.attacherVehicleJoint.maxRot2 ~= nil then
						self.Copy.maxRot2 = self.attacherVehicleJoint.maxRot2;
						self.Copy.minRot2 = self.attacherVehicleJoint.minRot2;
						
						self.attacherVehicleJoint.maxRot2[1] = math.rad(-x);
						--self.attacherVehicleJoint.minRot2[1] = math.rad(-10);
					end;--]]
					break;				
				end;
			end;
			self.mowerIsAttached = false;
		end;
	end;

end;

function eurohit810n:draw()
end;

function eurohit810n:onAttach(attacherVehicle)
	self.mowerIsAttached = true;
end;
function eurohit810n:onDetach()
	self.attacherVehicleJoint = nil;
end;

