
---xyzspain 




Lely = {};

function Lely.prerequisitesPresent(specializations)
     return SpecializationUtil.hasSpecialization(Attachable, specializations);
end;

function Lely:load(xmlFile)

-- self.setStateEvent= SpecializationUtil.callSpecializationsFunction("setStateEvent");
	-- desplazamiento de conjuntos
	
		--------------------------
		local rotationNode1 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.rotation1#index"));
    if rotationNode1 ~= nil then
        self.rotation1 = {};
        self.rotation1.node = rotationNode1;
        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 translationNode1 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.translation1#index"));
    if translationNode1 ~= nil then
        self.translation1 = {};
        self.translation1.node = translationNode1;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.translation1#minTrans"));
        self.translation1.minTrans = {};
        self.translation1.minTrans[1] = Utils.getNoNil(x, 0);
        self.translation1.minTrans[2] = Utils.getNoNil(y, 0);
        self.translation1.minTrans[3] = Utils.getNoNil(z, 0);

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

	    self.translation1.transTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.translation1#transTime"), 2)*1000;
      
    end;
	local count = getXMLInt(xmlFile, "vehicle.translationParts#count");
	if count ~= nil then
		self.translationParts = {}
		local translationPart = self.translationParts;
		for i=1, count do
			local varName = string.format("translationPart" .. "%d", i);
			translationPart[varName] = {};
			local translationPartname = string.format("vehicle.translationParts.translationPart" .. "%d", i);
			translationPart[varName].index = Utils.indexToObject(self.components, getXMLString(xmlFile, translationPartname .. "#index"));
			local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, translationPartname .. "#minTrans"));
			translationPart[varName].minTrans = {};
			translationPart[varName].minTrans[1] = Utils.getNoNil(x, 0);
			translationPart[varName].minTrans[2] = Utils.getNoNil(y, 0);
			translationPart[varName].minTrans[3] = Utils.getNoNil(z, 0);
			local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, translationPartname .. "#maxTrans"));
			translationPart[varName].maxTrans = {};
			translationPart[varName].maxTrans[1] = Utils.getNoNil(x, 0);
			translationPart[varName].maxTrans[2] = Utils.getNoNil(y, 0);
			translationPart[varName].maxTrans[3] = Utils.getNoNil(z, 0);

			translationPart[varName].transTime = Utils.getNoNil(getXMLInt(xmlFile, translationPartname .. "#transTime"), 2)*1000;	
			translationPart[varName].touchTransLimit = Utils.getNoNil(getXMLInt(xmlFile, translationPartname .. "#touchTransLimit"), 1)/1000;	
		end;
	end;
  
end;

function Lely:delete()
	
end;



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

function Lely:keyEvent(unicode, sym, modifier, isDown)
self.on=true;
end;



function Lely:getSaveAttributesAndNodes(nodeIdent)
	
end;

function Lely:loadFromAttributesAndNodes(xmlFile, key, resetVehicles)
	
end;

function Lely:update(dt)

	if self:getIsActiveForInput() then
	
		   if InputBinding.hasEvent (InputBinding.SWITCH_IMPLEMENT) then 
		   -- self:setStateEvent("on", not self.on)
			self.on =not self.on
			end;
		if self.on then
	
			
		----------------------------------
				if InputBinding.hasEvent (InputBinding.POWERSHAFT_UP) then
					 -- self:setStateEvent("down", not self.down);
					 self.down =not self.down
				end;
				if InputBinding.hasEvent (InputBinding.POWERSHAFT_DOWN) then
					 -- self:setStateEvent("up", not self.up);
					 self.up =not self.up
				end;
				if InputBinding.hasEvent (InputBinding.POWERSHAFT_LEFT)  then
					 -- self:setStateEvent("left", not self.left);
					 self.left =not self.left
				end;
				if InputBinding.hasEvent (InputBinding.POWERSHAFT_RIGHT) then
					 -- self:setStateEvent("right", not self.right);
					 self.right =not self.right
				end;
				
				if InputBinding.hasEvent (InputBinding.CHANGE_ATTACH) then
					 -- self:setStateEvent("changeAttach", not self.changeAttach);
					 self.changeAttach =not self.changeAttach
				end;
		--------------------------------
		end; ---on
	end;
	
	
	
		if self.changeAttach then
			self.rotation1max= true;
			self.translation1max= true;
		else
			self.rotation1max= false;
			self.translation1max= false;
		end;
	
	
		------------------------------------------------------------------------------

		if self.translation1max ~= nil  then 
			local x, y, z = getTranslation(self.translation1.node); 
			local trans = {x,y,z}; 
			local newTrans = Utils.getMovedLimitedValues(trans, self.translation1.maxTrans, self.translation1.minTrans, 3, self.translation1.transTime, dt, not self.translation1max); 
			setTranslation(self.translation1.node, unpack(newTrans)); 
		end;	

	if self.rotation1max ~= 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.rotation1max);
        setRotation(self.rotation1.node, unpack(newRot));
    end;
			if self.down then
			local translationPart = self.translationParts.translationPart1;
			local trans = {getTranslation(translationPart.index)};
			local newTrans = Utils.getMovedLimitedValues(trans, translationPart.maxTrans, translationPart.minTrans, 3, translationPart.transTime, dt, true);
			setTranslation(translationPart.index, unpack(newTrans));
		
	    end;		
		if self.up then
			local translationPart = self.translationParts.translationPart1;
			local trans = {getTranslation(translationPart.index)};
			local newTrans = Utils.getMovedLimitedValues(trans, translationPart.maxTrans, translationPart.minTrans, 3, translationPart.transTime, dt, false);
			setTranslation(translationPart.index, unpack(newTrans));
			
			
	    end;  		

		if self.left then
			local translationPart = self.translationParts.translationPart2;
			local trans = {getTranslation(translationPart.index)};
			local newTrans = Utils.getMovedLimitedValues(trans, translationPart.maxTrans, translationPart.minTrans, 3, translationPart.transTime, dt, true);
			setTranslation(translationPart.index, unpack(newTrans));
			
			
	    end;		
		if self.right then
			local translationPart = self.translationParts.translationPart2;
			local trans = {getTranslation(translationPart.index)};
			local newTrans = Utils.getMovedLimitedValues(trans, translationPart.maxTrans, translationPart.minTrans, 3, translationPart.transTime, dt, false);
			setTranslation(translationPart.index, unpack(newTrans));
			
			
	    end;  	
	
	-- juntas

	
	

