-- Henly20 (Ls_uk Modteam)
-- 20.11.2010
-- > www.LS-UK.info
-- >Modificação By Alemão
-- 14/02/2012


footbrake5000 = {};

function footbrake5000.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Motorized, specializations);
end;

function footbrake5000:load(xmlFile)
    
	local rotationPartNodeacelerador2 = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.acelerador2#index"));
    if rotationPartNodeacelerador2 ~= nil then
        self.rotationPartacelerador2 = {};
        self.rotationPartacelerador2.node = rotationPartNodeacelerador2;

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

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

        self.rotationPartacelerador2.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.acelerador2#rotTime"), 2)*1000;
        self.rotationPartacelerador2.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.acelerador2#touchRotLimit"), 10));
    end;
    
    local rotationPartNodefreio = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.freio#index"));
    if rotationPartNodefreio ~= nil then
        self.rotationPartfreio = {};
        self.rotationPartfreio.node = rotationPartNodefreio;

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

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

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

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

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

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

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

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

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

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

        self.rotationPartcontagiro.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.contagiro#rotTime"), 2)*1000;
        self.rotationPartcontagiro.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.contagiro#touchRotLimit"), 10));
    end;
	local rotationPartNodereduzida = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.reduzida#index"));
    if rotationPartNodereduzida ~= nil then
        self.rotationPartreduzida = {};
        self.rotationPartreduzida.node = rotationPartNodereduzida;

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

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

        self.rotationPartreduzida.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.reduzida#rotTime"), 2)*1000;
        self.rotationPartreduzida.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.reduzida#touchRotLimit"), 10));
    end;
	local rotationPartNodeembreagem = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.embreagem#index"));
    if rotationPartNodeembreagem ~= nil then
        self.rotationPartembreagem = {};
        self.rotationPartembreagem.node = rotationPartNodeembreagem;

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

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

        self.rotationPartembreagem.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.embreagem#rotTime"), 2)*1000;
        self.rotationPartembreagem.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.embreagem#touchRotLimit"), 10));
    end;
	local rotationPartNodehidraulico = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.hidraulico#index"));
    if rotationPartNodehidraulico ~= nil then
        self.rotationParthidraulico = {};
        self.rotationParthidraulico.node = rotationPartNodehidraulico;

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

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

        self.rotationParthidraulico.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.hidraulico#rotTime"), 2)*1000;
        self.rotationParthidraulico.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.hidraulico#touchRotLimit"), 10));
    end;
	local rotationPartNodehidraulico2 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.hidraulico2#index"));
    if rotationPartNodehidraulico2 ~= nil then
        self.rotationParthidraulico2 = {};
        self.rotationParthidraulico2.node = rotationPartNodehidraulico2;

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

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

        self.rotationParthidraulico2.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.hidraulico2#rotTime"), 2)*1000;
        self.rotationParthidraulico2.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.hidraulico2#touchRotLimit"), 10));
    end;
	local rotationPartNodehidraulico3 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.hidraulico3#index"));
    if rotationPartNodehidraulico3 ~= nil then
        self.rotationParthidraulico3 = {};
        self.rotationParthidraulico3.node = rotationPartNodehidraulico3;

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

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

        self.rotationParthidraulico3.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.hidraulico3#rotTime"), 2)*1000;
        self.rotationParthidraulico3.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.hidraulico3#touchRotLimit"), 10));
    end;
	local rotationPartNodeacelerador1 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.acelerador1#index"));
    if rotationPartNodeacelerador1 ~= nil then
        self.rotationPartacelerador1 = {};
        self.rotationPartacelerador1.node = rotationPartNodeacelerador1;
		
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.acelerador1#minRot"));
        self.rotationPartacelerador1.minRot = {};
        self.rotationPartacelerador1.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotationPartacelerador1.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotationPartacelerador1.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

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

        self.rotationPartacelerador1.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.acelerador1#rotTime"), 2)*2000;
        self.rotationPartacelerador1.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.acelerador1#touchRotLimit"), 10));
    end;
	
	self.rotationMaxembreagemActive = false;
    self.braking = false; 
	
end;

function footbrake5000:keyEvent(unicode, sym, modifier, isDown)
 	if self.isEntered then 
		if sym == Input.KEY_home then
			self.rotationMaxreduzida = isDown;
		end;	
		if sym == Input.KEY_end then
			self.rotationMinreduzida = isDown;
		end;
		if isDown and sym == Input.KEY_x   then
			self.rotationMaxhidraulico2 = not self.rotationMaxhidraulico2;
		end;
		if isDown and sym == Input.KEY_v   then
			self.rotationMaxhidraulico = not self.rotationMaxhidraulico;
		end;
		if isDown and sym == Input.KEY_v   then
			self.rotationMaxhidraulico3 = not self.rotationMaxhidraulico3;
		end;
	end;
end;

