--
-- Lights - HardPoint
--
-- @author:    	Xentro (Marcus@Xentro.se)
-- @website:	www.Xentro.se
-- @history:	v1.0 - 2015-01-06 - Initial implementation
-- 

Lights = {};

function Lights:prerequisitesPresent(specializations)
	return true;
end;

function Lights:load(xmlFile, parentClassObj)
	local mirrorNodes = Lights.loadXML(self, xmlFile, "mirrors", "mirror");
	if mirrorNodes ~= nil then
		self.mirrors = {};
		self.mirrors.nodes = mirrorNodes
		self.mirrors["LAST"] = -1;
	end;
	
	local turnRightNodes = Lights.loadXML(self, xmlFile, "turnSignal", "right");
	local turnLeftNodes = Lights.loadXML(self, xmlFile, "turnSignal", "left");
	if turnRightNodes ~= nil and turnLeftNodes ~= nil then
		self.turnSignal = {};
		self.turnSignal["right"] = {};
		self.turnSignal["right"].lastUpdate = false;
		self.turnSignal["right"].nodes = turnRightNodes;
		self.turnSignal["left"] = {};
		self.turnSignal["left"].lastUpdate = false;
		self.turnSignal["left"].nodes = turnLeftNodes;
	end;
	
	local lightNodes = Lights.loadXML(self, xmlFile, "lights", "light");
	if lightNodes ~= nil then
		self.light = {};
		self.light.lastUpdate = -1;
		self.light.nodes = lightNodes;
	end;
	
	local beaconNodes = Lights.loadXML(self, xmlFile, "beacons", "beacon");
	if beaconNodes ~= nil then
		self.beacons = beaconNodes;
	end;
end;

function Lights.loadXML(self, xmlFile, tag, tag2)
	local entry;
	i = 0;
	while true do
		local key = string.format("vehicle.Lights." .. tag .. "." .. tag2 .. "(%d)", i);
		if not hasXMLProperty(xmlFile, key) then break; end;
		
		local node = Utils.indexToObject(self.components, getXMLString(xmlFile, key .. "#node"));
		if node ~= nil then
			if entry == nil then
				entry = {};
			end;
			
			if tag2 == "beacon" then
				local rotatorNode = Utils.indexToObject(self.components, getXMLString(xmlFile, key .. "#rotator"));
				local speed = Utils.getNoNil(getXMLFloat(xmlFile, key .. "#speed"), 0.015);
				
				setVisibility(node, false);
				
				if rotatorNode ~= nil then
					table.insert(entry, {node = node, rotator = rotatorNode, speed = speed, active = false});
				end;
			else
				table.insert(entry, node);
			end;
		end;
		
		i = i + 1;
	end;
	
	return entry;
end;

function Lights:delete()
end;

function Lights:readStream(streamId, connection)
end;

function Lights:writeStream(streamId, connection)
end;

function Lights:update(dt)
end;

function Lights:updateTick(dt)
	local v = self.vehicle;
	
	if self.isClient then
		if self.mirrors ~= nil then
			if v.camIndex ~= self.mirrors["LAST"] then
				local cam = v.cameras[v.camIndex];
				
				if cam ~= nil then
					for _, node in ipairs(self.mirrors.nodes) do
						setVisibility(node, cam.useMirror and v.isEntered);
					end;
				end;
				
				self.mirrors["LAST"] = v.camIndex;
			else
				if v.forceLightsMirrorChange then
					self.mirrors["LAST"] = -1;
					v.forceLightsMirrorChange = false;
				end;
			end;
		end;
	end;
	
	if self.light ~= nil then
		if v.lightsTypesMask ~= self.light.lastUpdate then
			for _, node in ipairs(self.light.nodes) do
				setVisibility(node, v.lightsTypesMask > 0);
			end;
			
			self.light.lastUpdate = v.lightsTypesMask;
		end;
	end;
	
	if self.turnSignal ~= nil then
		for i, k in pairs(self.turnSignal) do
			local visR = getVisibility(v.turnSignals[i][1]);
			
			if k.lastUpdate ~= visR then
				for _, node in ipairs(k.nodes) do
					setVisibility(node, visR);
				end;
				
				k.lastUpdate = visR;
			end;
		end;
	end;
	
	if self.beacons ~= nil then		
		for _, b in ipairs(self.beacons) do
			if b.active ~= v.beaconLightsActive then
				setVisibility(b.node, v.beaconLightsActive);
				b.active = v.beaconLightsActive;
			end;
			
			if v.beaconLightsActive then
				rotate(b.node, 0, b.speed * dt, 0);
				rotate(b.rotator, 0, b.speed * dt, 0);
			end;
		end;
	end;
end;

function Lights:draw()
end;