--[[
DayTimeRunningLights

Specialization for day time running lights on an vehicle

Author:		Ifko[nator]
Datum:		09.02.2020
Version:	v1.0

History:	v1.0 @ 09.02.2020 - initial implementation in FS 19
			-------------------------------------------------------------------------------------------------------------------------------------------------------------
]]

DayTimeRunningLights = {};

function DayTimeRunningLights.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Lights, specializations); 
end;

function DayTimeRunningLights.registerEventListeners(vehicleType)
	local functionNames = {
        "onPreLoad",
		"onLoad",
		"onUpdate"
	};

	for _, functionName in ipairs(functionNames) do
		SpecializationUtil.registerEventListener(vehicleType, functionName, DayTimeRunningLights);
	end;
end;

function DayTimeRunningLights:onPreLoad(savegame)
    self.setLightsTypesMask = Utils.prependedFunction(self.setLightsTypesMask, DayTimeRunningLights.setLightsTypesMask);
end;

function DayTimeRunningLights:onLoad(savegame)
    if FlieglTransportPackUtil == nil then
        return;
    end;

    DayTimeRunningLights.debugPriority = FlieglTransportPackUtil.getDebugPriority(self.xmlFile, "vehicle.lights.daytimeRunningLights#debugPriority");
    
    local specDayTimeRunningLights = FlieglTransportPackUtil.getSpecByName(self, "dayTimeRunningLights");

    specDayTimeRunningLights.daytimeRunningLights = {};
    specDayTimeRunningLights.lightsAreOn = false;

    FlieglTransportPackUtil.printDebug("", DayTimeRunningLights.debugPriority, false, "");
	FlieglTransportPackUtil.printDebug("-----------------------------------------------Debug from the DayTimeRunningLights.lua Start------------------------------------------------", DayTimeRunningLights.debugPriority, false, "");

    local daytimeRunningLightNumber = 0;
        
    while true do
        local daytimeRunningLightKey = "vehicle.lights.daytimeRunningLights.daytimeRunningLight(" .. tostring(daytimeRunningLightNumber) .. ")";

        if not hasXMLProperty(self.xmlFile, daytimeRunningLightKey) then
            FlieglTransportPackUtil.printDebug("", 1, false, "");
            FlieglTransportPackUtil.printDebug("-----------------------------------------------Debug from the DayTimeRunningLights.lua end--------------------------------------------------", DayTimeRunningLights.debugPriority, false, "");
        
            break;
        end;

        local daytimeRunningLight = {};
       
        daytimeRunningLight.node = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, daytimeRunningLightKey .. "#node"), self.i3dMappings);
        
        if daytimeRunningLight.node ~= nil then
            daytimeRunningLight.intensity = Utils.getNoNil(getXMLInt(self.xmlFile, daytimeRunningLightKey .. "#intensity"), 25);
				
            setVisibility(daytimeRunningLight.node, false);
            
            table.insert(specDayTimeRunningLights.daytimeRunningLights, daytimeRunningLight);

            FlieglTransportPackUtil.printDebug("Load day time running light number '" .. daytimeRunningLightNumber + 1 .. "' successfully.", DayTimeRunningLights.debugPriority, true, "DayTimeRunningLights");
        else
            FlieglTransportPackUtil.printError("Missing the 'node' for the day time running light number '" .. daytimeRunningLightNumber + 1 .. "'! This day time running light will be ignored!", false, false, "DayTimeRunningLights");
        end;
    
        daytimeRunningLightNumber = daytimeRunningLightNumber + 1;
    end;
end;

function DayTimeRunningLights:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
    if self:getIsActive() then
        local specDayTimeRunningLights = FlieglTransportPackUtil.getSpecByName(self, "dayTimeRunningLights");
        
        for _, daytimeRunningLight in pairs(specDayTimeRunningLights.daytimeRunningLights) do
            local _, y, z, w = getShaderParameter(daytimeRunningLight.node, "lightControl");

            setShaderParameter(daytimeRunningLight.node, "lightControl", math.max(1 * daytimeRunningLight.intensity, 0), y, z, w, false);
            setVisibility(daytimeRunningLight.node, self:getIsMotorStarted() and not specDayTimeRunningLights.lightsAreOn);
        end;
    end;
end;

function DayTimeRunningLights:setLightsTypesMask(lightsTypesMask, force, noEventSend)
    local specDayTimeRunningLights = FlieglTransportPackUtil.getSpecByName(self, "dayTimeRunningLights");

    specDayTimeRunningLights.lightsAreOn = bitAND(lightsTypesMask, 1) ~= 0;
end;