--#####################################################################--
-- VehicleControls														   --
--#####################################################################--
-- @Idee	Marc85													   --
--#####################################################################--
-- @author 	BlackyBPG						www.blackpanthergroup.de   --
--																	   --
-- @date  09/10/2013 (dd/mm/yyyy)									   --
--																	   --
-- Hinweis: Editieren nur nach vorheriger absprache des Authors		   --
--#####################################################################--
vehicleControls = {};
function vehicleControls.prerequisitesPresent(specializations)
    return true;
end;

function vehicleControls:load(xmlFile)
	self.turnSignal =  {};
	local turnIndexStr = getXMLString(xmlFile, "vehicle.vehicleControls.turnSignal#index");
	if turnIndexStr ~= nil then
		local turnIndex = Utils.indexToObject(self.rootNode, turnIndexStr);
		if turnIndex ~= nil and turnIndex ~= 0 then
			self.turnSignal.index = turnIndex;
			self.turnSignal.leftRot = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.vehicleControls.turnSignal#leftRot"),20);
			self.turnSignal.rightRot = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.vehicleControls.turnSignal#rightRot"),-20);
			self.turnSignal.lrRot = 0;
			self.turnSignal.setBack = 0;
		end;
	end;

	self.speeder =  {};
	local speedIndexStr = getXMLString(xmlFile, "vehicle.vehicleControls.speed#index");
	if speedIndexStr ~= nil then
		local speedIndex = Utils.indexToObject(self.rootNode, speedIndexStr);
		if speedIndex ~= nil and speedIndex ~= 0 then
			self.speeder.index = speedIndex;
			self.speeder.maxRot = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.vehicleControls.speed#maxRot"),-242);
			self.speeder.maxSpeed = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.vehicleControls.speed#maxSpeed"),60);
			self.speeder.lastSpeed1 = 0;
			self.speeder.lastSpeed2 = 0;
			self.speeder.lastSpeed3 = 0;
			self.speeder.lastSpeedPos = 1;
		end;
	end;

	self.rpms =  {};
	local rpmIndexStr = getXMLString(xmlFile, "vehicle.vehicleControls.rpm#index");
	if rpmIndexStr ~= nil then
		local rpmIndex = Utils.indexToObject(self.rootNode, rpmIndexStr);
		if rpmIndex ~= nil and rpmIndex ~= 0 then
			self.rpms.index = rpmIndex;
			self.rpms.maxRot = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.vehicleControls.rpm#maxRot"),245);
			self.rpms.maxRPM = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.vehicleControls.rpm#maxRPM"),3000);
			self.rpms.lastRPM1 = 0;
			self.rpms.lastRPM2 = 0;
			self.rpms.lastRPM3 = 0;
			self.rpms.lastRPMPos = 1;
		end;
	end;

	self.oilNeedle =  {};
	local oilIndexStr = getXMLString(xmlFile, "vehicle.vehicleControls.oilLevel#index");
	if oilIndexStr ~= nil then
		local oilIndex = Utils.indexToObject(self.rootNode, oilIndexStr);
		if oilIndex ~= nil and oilIndex ~= 0 then
			self.oilNeedle.index = oilIndex;
			self.oilNeedle.maxRot = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.vehicleControls.oilLevel#maxRot"),70);
		end;
	end;

	self.fuel =  {};
	local fuelIndexStr = getXMLString(xmlFile, "vehicle.vehicleControls.fuel#index");
	if fuelIndexStr ~= nil then
		local fuelIndex = Utils.indexToObject(self.rootNode, fuelIndexStr);
		if fuelIndex ~= nil and fuelIndex ~= 0 then
			self.fuel.index = fuelIndex;
			self.fuel.maxRot = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.vehicleControls.fuel#maxRot"),-134);
			self.fuel.reserve = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.vehicleControls.fuel#reserve"),-19);
			local rlIndex = getXMLString(xmlFile, "vehicle.vehicleControls.fuel#reserveLightIndex");
			if rlIndex ~= nil then
				self.fuel.reserveLight = Utils.indexToObject(self.rootNode, rlIndex);
			end;
		end;
	end;

	self.isSelectable = true;
end;

function vehicleControls:getAllowFillShovel(superFunc, fillType)
	if self.isTurnedOn then
		if superFunc ~= nil then
			return superFunc(self, fillType);
		end;
	end;
	return false;
end;

