-- Allows using:
-- designMaterial4Configurations up to designMaterial49Configurations and 
-- design9Configurations up to design49Configurations

MoreDesignConfigs = {
  prerequisitesPresent = function()
    return true
  end
}

function MoreDesignConfigs.initSpecialization()
  -- Increase max configuration options to 256
  ConfigurationUtil.SEND_NUM_BITS = 8

  local schema = Vehicle.xmlSchema
  for i = 9, 20 do
    g_configurationManager:addConfigurationType(string.format("uudesign%d", i), g_i18n:getText("configuration_design"), nil, nil, ConfigurationUtil.getConfigColorSingleItemLoad, ConfigurationUtil.getConfigColorPostLoad, ConfigurationUtil.SELECTOR_MULTIOPTION)
		ConfigurationUtil.registerColorConfigurationXMLPaths(schema, string.format("uudesign%d", i))
		ObjectChangeUtil.registerObjectChangeXMLPaths(schema, string.format("vehicle.uudesign%dConfigurations.uudesign%dConfiguration(?)", i, i))
	end

  schema:setXMLSpecializationType("BaseMaterial")
	
  for j = 4, 20 do
    g_configurationManager:addConfigurationType(string.format("uudesignMaterial%d", j), g_i18n:getText("configuration_designColor"), nil, nil, ConfigurationUtil.getConfigColorSingleItemLoad, ConfigurationUtil.getConfigColorPostLoad, ConfigurationUtil.SELECTOR_COLOR)
    BaseMaterial.registerBaseMaterialConfigurationsXMLPaths(schema, string.format("uudesignMaterial%d", j))
    ConfigurationUtil.registerColorConfigurationXMLPaths(schema, string.format("uudesignMaterial%d", j))
    ObjectChangeUtil.registerObjectChangeXMLPaths(schema, string.format("vehicle.uudesignMaterial%dConfigurations.uudesignMaterial%dConfiguration(?)", j, j))
  end
  
	schema:setXMLSpecializationType()
end

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

function MoreDesignConfigs:onLoad(savegame)
  for i = 9, 20 do
    local name = string.format("uudesign%d", i)

    if self.configurations[name] ~= nil then
      ObjectChangeUtil.updateObjectChanges(self.xmlFile, string.format("vehicle.uudesign%dConfigurations.uudesign%dConfiguration", i, i), self.configurations[name], self.components, self)
    end
  end

  for j = 4, 20 do
    local name = string.format("uudesignMaterial%d", j)

    if self.configurations[name] ~= nil then
      self:applyBaseMaterialConfiguration(self.xmlFile, string.format("uudesignMaterial%d", j), self.configurations[name])
      ObjectChangeUtil.updateObjectChanges(self.xmlFile, string.format("vehicle.uudesignMaterial%dConfigurations.uudesignMaterial%dConfiguration", j, j), self.configurations[name], self.components, self)
    end
  end
end