-- init easyAutoLoad
-- put together by alfalfa6945

local autoloadModName = g_currentModName

if g_modIsLoaded["FS19_BestAutoLoad"] then
	local specTypeName = "easyAutoLoader2"
	local className = "easyAutoLoader2"
	local filename = Utils.getFilename("easyAutoLoader2.lua", modDirectory.."Scripts/")
	g_specializationManager:addSpecialization(specTypeName, className, filename)
	local baseType = {"baseAttachable", "baseDrivable"}
	for index, typeName in pairs({"autoloadTrailer", "autoloadTruck"}) do
		local parentName = baseType[index]
		local parent = g_vehicleTypeManager.vehicleTypes[parentName]
		if parent ~= nil then
			g_vehicleTypeManager:addVehicleType(typeName, "Vehicle", parent.filename, nil)
			for _, specName in ipairs(parent.specializationNames) do
				g_vehicleTypeManager:addSpecialization(typeName, specName)
			end
		end
		for _, specName in ipairs({"tensionBelts", "easyAutoLoader2"}) do
			spec = g_specializationManager:getSpecializationByName(specName)
			g_vehicleTypeManager:addSpecialization(typeName, spec.name)
		end
	end
end

Mission00.load = Utils.prependedFunction(Mission00.load, function (mission)
	if g_modIsLoaded["FS19_BestAutoLoad"] then
		local conflict = false
		for typeName, typeEntry in pairs(g_vehicleTypeManager.vehicleTypes) do
			for i = 1, #typeEntry.specializationNames do
				if string.match(typeEntry.specializationNames[i], 'easyAutoLoader2') then
					if typeEntry.specializationNames[i] ~= "FS19_BestAutoLoad.easyAutoLoader2"  then
						conflict = true
						local mod, vehicleTypeName = string.match(typeName, "([^.]+).([^.]+)")
						print("  Warning: vehicleType "..'"'..vehicleTypeName..'"'.." in the mod "..'"'..mod..'"'.." conflicts with FS19_BestAutoLoad")
					end
				end
			end
		end
		if conflict then
			print("  Valid easyAutoLoad vehicleTypes are \"autoloadTrailer\" or \"autoloadTruck\"")
		end
	end
end)