function vehicleControls:update(dt)
	if self:getIsActive() then
		if self.oilNeedle ~= nil and self.oilNeedle.index ~= nil and self.operatingTime ~= nil and self.nextOilChange ~= nil and self.oilChangeInterval ~= nil then
			local rotStep = self.oilNeedle.maxRot / (self.oilChangeInterval * 600)
			local curRot = rotStep * ((self.oilChangeInterval * 600) - ((self.nextOilChange * 600) - self.operatingTime));
			if not self.isMotorStarted then
				curRot = self.oilNeedle.maxRot;
			end;
			setRotation(self.oilNeedle.index, 0,Utils.degToRad(curRot),0);
		end;
		if self.fuel ~= nil and self.fuel.index ~= nil and self.fuelCapacity ~= nil then
			local curFuel = math.min(self.fuelCapacity, math.max(0, self.fuelFillLevel));
			local curRot = self.fuel.maxRot / self.fuelCapacity * curFuel;
			setRotation(self.fuel.index, 0,Utils.degToRad(curRot),0);
			if self.fuel.reserveLight ~= nil then
				if curRot >= self.fuel.reserve then
					setVisibility(self.fuel.reserveLight, true);
				else
					setVisibility(self.fuel.reserveLight, false);
				end;
			end;
		end;
		if self.speeder ~= nil and self.speeder.index ~= nil then
			local curSpeed = math.min(100, math.max(0, self.lastSpeed*self.speedDisplayScale*3600));
			local curRot = self.speeder.maxRot / self.speeder.maxSpeed * ((curSpeed + self.speeder.lastSpeed1 + self.speeder.lastSpeed2 + self.speeder.lastSpeed3) / 4);
			setRotation(self.speeder.index, 0,Utils.degToRad(curRot),0);
			if self.speeder.lastSpeedPos == 1 then
				self.speeder.lastSpeedPos = 2;
				self.speeder.lastSpeed1 = curSpeed;
			elseif self.speeder.lastSpeedPos == 2 then
				self.speeder.lastSpeedPos = 3;
				self.speeder.lastSpeed2 = curSpeed;
			else
				self.speeder.lastSpeedPos = 1;
				self.speeder.lastSpeed3 = curSpeed;
			end;
		end;

		if self.rpms ~= nil and self.rpms.index ~= nil and self.motor ~= nil then
			local curRPM = self.motor.lastMotorRpm;
			local curRot = self.rpms.maxRot / self.rpms.maxRPM * ((curRPM + self.rpms.lastRPM1 + self.rpms.lastRPM2 + self.rpms.lastRPM3) / 4);
			if not self.isMotorStarted then
				curRot = 0;
			end;
			setRotation(self.rpms.index, 0,Utils.degToRad(curRot),0);
			if self.rpms.lastRPMPos == 1 then
				self.rpms.lastRPMPos = 2;
				self.rpms.lastRPM1 = curRPM;
			elseif self.rpms.lastRPMPos == 2 then
				self.rpms.lastRPMPos = 3;
				self.rpms.lastRPM2 = curRPM;
			else
				self.rpms.lastRPMPos = 1;
				self.rpms.lastRPM3 = curRPM;
			end;
		end;
	end;

	if self.B3 ~= nil and self.B3.dirLeft ~= nil and self.B3.dirRight ~= nil then
		if self.turnSignal ~= nil and self.turnSignal.index ~= nil then
			if self.B3.dirLeft[1].a and not self.B3.wl then
				if self.turnSignal.lrRot < self.turnSignal.leftRot then
					self.turnSignal.lrRot = self.turnSignal.lrRot + (dt * 0.05);
					setRotation(self.turnSignal.index,0,Utils.degToRad(self.turnSignal.lrRot),0);
				end;
			elseif self.B3.dirRight[1].a and not self.B3.wl then
				if self.turnSignal.lrRot > self.turnSignal.rightRot then
					self.turnSignal.lrRot = self.turnSignal.lrRot - (dt * 0.05);
					setRotation(self.turnSignal.index,0,Utils.degToRad(self.turnSignal.lrRot),0);
				end;
			else
				if self.turnSignal.lrRot < 0 then
					self.turnSignal.setBack = 1;
					self.turnSignal.lrRot = self.turnSignal.lrRot + (dt * 0.05);
					setRotation(self.turnSignal.index,0,Utils.degToRad(self.turnSignal.lrRot),0);
				elseif self.turnSignal.lrRot > 0 then
					self.turnSignal.setBack = 2;
					self.turnSignal.lrRot = self.turnSignal.lrRot - (dt * 0.05);
					setRotation(self.turnSignal.index,0,Utils.degToRad(self.turnSignal.lrRot),0);
				end;
				if (self.turnSignal.lrRot < 0 and self.turnSignal.setBack == 2) or (self.turnSignal.lrRot > 0 and self.turnSignal.setBack == 1) then
					self.turnSignal.setBack = 0;
					setRotation(self.turnSignal.index,0,0,0);
				end;
			end;
		end;
	end;
end;

function vehicleControls:draw()
end;

function vehicleControls:delete()
end;

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

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

function vehicleControls:readStream(streamId, connection)
end;

function vehicleControls:writeStream(streamId, connection)
end;

