--
-- moto
--
-- M@D Author  werik
-- M@D date  22.02.2016


moto = {}

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

function moto:load(xmlFile)

        self.JointLock = false;
-- voda

local rotationPartNodevoda1 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.voda1#index"));
    if rotationPartNodevoda1 ~= nil then
        self.rotationPartvoda1 = {};
        self.rotationPartvoda1.node = rotationPartNodevoda1;

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

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

        self.rotationPartvoda1.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.voda1#rotTime"), 2)*1000;
        self.rotationPartvoda1.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.voda1#touchRotLimit"), 10));
    end;
local rotationPartNodevoda2 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.voda2#index"));
    if rotationPartNodevoda2 ~= nil then
        self.rotationPartvoda2 = {};
        self.rotationPartvoda2.node = rotationPartNodevoda2;

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

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

        self.rotationPartvoda2.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.voda2#rotTime"), 2)*1000;
        self.rotationPartvoda2.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.voda2#touchRotLimit"), 10));
    end;
local rotationPartNodevoda3 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.voda3#index"));
    if rotationPartNodevoda3 ~= nil then
        self.rotationPartvoda3 = {};
        self.rotationPartvoda3.node = rotationPartNodevoda3;

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

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

        self.rotationPartvoda3.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.voda3#rotTime"), 2)*1000;
        self.rotationPartvoda3.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.voda3#touchRotLimit"), 10));
    end;
local rotationPartNodevoda4 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.voda4#index"));
    if rotationPartNodevoda4 ~= nil then
        self.rotationPartvoda4 = {};
        self.rotationPartvoda4.node = rotationPartNodevoda4;

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

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

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

      self.quadro1OFF = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.quadro1OFF#index"));
      self.quadro1OFFActive = false;

-- schetchik

        self.crankshaft1Node = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.crankshaft1#index"));
        self.crankshaft2Node = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.crankshaft2#index"));
        self.crankshaft3Node = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.crankshaft3#index"));


      self.mirr1 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.mirr1#index"));
      self.mirr1Active = false;
      self.mirr2 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.mirr2#index"));
      self.mirr2Active = false;

-- animations

 self.enteredanimation = getXMLString(xmlFile, "vehicle.enteredanimation#animationName");
 self.enteredanimationSpeed = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.enteredanimation#speed"), 1);
 self.startanimation = getXMLString(xmlFile, "vehicle.startanimation#animationName");
 self.startanimationSpeed = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.startanimation#speed"), 1);
 self.attachanimation = getXMLString(xmlFile, "vehicle.attachanimation#animationName");
 self.attachanimationSpeed = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.attachanimation#speed"), 1);
 self.enteredanimation2 = getXMLString(xmlFile, "vehicle.enteredanimation2#animationName");
 self.enteredanimation2Speed = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.enteredanimation2#speed"), 1);
 self.enteredanimation3 = getXMLString(xmlFile, "vehicle.enteredanimation3#animationName");
 self.enteredanimation3Speed = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.enteredanimation3#speed"), 1);
 self.animation2 = getXMLString(xmlFile, "vehicle.animation2#animationName");
 self.animation2Speed = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.animation2#speed"), 1);
                      self.Anim2 = false;

	self.movingFlag = self:getNextDirtyFlag();
end;
      
function moto:delete()    
end;

function moto:mouseEvent(posX, posY, isDown, isUp, button)
end;
	  
function moto:keyEvent(unicode, sym, modifier, isDown)
end;

function moto:readUpdateStream(streamId, timestamp, connection)
	if connection:getIsServer() then
		self.Anim2 = streamReadBool(streamId);
	end;
end;

function moto:writeUpdateStream(streamId, connection, dirtyMask)
	if not connection:getIsServer() then
		streamWriteBool(streamId, self.Anim2);
	end;
end;

function moto:update(dt)

 if self.JointLock or self.lastSpeed * 3850 > 2 then
-- if self.lastSpeed * 3850 > 5 then
self.rotationMaxvoda1 = true;
self.rotationMaxvoda2 = false;
self.rotationMaxvoda3 = true;
self.rotationMaxvoda4 = false;
         else
self.rotationMaxvoda1 = false;
self.rotationMaxvoda2 = true;
self.rotationMaxvoda3 = false;
self.rotationMaxvoda4 = true;
end;