function footbrake5000:update(dt)
        
		local maxhandthrottle = 1000;
			if self.rotationPartacelerador2 ~= nil then
				local x, y, z = getRotation(self.rotationPartacelerador2.node);
				y = ((self.rotationPartacelerador2.maxRot[2] - self.rotationPartacelerador2.minRot[2]) / maxhandthrottle) * (self.motor.lastMotorRpm/2) + self.rotationPartacelerador2.minRot[2];
				setRotation(self.rotationPartacelerador2.node, x, y ,z);
			end;
		
        if self.rotationPartfreio ~= nil then
		local x, y, z = getRotation(self.rotationPartfreio.node);
		local rot = {x,y,z};
		local newRot = Utils.getMovedLimitedValues(rot, self.rotationPartfreio.maxRot, self.rotationPartfreio.minRot, 3, self.rotationPartfreio.rotTime, dt, not self.rotationMaxfreio);
		setRotation(self.rotationPartfreio.node, unpack(newRot));
	end;
	    if self.isEntered then
		if Input.isKeyPressed(Input.KEY_s) then
			self.rotationMaxfreio = true;
		else
			self.rotationMaxfreio = false;
		end;
	end;
	
		if self.rotationPartchavepartida ~= nil then
		local x, y, z = getRotation(self.rotationPartchavepartida.node);
		local rot = {x,y,z};
		local newRot = Utils.getMovedLimitedValues(rot, self.rotationPartchavepartida.maxRot, self.rotationPartchavepartida.minRot, 3, self.rotationPartchavepartida.rotTime,dt, not self.rotationMaxchavepartida);
		setRotation(self.rotationPartchavepartida.node, unpack(newRot));
	    end;
		if self.isMotorStarted then
		self.rotationMaxchavepartida = true;
	else
		self.rotationMaxchavepartida = false;
end;
	
		inputAxisY = InputBinding.getAnalogInputAxis(InputBinding.AXIS_MOVE_FORWARD_VEHICLE);

        if InputBinding.isAxisZero(inputAxisY) then
        inputAxisY = InputBinding.getDigitalInputAxis(InputBinding.AXIS_MOVE_FORWARD_VEHICLE);
        end;
		
		if self.isEntered ~= nil then

		if (self.movingDirection*self.lastSpeed*(-inputAxisY)) < -0.001 then		
			self.braking = true;
			self.rotationMaxembreagemActive = true;
		else
			self.braking = false;
			self.rotationMaxembreagemActive = false;
		end;
    end;
	
		local maxrpmneedle = 900;
			if self.rotationPartcontagiro ~= nil then
				local x, y, z = getRotation(self.rotationPartcontagiro.node);
				y = ((self.rotationPartcontagiro.maxRot[2] - self.rotationPartcontagiro.minRot[2]) / maxrpmneedle) * (self.motor.lastMotorRpm/2.2) + self.rotationPartcontagiro.minRot[2];
				setRotation(self.rotationPartcontagiro.node, x, y ,z);
			end;
		if self.rotationPartreduzida ~= nil and (self.rotationMaxreduzida or self.rotationMinreduzida) then
			local x, y, z = getRotation(self.rotationPartreduzida.node);
			local rot = {x,y,z};
			local newRot = Utils.getMovedLimitedValues(rot, self.rotationPartreduzida.maxRot, self.rotationPartreduzida.minRot, 3, self.rotationPartreduzida.rotTime, dt, not self.rotationMaxreduzida);
			setRotation(self.rotationPartreduzida.node, unpack(newRot));
		end;
		if self.rotationPartembreagem ~= nil then
			local x, y, z = getRotation(self.rotationPartembreagem.node);
			local rot = {x,y,z};
			local newRot = Utils.getMovedLimitedValues(rot, self.rotationPartembreagem.maxRot, self.rotationPartembreagem.minRot, 3, self.rotationPartembreagem.rotTime, dt, not self.rotationMaxembreagemActive);
			setRotation(self.rotationPartembreagem.node, unpack(newRot));
		end;
		if self.rotationParthidraulico ~= nil then
			local x, y, z = getRotation(self.rotationParthidraulico.node);
			local rot = {x,y,z};
			local newRot = Utils.getMovedLimitedValues(rot, self.rotationParthidraulico.maxRot, self.rotationParthidraulico.minRot, 3, self.rotationParthidraulico.rotTime, dt, not self.rotationMaxhidraulico);
			setRotation(self.rotationParthidraulico.node, unpack(newRot));
		end;
        if self.rotationParthidraulico2 ~= nil then
			local x, y, z = getRotation(self.rotationParthidraulico2.node);
			local rot = {x,y,z};
			local newRot = Utils.getMovedLimitedValues(rot, self.rotationParthidraulico2.maxRot, self.rotationParthidraulico2.minRot, 3, self.rotationParthidraulico2.rotTime, dt, not self.rotationMaxhidraulico2);
			setRotation(self.rotationParthidraulico2.node, unpack(newRot));
		end;
        if self.rotationParthidraulico3 ~= nil then
			local x, y, z = getRotation(self.rotationParthidraulico3.node);
			local rot = {x,y,z};
			local newRot = Utils.getMovedLimitedValues(rot, self.rotationParthidraulico3.maxRot, self.rotationParthidraulico3.minRot, 3, self.rotationParthidraulico3.rotTime, dt, not self.rotationMaxhidraulico3);
			setRotation(self.rotationParthidraulico3.node, unpack(newRot));
		end;		
		local maxhandthrottle = 2000;
			if self.rotationPartacelerador1 ~= nil then
				local x, y, z = getRotation(self.rotationPartacelerador1.node);
				y = ((self.rotationPartacelerador1.maxRot[2] - self.rotationPartacelerador1.minRot[2]) / maxhandthrottle) * (self.motor.lastMotorRpm/2) + self.rotationPartacelerador1.minRot[2];
				setRotation(self.rotationPartacelerador1.node, x, y ,z);
			end;
end;

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

function footbrake5000:draw() 
end;

function footbrake5000:onEnter()
  
end;

function footbrake5000:onLeave()
 
end;

function footbrake5000:delete()

end;


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