--
-- @modified Mev01
-- @date  11/04/09

Saemaschine = {};

function Saemaschine.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(SowingMachine, specializations);
end;

function Saemaschine:load(xmlFile)
    self.mainRollerNode  = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.roller#Main"));
    self.leftRollerNode  = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.roller#Left"));
	self.rightRollerNode = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.roller#Right"));
    self.backRollerNode = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.roller#Back"));
	self.rollerRotationScale = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.roller#rotScale"), 1);

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

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

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

        self.rotationPartRight.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotationPartRight#rotTime"), 2)*1000;
        self.rotationPartRight.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotationPartRight#touchRotLimit"), 10));
    end;

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

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

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

        self.rotationPartLeft.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotationPartLeft#rotTime"), 2)*1000;
        self.rotationPartLeft.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotationPartLeft#touchRotLimit"), 10));
    end;

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

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

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

        self.rotationPartBack.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotationPartBack#rotTime"), 2)*1000;
        self.rotationPartBack.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotationPartBack#touchRotLimit"), 10));
    end;


    self.hasGroundContact = false;
    self.isExpandedRight = false;
    self.isExpandedLeft = false;
	self.isExpandedBack = false;
    self.rotationMaxRight = true;
    self.rotationMaxLeft = true;
	self.rotationMaxBack = true;
    self.rotationSpeed = 0;

end;

function Saemaschine:delete()
end;

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

function Saemaschine:keyEvent(unicode, sym, modifier, isDown)

	 if isDown and sym == Input.KEY_n then
		self.rotationMaxRight = not self.rotationMaxRight;
            self.rotationMaxLeft = not self.rotationMaxLeft;
			self.rotationMaxBack = not self.rotationMaxBack;

		elseif isDown and sym == Input.KEY_l then
            self.rotationMaxBack = not self.rotationMaxBack;
			end;
end;

function Saemaschine:update(dt)

    if self.isAttached then
        self.isExpandedRight = false;
        if self.rotationPartRight ~= nil then
            local x, y, z = getRotation(self.rotationPartRight.node);
            local minRot = self.rotationPartRight.minRot;
            local eps = self.rotationPartRight.touchRotLimit;

            if math.abs(x-minRot[1]) < eps and math.abs(y-minRot[2]) < eps and math.abs(z-minRot[3]) < eps then
                self.isExpandedRight = true;
            end;
        end;

        self.isExpandedLeft = false;
        if self.rotationPartLeft ~= nil then
            local x, y, z = getRotation(self.rotationPartLeft.node);
            local minRot = self.rotationPartLeft.minRot;
            local eps = self.rotationPartLeft.touchRotLimit;

            if math.abs(x-minRot[1]) < eps and math.abs(y-minRot[2]) < eps and math.abs(z-minRot[3]) < eps then
                self.isExpandedLeft = true;
            end;
        end;

		self.isExpandedBack = false;
        if self.rotationPartBack ~= nil then
            local x, y, z = getRotation(self.rotationPartBack.node);
            local minRot = self.rotationPartBack.minRot;
            local eps = self.rotationPartBack.touchRotLimit;

            if math.abs(x-minRot[1]) < eps and math.abs(y-minRot[2]) < eps and math.abs(z-minRot[3]) < eps then
                self.isExpandedBack = true;
            end;
        end;

    end;


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

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

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


          if self.hasGroundContact == true then

					self.rotationSpeed = self.attacherVehicle.lastSpeed * self.attacherVehicle.movingDirection;
                    rotate(self.mainRollerNode,   self.rollerRotationScale * self.rotationSpeed ,0,0);
					rotate(self.leftRollerNode,   self.rollerRotationScale * self.rotationSpeed ,0,0);
					rotate(self.rightRollerNode,  self.rollerRotationScale * self.rotationSpeed ,0,0);
                    rotate(self.backRollerNode,  self.rollerRotationScale * self.rotationSpeed ,0,0);
				end;

end;

function Saemaschine:draw()

     if self.attacherVehicle then
        g_currentMission:addExtraPrintText("Taste N : Sämaschine klappen ");
	end;

end;

function Saemaschine:onAttach()
end;

function Saemaschine:onDetach()
     self.hasGroundContact = false;
end;
