--
-- Blocirator components
-- Specialization for Blocirator components
--
-- @author  	dim-dim
-- @version 	v1.0
-- @date  		16/11/11

-- @history:
-- VaderstadCarrier
-- Specialization for Vaderstad Carrier cultivators.
-- @uthor : Bayn - www.tsforum3.com
-- D@te : 09.09.2010
--
-- This script has been made under "OpenSource rules". 
-- That means you can use it without asking authorization, but also without forgetting to quote the author's name.


blocirator = {};

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

function blocirator:load(xmlFile)

    self.controlpath = Utils.getFilename("Textures/blocon.png", self.baseDirectory);
    self.bloconPoxX = 0.0154;
    self.bloconPoxY = 1-0.77;
    self.bloconWidth = 0.2;
    self.bloconHeight = 0.2;
    self.blocon = Overlay:new("hudControlpath", self.controlpath, self.bloconPoxX, self.bloconPoxY, self.bloconWidth, self.bloconHeight);
	
    self.controlpath1 = Utils.getFilename("Textures/blocoff.png", self.baseDirectory);
    self.blocoffPoxX = 0.0154;
    self.blocoffPoxY = 1-0.77;
    self.blocoffWidth = 0.2;
    self.blocoffHeight = 0.2;
    self.blocoff = Overlay:new("hudControlpath1", self.controlpath1, self.blocoffPoxX, self.blocoffPoxY, self.blocoffWidth, self.blocoffHeight);	
	
	local WhePrXNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.WhePr#index"));
    if WhePrXNode ~= nil then
        self.WhePr = {};
        self.WhePr.node = WhePrXNode;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.WhePr#minRot"));
        self.WhePr.minRot = {};
        self.WhePr.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.WhePr.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.WhePr.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

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

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

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

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

function blocirator:delete()

end;

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

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

function blocirator:update(dt)
    if self.attacherVehicle then
		if self.isActive then
			if InputBinding.hasEvent(InputBinding.TRANSPORT_WHEEL) then
				self.Frame = not self.Frame;				
			end;
			if  self.Frame then
				self.WhePrMax = true;
				self.WhePrMin = false;
				self.WheLevMax = true;
				self.WheLevMin = false;	
				setJointRotationLimit(self.componentJoints[1].jointIndex, 1, true, math.rad(-180), math.rad(180));	
				setJointRotationLimit(self.componentJoints[2].jointIndex, 1, true, math.rad(-180), math.rad(180));			
				else
				self.WhePrMax = false;
				self.WhePrMin = true;
				self.WheLevMax = false;
				self.WheLevMin = true;				
				setJointRotationLimit(self.componentJoints[1].jointIndex, 1, true, math.rad(-0.5), math.rad(0.5));	
				setJointRotationLimit(self.componentJoints[2].jointIndex, 1, true, math.rad(-0.5), math.rad(0.5));				
			end;		
			setJointFrame(self.componentJoints[1].jointIndex, 1,self.componentJoints[1].jointNode);	
			setJointFrame(self.componentJoints[2].jointIndex, 1,self.componentJoints[2].jointNode);			
			local doRotate = self.WhePrMax or self.WhePrMin
			if self.WhePr ~= nil and doRotate then
				local x, y, z = getRotation(self.componentJoints[1].jointNode);
				local rot = {x,y,z};
				local newRot = Utils.getMovedLimitedValues(rot, self.WhePr.maxRot, self.WhePr.minRot, 3, self.WhePr.rotTime, dt, not self.WhePrMax);
				setRotation(self.componentJoints[1].jointNode,unpack(newRot));
			end;
			local doRotate = self.WheLevMax or self.WheLevMin
			if self.WheLev ~= nil and doRotate then
				local x, y, z = getRotation(self.componentJoints[2].jointNode);
				local rot = {x,y,z};
				local newRot = Utils.getMovedLimitedValues(rot, self.WheLev.maxRot, self.WheLev.minRot, 3, self.WheLev.rotTime, dt, not self.WheLevMax);
				setRotation(self.componentJoints[2].jointNode,unpack(newRot));
			end;
		end;
	end;	
end;

function blocirator:draw()
	if self.Frame then
		g_currentMission:addHelpButtonText(g_i18n:getText("TransportWheelOff"), InputBinding.TRANSPORT_WHEEL);
	else
		g_currentMission:addHelpButtonText(g_i18n:getText("TransportWheelOn"), InputBinding.TRANSPORT_WHEEL);
	end;
	if  self.Frame then
			if self.blocon ~= nil then
				self.blocon:render();
			end;		
		else
			if self.blocoff ~= nil then
				self.blocoff:render();
			end;				
	end;	
end;

function blocirator:onDetach()

end;

function blocirator:onAttach()
end; 
function blocirator:onLeave()

end;

function blocirator:onActivate()
end;

function blocirator:onDeactivate()

end;

function blocirator:CarrierWorkingTime(diffTime)

end; 