if self.lastSpeed * 3850 < 0.5 then
            setVisibility(self.quadro1OFF, self.quadro1OFFActive);
            self.quadro1OFFActive = true;
         else
            setVisibility(self.quadro1OFF, self.quadro1OFFActive);
            self.quadro1OFFActive = false;
   end;

	if g_currentMission.environment.dayTime > 23*3600000 or g_currentMission.environment.dayTime < 4.5*3600000 then
--        if g_currentMission.environment.currentHour > 21 or  g_currentMission.environment.currentHour < 6 then
	setVisibility(self.mirr1, self.mirr1Active);
	self.mirr1Active = true;
	setVisibility(self.mirr2, self.mirr2Active);
	self.mirr2Active = true;
                       else
             setVisibility(self.mirr1, self.mirr1Active);
             self.mirr1Active = false;
             setVisibility(self.mirr2, self.mirr2Active);
             self.mirr2Active = false;
   end;

    if self.enteredanimation ~= nil and self.playAnimation ~= nil then
 if self.isEntered or self.isMotorStarted then
	        self:playAnimation(self.enteredanimation, self.enteredanimationSpeed, self:getAnimationTime(self.enteredanimation), true);
else
	        self:playAnimation(self.enteredanimation, -self.enteredanimationSpeed, self:getAnimationTime(self.enteredanimation), true);
	    end;
     end;

    if self.enteredanimation2 ~= nil and self.playAnimation ~= nil then
-- if self.JointLock or self.isEntered then
 if self.JointLock or self.isEntered then
	        self:playAnimation(self.enteredanimation2, self.enteredanimation2Speed, self:getAnimationTime(self.enteredanimation2), true);
 else
	        self:playAnimation(self.enteredanimation2, -self.enteredanimation2Speed, self:getAnimationTime(self.enteredanimation2), true);
	    end;
     end;

    if self.enteredanimation3 ~= nil and self.playAnimation ~= nil then
 if self.isEntered then
	        self:playAnimation(self.enteredanimation3, self.enteredanimation3Speed, self:getAnimationTime(self.enteredanimation3), true);
 else
	        self:playAnimation(self.enteredanimation3, -self.enteredanimation3Speed, self:getAnimationTime(self.enteredanimation3), true);
	    end;
     end;

    if self.startanimation ~= nil and self.playAnimation ~= nil then
if self.isMotorStarted then
	        self:playAnimation(self.startanimation, self.startanimationSpeed, self:getAnimationTime(self.startanimation), true);
else
	        self:playAnimation(self.startanimation, -self.startanimationSpeed, self:getAnimationTime(self.startanimation), true);
	    end;
     end;

	 -- voda

if self.rotationMaxvoda1 == true then
if self.rotationPartvoda1 ~= nil then
		   local x, y, z = getRotation(self.rotationPartvoda1.node);
		   local rot = {x,y,z};
		   local newRot = Utils.getMovedLimitedValues(rot, self.rotationPartvoda1.maxRot, self.rotationPartvoda1.minRot, 3, self.rotationPartvoda1.rotTime, dt, not self.rotationMaxvoda1);
		   setRotation(self.rotationPartvoda1.node, unpack(newRot));
       end;
   end;
if self.rotationMaxvoda2 == true then
if self.rotationPartvoda2 ~= nil then
		   local x, y, z = getRotation(self.rotationPartvoda2.node);
		   local rot = {x,y,z};
		   local newRot = Utils.getMovedLimitedValues(rot, self.rotationPartvoda2.maxRot, self.rotationPartvoda2.minRot, 3, self.rotationPartvoda2.rotTime, dt, not self.rotationMaxvoda2);
		   setRotation(self.rotationPartvoda2.node, unpack(newRot));
       end;
   end;
if self.rotationMaxvoda3 == true then
if self.rotationPartvoda3 ~= nil then
		   local x, y, z = getRotation(self.rotationPartvoda3.node);
		   local rot = {x,y,z};
		   local newRot = Utils.getMovedLimitedValues(rot, self.rotationPartvoda3.maxRot, self.rotationPartvoda3.minRot, 3, self.rotationPartvoda3.rotTime, dt, not self.rotationMaxvoda3);
		   setRotation(self.rotationPartvoda3.node, unpack(newRot));
       end;
   end;
