extraLights = {};

function extraLights.prerequisitesPresent(specializations)
    return true;
end;

function extraLights:load(xmlFile)

	self.setLightStatus = SpecializationUtil.callSpecializationsFunction("setLightStatus");

	self.farLights = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.farLights#index"));
	setVisibility(self.farLights, false);
	self.farLights2 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.farLights2#index"));
	setVisibility(self.farLights2, false);
	self.frontWorklights = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.frontWorklights#index"));
	setVisibility(self.frontWorklights, false);
	self.backWorklights = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.backWorklights#index"));
	setVisibility(self.backWorklights, false);
	self.brakelights = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.brakelights#index"));
	setVisibility(self.brakelights, false);
	
	self.animation = {};
    self.animation.animCharSet = 0;
    self.animationEnabled = false;
	self.animation.objects = {};
    local rootNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.blitzlicht#rootNode"));

    if rootNode ~= nil then
		local j =1;
		local parent = getParent(rootNode);
		for i= 1, getNumOfChildren(parent) do
			local index = getChildAt(parent,i-1);
			local animCharSet = getAnimCharacterSet(index);
			if animCharSet ~= 0 then
				self.animation.objects[j] = index;
				j = j+1;
			end;
		end;
		
        self.animation.animCharSet = getAnimCharacterSet(rootNode);
        if self.animation.animCharSet ~= 0 then
            self.animation.clip = getAnimClipIndex(self.animation.animCharSet, getXMLString(xmlFile, "vehicle.blitzlicht#animationClip"));
            if self.animation.clip >= 0 then
                assignAnimTrackClip(self.animation.animCharSet, 0, self.animation.clip);
                self.animation.speedScale = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.blitzlicht#speedScale"), 1);
                setAnimTrackSpeedScale(self.animation.animCharSet, self.animation.clip, self.animation.speedScale);
                setAnimTrackLoopState(self.animation.animCharSet, 0, true);
            end;
        end;
    end;
	
	self.lightCount = 1;
	self.blitzing = false;
	
end;

function extraLights:delete()
end;

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

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

function extraLights:update(dt)
	
	if self:getIsActiveForInput() then
		if InputBinding.hasEvent(InputBinding.FendRedBull936_Flashlights) then
			self.blitzing = not self.blitzing;
		end;
		if InputBinding.hasEvent(InputBinding.TOGGLE_LIGHTS) then
			if not Input.isKeyPressed(Input.KEY_shift) then
				self.lightCount =  self.lightCount+1;
			else
				self.lightCount =  self.lightCount-1;
			end;
			if self.lightCount > 7 then
				self.lightCount = 1;
			elseif self.lightCount < 1 then
				self.lightCount = 7;
			end;
			self:setLightStatus(self.lightCount);
		end;
		if self.lightCount ~= 1 then
			self.lightsActive = true;
		end;
        if not self.animationEnabled and self.blitzing then
			enableAnimTrack(self.animation.animCharSet, 0);
			self.animationEnabled = true;
		end;
        if self.animationEnabled and not self.blitzing then
			setAnimTrackTime(self.animation.animCharSet, 0, 0);
			disableAnimTrack(self.animation.animCharSet, 0);
			self.animationEnabled = false;
			for k,v in pairs(self.animation.objects) do
				setVisibility(v,false);
			end;
		end;
	else
		if self.animationEnabled then
			setAnimTrackTime(self.animation.animCharSet, 0, 0);
			disableAnimTrack(self.animation.animCharSet, 0);
			self.animationEnabled = false;
			self.blitzing = false;
			for k,v in pairs(self.animation.objects) do
				setVisibility(v,false);
			end;
		end;
	end;

	local inputAxisY = InputBinding.getAnalogInputAxis(InputBinding.AXIS_FORWARD);	
    if InputBinding.isAxisZero(inputAxisY) then
        inputAxisY = InputBinding.getDigitalInputAxis(InputBinding.AXIS_FORWARD);
    end;
	if (self.movingDirection*self.lastSpeed*(-inputAxisY)) < -0.0002 then
		setVisibility(self.brakelights, true);
	else
		setVisibility(self.brakelights, false);
	end;

end;

function extraLights:draw()
end;
	
function extraLights:setLightStatus(lightCount)
	local far = false;
	local far2 = false;
	local front = false;
	local back = false;
	if lightCount == 2 then
		far = true;
		far2 = false;
		front = false;
		back = false;
	elseif lightCount == 3 then
		far = ture;
		far2 = true;
		front = false;
		back = false;
	elseif lightCount == 4 then
		far = ture;
		far2 = false;
		front = true;
		back = false;
	elseif lightCount == 5 then
		far = true;
		far2 = false;
		front = true;
		back = ture;
	elseif lightCount == 6 then
		far = true;
		far2 = true;
		front = false;
		back = true;
	elseif lightCount == 7 then
		far = true;
		far2 = false;
		front = false;
		back = true;

	end;
	setVisibility(self.farLights, far);
	setVisibility(self.farLights2, far2);
	setVisibility(self.frontWorklights, front);
	setVisibility(self.backWorklights, back);	
end;

function extraLights:onLeave()
	setVisibility(self.farLights, false);
	setVisibility(self.farLights2, false);
	setVisibility(self.frontWorklights, false);
	setVisibility(self.backWorklights, false);
end;