-- 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, 23 do
    g_configurationManager:addConfigurationType(string.format("arcdesign%d", i), g_i18n:getText("configuration_design"), nil, nil, ConfigurationUtil.getConfigColorSingleItemLoad, ConfigurationUtil.getConfigColorPostLoad, ConfigurationUtil.SELECTOR_MULTIOPTION)
		ConfigurationUtil.registerColorConfigurationXMLPaths(schema, string.format("arcdesign%d", i))
		ObjectChangeUtil.registerObjectChangeXMLPaths(schema, string.format("vehicle.arcdesign%dConfigurations.arcdesign%dConfiguration(?)", i, i))
	end

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

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

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

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

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

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