end;




	


function Lely:draw()
	
	
	
end;

function Lely:onAttach(attacherVehicle)
	self.attacherVehicle = attacherVehicle;
	self.on=false;
end;

function Lely:onDetach()
self.on=false;
end;

-- function Lely:setStateEvent(level1, value, noEventSend)
	
    -- if value== true then
      -- value = "true"
    -- elseif value==false then
      -- value = "false"
    -- elseif value ~= "true" and value ~= "false" then
      -- value = string.format("%f", value)
    -- end
	
	-- Lely.sendEvent(self, level1, value, noEventSend);
    	
	-- if value== "true" then
      -- value = true
    -- elseif value=="false" then
      -- value = false
    -- elseif value ~= true and value ~= false then
      -- value = value * 1.0
    -- end
	
    -- self[level1]= value;
-- end;
-- function Lely:readStream(streamId, connection)  
	

	-- self.on = streamReadBool(streamId)
	
	-- self.down = streamReadBool(streamId)
	-- self.up = streamReadBool(streamId)
	-- self.left = streamReadBool(streamId)
	-- self.right = streamReadBool(streamId)
	-- self.changeAttach = streamReadBool(streamId)
-- end;

-- function Lely:writeStream(streamId, connection)
	
	
	-- streamWriteBool(streamId, self.on)
	-- streamWriteBool(streamId, self.down)
	-- streamWriteBool(streamId, self.up)
	-- streamWriteBool(streamId, self.left)
	-- streamWriteBool(streamId, self.right)
	-- streamWriteBool(streamId, self.changeAttach)
-- end;


-- Lely = {};
-- Lely_mt = Class(Lely, Event);

-- InitEventClass(Lely, "Lely");

-- function Lely:emptyNew()  
    -- local self = Event:new(Lely_mt );
    -- self.className="Lely";
    -- return self;
-- end;

-- function Lely:new(object, level1, value) 
    -- self.object = object;
    -- self.level1 = level1;
    -- self.value = value;

    -- return self;
-- end;

-- function Lely:readStream(streamId, connection)
    -- local id = streamReadInt32(streamId);
    -- self.level1 = streamReadString(streamId);
    -- self.value = streamReadString(streamId);
    -- self.object = networkGetObject(id);
    -- self:run(connection);
-- end;

-- function Lely:writeStream(streamId, connection)
    -- streamWriteInt32(streamId, networkGetObjectId(self.object));
    -- streamWriteString(streamId, self.level1 );
    -- streamWriteString(streamId, self.value );
-- end;

-- function Lely:run(connection)
    -- self.object:setStateEvent(self.level1, self.value, true);
    -- if not connection:getIsServer() then  
      -- g_server:broadcastEvent(Lely:new(self.object, self.level1, self.value), nil, connection, self.object);
    -- end;
-- end;

-- function Lely.sendEvent(object, level1, value, noEventSend)
	-- if noEventSend == nil or noEventSend == false then
		-- if g_server ~= nil then 
			-- g_server:broadcastEvent(Lely:new(object, level1, value), nil, nil, object);
		-- else
			-- g_client:getServerConnection():sendEvent(Lely:new(object, level1, value));
		-- end;
	-- end;
-- end;