if self.rotationMaxvoda4 == true then
if self.rotationPartvoda4 ~= nil then
		   local x, y, z = getRotation(self.rotationPartvoda4.node);
		   local rot = {x,y,z};
		   local newRot = Utils.getMovedLimitedValues(rot, self.rotationPartvoda4.maxRot, self.rotationPartvoda4.minRot, 3, self.rotationPartvoda4.rotTime, dt, not self.rotationMaxvoda4);
		   setRotation(self.rotationPartvoda4.node, unpack(newRot));
       end;
   end;
   
   -- schetchik

if self.crankshaft1Node ~= nil and (self.movingDirection > 0 or self.movingDirection < 0) then
         	rotate(self.crankshaft1Node, self.lastSpeed*0.33, 0, 0);
    	end;
if self.crankshaft2Node ~= nil and (self.movingDirection > 0 or self.movingDirection < 0) then
         	rotate(self.crankshaft2Node, self.lastSpeed*0.033, 0, 0);
    	end;
if self.crankshaft3Node ~= nil and (self.movingDirection > 0 or self.movingDirection < 0) then
         	rotate(self.crankshaft3Node, self.lastSpeed*0.0033, 0, 0);
    	end;

           if self:getIsActiveForInput() then
                  if self.animation2 ~= nil then
		if InputBinding.hasEvent(InputBinding.Animated2) then
			if self.isServer then
				self.Anim2 = not self.Anim2;
				self:raiseDirtyFlags(self.movingFlag);
				else
				g_client:getServerConnection():sendEvent(motoEvent:new(1, self));
	        end;
               end;
        end;
end;

    if self.animation2 ~= nil and self.playAnimation ~= nil then
        if self.Anim2 then
	        self:playAnimation(self.animation2, self.animation2Speed, self:getAnimationTime(self.animation2), true);
else
	        self:playAnimation(self.animation2, -self.animation2Speed, self:getAnimationTime(self.animation2), true);
	    end;
     end;
end;

function moto:draw()
           if self:getIsActiveForInput() then
    if self.isClient then
			if not self.Anim2 then
					g_currentMission:addHelpButtonText(g_i18n:getText("playAnim2"), InputBinding.Animated2);
				else
					g_currentMission:addHelpButtonText(g_i18n:getText("notPlayAnim2"), InputBinding.Animated2);
				end;
         end;
         end;
end;

function moto:validateAttacherJoint(implement, jointDesc, dt)
    return true;
end;

function moto:attachImplement(implement)

	local jointType = implement.object.attacherJoint.jointType;
	local jointIndex = implement.jointDescIndex;

    if self.attachanimation ~= nil and self.playAnimation ~= nil then
	if jointType == Vehicle.JOINTTYPE_MANUREBARREL then
		if jointIndex == 1 then
	        self:playAnimation(self.attachanimation, self.attachanimationSpeed, self:getAnimationTime(self.attachanimation), true);
                      self.JointLock = true;
    else
	        self:playAnimation(self.attachanimation, -self.attachanimationSpeed, self:getAnimationTime(self.attachanimation), true);
                      self.JointLock = false;
	 end;
   end;
  end;
end;

function moto:detachImplement(implementIndex)

	local implement = self.attachedImplements[implementIndex];
	local jointIndex = implement.jointDescIndex;
    if self.attachanimation ~= nil and self.playAnimation ~= nil then
	if jointIndex == 1 then
	        self:playAnimation(self.attachanimation, -self.attachanimationSpeed, self:getAnimationTime(self.attachanimation), true);
                      self.JointLock = false;
    else
	        self:playAnimation(self.attachanimation, self.attachanimationSpeed, self:getAnimationTime(self.attachanimation), true);
                      self.JointLock = true;
	end;
  end;
end;

motoEvent = {};
motoEvent_mt = Class(motoEvent, Event);

InitEventClass(motoEvent, "motoEvent");

function motoEvent:emptyNew()
	local self = Event:new(motoEvent_mt);
	print("emptyNew");
    return self;
end;

function motoEvent:new(numKey, object)
	local self = motoEvent:emptyNew();
	self.numKey = numKey;
	self.object = object;
	return self;
end;

function motoEvent:readStream(streamId, connection)
	local numKey = streamReadInt8(streamId);
	local id = streamReadInt32(streamId);
    local object = networkGetObject(id);
	if object ~= nil then
		if numKey == 1 then object.Anim2 = not object.Anim2; end;
	end;
end;

function motoEvent:writeStream(streamId, connection)
	streamWriteInt8(streamId, self.numKey);
	streamWriteInt32(streamId, networkGetObjectId(self.object));
end;