-- @author  Rival
-- @date  28/05/2020

-- Do not edit without my permission


advancedLights = {}

function advancedLights.prerequisitesPresent(specializations)
    return true
end

function advancedLights.registerOverwrittenFunctions(vehicleType)
	SpecializationUtil.registerOverwrittenFunction(vehicleType, "loadDashboardGroupFromXML", advancedLights.loadDashboardGroupFromXML)
	SpecializationUtil.registerOverwrittenFunction(vehicleType, "getIsDashboardGroupActive", advancedLights.getIsDashboardGroupActive)
end

function advancedLights.registerEventListeners(vehicleType)
	SpecializationUtil.registerEventListener(vehicleType, "onLoad", advancedLights)
end

function advancedLights:onLoad(savegame)
	if self.loadDashboardsFromXML ~= nil then
		self:loadDashboardsFromXML(self.xmlFile, "vehicle.lights.dashboards", {valueTypeToLoad = "lightState2", valueObject = self.spec_lights, valueFunc = "lightsTypesMask", additionalAttributesFunc = Lights.dashboardLightAttributes, stateFunc = advancedLights.dashboardLightStateDisplay})
	end
end

function advancedLights:loadDashboardGroupFromXML(superFunc, xmlFile, key, group)
    if not superFunc(self, xmlFile, key, group) then
        return false
    end
    group.attachedTrailers = getXMLInt(xmlFile, key .. "#attachedTrailers")
    return true
end

function advancedLights:getIsDashboardGroupActive(superFunc, group)
	if group.attachedTrailers ~= nil then
		local attachedImplementsWithLights = 0
		for k,v in pairs(self.spec_attacherJoints.attachedImplements) do
			if v.object.spec_lights ~= nil and v.object.spec_lights.activeTurnLightSetup ~= nil and #v.object.spec_lights.activeTurnLightSetup.turnLightsLeft > 0 and #v.object.spec_lights.activeTurnLightSetup.turnLightsRight > 0 then
				attachedImplementsWithLights = attachedImplementsWithLights + 1
			end
			if v.object.spec_attacherJoints ~= nil and v.object.spec_attacherJoints.attachedImplements ~= nil and #v.object.spec_attacherJoints.attachedImplements > 0 then
				for k2,v2 in pairs(v.object.spec_attacherJoints.attachedImplements) do
					if v2.object.spec_lights ~= nil and v2.object.spec_lights.activeTurnLightSetup ~= nil and #v2.object.spec_lights.activeTurnLightSetup.turnLightsLeft > 0 and #v2.object.spec_lights.activeTurnLightSetup.turnLightsRight > 0 then
						attachedImplementsWithLights = attachedImplementsWithLights + 1
					end
					if v2.object.spec_attacherJoints ~= nil and v2.object.spec_attacherJoints.attachedImplements ~= nil and #v2.object.spec_attacherJoints.attachedImplements > 0 then
						for k3,v3 in pairs(v.object.spec_attacherJoints.attachedImplements) do
							if v3.object.spec_lights ~= nil and v3.object.spec_lights.activeTurnLightSetup ~= nil and #v3.object.spec_lights.activeTurnLightSetup.turnLightsLeft > 0 and #v3.object.spec_lights.activeTurnLightSetup.turnLightsRight > 0 then
								attachedImplementsWithLights = attachedImplementsWithLights + 1
							end
						end
					end
				end
			end
		end
		if group.attachedTrailers > attachedImplementsWithLights then
			return false
		end
	end
    return superFunc(self, group)
end

function advancedLights.dashboardLightStateDisplay(self, dashboard, newValue, minValue, maxValue, isActive)
    local lightsTypesMask = self.spec_lights.lightsTypesMask
    local lightIsActive = false
    if dashboard.lightTypes ~= nil then
        for _, lightType in pairs(dashboard.lightTypes) do
            if bitAND(lightsTypesMask, 2^lightType) ~= 0 then
                lightIsActive = true
                break
            end
        end
    end
    if lightIsActive then
        for _, excludedLightType in pairs(dashboard.excludedLightTypes) do
            if bitAND(lightsTypesMask, 2^excludedLightType) ~= 0 then
                lightIsActive = false
                break
            end
        end
    end
	setShaderParameter(dashboard.node, "lightControl", (lightIsActive and isActive) and dashboard.intensity or dashboard.idleValue, isActive and 1 or 0, 0, 0, false)
end
