AdditionalProductionPoints = {
    modDir = g_currentModDirectory
}

local xmlFilename = string.format("%sxml/addToProductionPoint.xml", AdditionalProductionPoints.modDir)
local xml = loadXMLFile("mod_addToProductionPointXMLFile", xmlFilename)

UnloadTrigger.load = Utils.overwrittenFunction(UnloadTrigger.load, function (self, superFunc, components, xmlFile, xmlNode, target, extraAttributes, i3dMappings, ...)
    superFunc(self, components, xmlFile, xmlNode, target, extraAttributes, i3dMappings, ...)
    local i = 0
    while true do
        local path = "addToProductionPoint.productionPoint(" .. i .. ")"
        local attachTo = getXMLString(xml, path .. "#attachTo")
        local attached = g_fillTypeManager:getFillTypeIndexByName(getXMLString(xml, path .. "#fillType"))
        if attachTo == nil then
            break
        end

		if self.fillTypes ~= nil then
			for fillType, _ in pairs(self.fillTypes) do
				local inputName = g_fillTypeManager:getFillTypeNameByIndex(fillType)
				if inputName == attachTo and not self.fillTypes[attached] then
					self.fillTypes[attached] = true
				end
			end
		end
        i = i + 1
    end

	return true
end)

LoadTrigger.load = Utils.overwrittenFunction(LoadTrigger.load, function (self, superFunc, components, xmlFile, xmlNode, i3dMappings, rootNode, ...)
    superFunc(self, components, xmlFile, xmlNode, i3dMappings, rootNode, ...)
    local i = 0
    while true do
        local path = "addToProductionPoint.productionPoint(" .. i .. ")"
        local attachTo = getXMLString(xml, path .. "#attachTo")
        local attached = g_fillTypeManager:getFillTypeIndexByName(getXMLString(xml, path .. "#fillType"))
        if attachTo == nil then
            break
        end

		if self.fillTypes ~= nil then
			for fillType, _ in pairs(self.fillTypes) do
				local inputName = g_fillTypeManager:getFillTypeNameByIndex(fillType)
				if inputName == attachTo and not self.fillTypes[attached] then
					self.fillTypes[attached] = true
				end
			end
		end
        i = i + 1
    end

	return true
end)

ProductionPoint.load = Utils.overwrittenFunction(ProductionPoint.load, function (self, superFunc, components, xmlFile, key, customEnv, i3dMappings, ...)
    superFunc(self, components, xmlFile, key, customEnv, i3dMappings, ...)

    local i = 0
    while true do
        local path = "addToProductionPoint.productionPoint(" .. i .. ")"
        local attachTo = getXMLString(xml, path .. "#attachTo")
        local attached = g_fillTypeManager:getFillTypeIndexByName(getXMLString(xml, path .. "#fillType"))
        local replaceInAnyCase = Utils.getNoNil(getXMLBool(xml, path .. "#replaceInAnyCase"), false)
        if attachTo == nil then
            break
        end

		for p = 1, #self.productions do
			local production = self.productions[p]

			for x = 1, #production.outputs do
				local output = production.outputs[x]
				local outputName = g_fillTypeManager:getFillTypeNameByIndex(output.type)
				if outputName == attachTo and not self.inputFillTypeIds[g_fillTypeManager:getFillTypeIndexByName(attachTo)] or outputName == attachTo and replaceInAnyCase then
					local oldType = output.type
					local oldAmount = output.amount
					local oldSellDirectly = output.sellDirectly
					local oldCapacity = self.storage.capacities[oldType]
	
					output.type = attached

					if not oldSellDirectly then
						self.outputFillTypeIds[oldType] = false
						table.removeElement(self.outputFillTypeIdsArray, oldType)
						self.outputFillTypeIds[output.type] = true
						table.addElement(self.outputFillTypeIdsArray, output.type)
					else
						self.soldFillTypesToPayOut[output.type] = 0
					end

					output.amount = oldAmount
	
					self.storage.fillTypes[oldType] = false
					self.storage.fillTypes[attached] = true
					self.storage.capacities[attached] = oldCapacity
	
					for fillType, _ in pairs(self.storage.fillTypes) do
						table.insert(self.storage.sortedFillTypes, fillType)
					
						self.storage.fillLevels[fillType] = 0
						self.storage.fillLevelsLastSynced[fillType] = 0
						self.storage.fillLevelsLastPublished[fillType] = 0
					end
					
					table.sort(self.storage.sortedFillTypes)

					for _, loadTrigger in pairs(self.loadingStation.loadTriggers) do
						loadTrigger.fillTypes[oldType] = false
						loadTrigger.fillTypes[attached] = true
					end

					self.loadingStation:updateSupportedFillTypes()
				end
			end
	
			for x = 1, #production.inputs do
				local input = production.inputs[x]
				local inputName = g_fillTypeManager:getFillTypeNameByIndex(input.type)
				if inputName == attachTo and not self.storage.fillTypes[attached] and not self.outputFillTypeIds[attached] and not self.outputFillTypeIds[g_fillTypeManager:getFillTypeIndexByName(attachTo)] or inputName == attachTo and replaceInAnyCase then
					-- print(" " .. tostring() .. "!")
					local oldType = input.type
					local oldAmount = input.amount
					local oldCapacity = self.storage.capacities[oldType]

					self.inputFillTypeIds[oldType] = false
					table.removeElement(self.inputFillTypeIdsArray, oldType)
					self.storage.fillTypes[oldType] = false

					input.type = attached
					self.inputFillTypeIds[input.type] = true
					table.addElement(self.inputFillTypeIdsArray, input.type)
					input.amount = oldAmount

					self.storage.fillTypes[attached] = true
					self.storage.capacities[attached] = oldCapacity

					for fillType, _ in pairs(self.storage.fillTypes) do
						table.insert(self.storage.sortedFillTypes, fillType)
				
						self.storage.fillLevels[fillType] = 0
						self.storage.fillLevelsLastSynced[fillType] = 0
						self.storage.fillLevelsLastPublished[fillType] = 0
					end
				
					table.sort(self.storage.sortedFillTypes)

					for _, unloadTrigger in pairs(self.unloadingStation.unloadTriggers) do
						unloadTrigger.fillTypes[oldType] = false
						unloadTrigger.fillTypes[attached] = true
					end

					self.unloadingStation:updateSupportedFillTypes()
-- unloadingStation
					-- for supportedFillType, _ in pairs(self.storage:getSupportedFillTypes()) do
						-- Logging.xmlWarning(xmlFile, "storage fillType '%s' capacity is '%s'", g_fillTypeManager:getFillTypeNameByIndex(supportedFillType), self.storage:getCapacity(supportedFillType))
					-- end
				end
			end
		end
        i = i + 1
    end

    return true
end)