-- by modelleicher
-- 23.04.2019
-- www.ls-modcompany.de

extendedMaterialConfiguration = {};

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


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

function extendedMaterialConfiguration.initSpecialization()
	g_configurationManager:addConfigurationType("extendedMaterial", "extendedMaterial", nil, nil, nil, nil, ConfigurationUtil.SELECTOR_MULTIOPTION)
end


function extendedMaterialConfiguration:onLoad(savegame)
	self.loadColorChange = extendedMaterialConfiguration.loadColorChange;
				
	if self.configurations ~= nil then
		if self.configurations.extendedMaterial ~= nil then -- check if we have extendedMaterial configuration 
			key = "vehicle.extendedMaterialConfigurations.extendedMaterialConfiguration("..(self.configurations.extendedMaterial -1)..")."; -- XML key to the configuration
			
			-- load colorChanges 
			self:loadColorChange(key.."colorChange", true, nil, nil);
			-- load rimColorChanges 
			self:loadColorChange(key.."rimColorChange", nil, true, nil);
			-- load hubColorChanges
			self:loadColorChange(key.."hubColorChange", nil, nil, true);

		end;
	end;

	
end;

function extendedMaterialConfiguration:loadColorChange(key, nodeTrue, isRim, isHub)
	
	local i = 0;
	while true do
		local key1 = key.."("..i..")"; 
		local colorChange = {};
		
		if nodeTrue then -- if nodeTrue is true we have a node-index for our colorChange, we can also have a colorChange Set 
			colorChange.node = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, key1.."#baseNode"), self.i3dMappings);
			
			if colorChange.node == nil then
				colorChange.set = getXMLInt(self.xmlFile, key1.."#set")
			end;
		end;
		
		
		-- material index is confusing naming here, but the index is the colorMat index. e.g. the w value 
		colorChange.materialIndex = Utils.getNoNil(getXMLInt(self.xmlFile, key1.."#materialIndex"), 0);
		colorChange.colorMat = getXMLString(self.xmlFile, key1.."#colorMat");
		if (colorChange.colorMat == "" or colorChange.colorMat == nil) and colorChange.set == nil then
			break; -- break if we don't find a colorMat value 
		end;		
		-- load color data from string, either brand color by string or rgb value 
		local stringValue = getXMLString(self.xmlFile, key1.."#value")
		colorChange.value = g_brandColorManager:getBrandColorByName(stringValue) or {StringUtil.getVectorFromString(stringValue)};
		
		colorChange.share = Utils.getNoNil(getXMLBool(self.xmlFile, key1.."#share"), false);
		
		if nodeTrue then
			if colorChange.set ~= nil then
				local x = 0;
				while true do
					local setKey = "vehicle.extendedMaterialConfigurations.colorChangeSets.colorChangeSet("..(colorChange.set-1)..").colorChange("..x..")"
					local colorMat = getXMLString(self.xmlFile, setKey.."#colorMat");
					local node = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, setKey.."#baseNode"), self.i3dMappings);
					
					if node == nil then
						break;
					end;
					
					setShaderParameter(node, colorMat, colorChange.value[1], colorChange.value[2], colorChange.value[3], colorChange.materialIndex, false)
					x = x+1;
				end;
			else	
				-- if we have a node, set the shaderparameter for the node 
				setShaderParameter(colorChange.node, colorChange.colorMat, colorChange.value[1], colorChange.value[2], colorChange.value[3], colorChange.materialIndex, colorChange.share)
			end;
		end;
		
		if isRim then -- if this is a rimColorChange, go through the wheels/rims 
			for _, wheel in pairs(self.spec_wheels.wheels) do
				if wheel.wheelOuterRim ~= nil then			 
					 setShaderParameter(wheel.wheelOuterRim, colorChange.colorMat, colorChange.value[1], colorChange.value[2], colorChange.value[3], colorChange.materialIndex, colorChange.share)
				end;
				if wheel.wheelInnerRim ~= nil then
					 setShaderParameter(wheel.wheelInnerRim, colorChange.colorMat, colorChange.value[1], colorChange.value[2], colorChange.value[3], colorChange.materialIndex, colorChange.share)
				end;
			end;
		end;
		
		if isHub then -- if this is a hub colorChange, go through the hubs 
			for _, hub in pairs(self.spec_wheels.hubs) do
				if hub.node ~= nil then			 
					 setShaderParameter(hub.node, colorChange.colorMat, colorChange.value[1], colorChange.value[2], colorChange.value[3], colorChange.materialIndex, colorChange.share)
				end;
			end;
		
		end;
		
		i = i+1;
	end;


end;