package com.novoda.downloadmanager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

final class DownloadBatchFactory {

    private DownloadBatchFactory() {
        // non instantiable factory class
    }

    static DownloadBatch newInstance(Batch batch,
                                     FileOperations fileOperations,
                                     DownloadsBatchPersistence downloadsBatchPersistence,
                                     DownloadsFilePersistence downloadsFilePersistence,
                                     CallbackThrottle callbackThrottle) {
        DownloadBatchTitle downloadBatchTitle = DownloadBatchTitleCreator.createFrom(batch);
        Map<DownloadFileId, NetworkAddressAndFilePath> networkAddressAndFileNameById = batch.networkAddressAndFileNameById();
        List<DownloadFile> downloadFiles = new ArrayList<>(networkAddressAndFileNameById.size());
        DownloadBatchId downloadBatchId = batch.getDownloadBatchId();
        long downloadedDateTimeInMillis = System.currentTimeMillis();

        for (Map.Entry<DownloadFileId, NetworkAddressAndFilePath> networkAddressAndFilePathByDownloadId : networkAddressAndFileNameById.entrySet()) {
            NetworkAddressAndFilePath networkAddressAndFilePath = networkAddressAndFilePathByDownloadId.getValue();

            InternalFileSize fileSize = InternalFileSizeCreator.unknownFileSize();

            String fileNetworkAddress = networkAddressAndFilePath.networkAddress();
            FileName fileNameFromNetworkAddress = FileNameExtractor.extractFrom(fileNetworkAddress);

            String relativePath = networkAddressAndFilePath.relativePathToStoreDownload();

            FilePersistenceCreator filePersistenceCreator = fileOperations.filePersistenceCreator();
            FilePersistence filePersistence = filePersistenceCreator.create();

            FilePath filePath = FilePathCreator.create(filePersistence.basePath().path(), relativePath == null || relativePath.isEmpty() ? fileNameFromNetworkAddress.name() : relativePath);
            FileName fileName = FileNameExtractor.extractFrom(filePath.path());

            DownloadFileId downloadFileId = networkAddressAndFilePathByDownloadId.getKey();
            InternalDownloadFileStatus downloadFileStatus = new LiteDownloadFileStatus(
                    downloadBatchId,
                    downloadFileId,
                    InternalDownloadFileStatus.Status.QUEUED,
                    fileSize,
                    filePath
            );

            FileDownloader fileDownloader = fileOperations.fileDownloader();
            FileSizeRequester fileSizeRequester = fileOperations.fileSizeRequester();

            DownloadFile downloadFile = new DownloadFile(
                    downloadBatchId,
                    downloadFileId,
                    fileNetworkAddress,
                    downloadFileStatus,
                    fileName,
                    filePath,
                    fileSize,
                    fileDownloader,
                    fileSizeRequester,
                    filePersistence,
                    downloadsFilePersistence
            );
            downloadFiles.add(downloadFile);
        }

        InternalDownloadBatchStatus liteDownloadBatchStatus = new LiteDownloadBatchStatus(
                downloadBatchId,
                downloadBatchTitle,
                downloadedDateTimeInMillis,
                DownloadBatchStatus.Status.QUEUED
        );

        return new DownloadBatch(
                liteDownloadBatchStatus,
                downloadFiles,
                new HashMap<>(),
                downloadsBatchPersistence,
                callbackThrottle
        );
    }
}

@@ -1518,201 +1518,8 @@
 s();
-%0A            FileName fileNameFromNetworkAddress = FileNameExtractor.extractFrom(fileNetworkAddress);%0A%0A            String relativePath = networkAddressAndFilePath.relativePathToStoreDownload();
 %0A%0A  
@@ -1713,148 +1713,144 @@
     
-FilePath filePath = FilePathCreator.create(filePersistence.basePath().path(), relativePath == null %7C%7C relativePath.isEmpty() ? fileName
+String basePath = filePersistence.basePath().path();%0A            FilePath filePath = FilePathCreator.create(basePath, relativePath
 From
-N
+(n
 etwo
@@ -1862,31 +1862,21 @@
 ress
-.name() : relativ
+AndFil
 ePath)
+)
 ;%0A  
@@ -3535,10 +3535,436 @@
 ;%0A    %7D%0A
+%0A    private static String relativePathFrom(NetworkAddressAndFilePath networkAddressAndFilePath) %7B%0A        String relativePath = networkAddressAndFilePath.relativePathToStoreDownload();%0A        String fileNameFromNetworkAddress = FileNameExtractor.extractFrom(networkAddressAndFilePath.networkAddress()).name();%0A        return relativePath == null %7C%7C relativePath.isEmpty() ? fileNameFromNetworkAddress : relativePath;%0A    %7D%0A%0A
 %7D%0A

Refactor DownloadBatchFactory for readability.