package com.novoda.downloadmanager.lib;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;

import com.novoda.notils.string.QueryUtils;
import com.novoda.notils.string.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

class BatchDeletionRepository {

    private final DownloadDeleter downloadDeleter;
    private ContentResolver resolver;
    private final Uri downloadsUri;

    BatchDeletionRepository(DownloadDeleter downloadDeleter, ContentResolver resolver, DownloadsUriProvider downloadsUriProvider) {
        this.downloadDeleter = downloadDeleter;
        this.resolver = resolver;
        this.downloadsUri = downloadsUriProvider.getAllDownloadsUri();
    }

    void deleteMarkedBatchesFor(Collection<FileDownloadInfo> downloads) {
        List<Long> batchIdsToDelete = findBatchIdsToDelete();
        if (batchIdsToDelete.isEmpty()) {
            return;
        }

        deleteFileAndDownloadsFor(downloads, batchIdsToDelete);
        deleteBatchesFromDatabase(batchIdsToDelete);
    }

    private List<Long> findBatchIdsToDelete() {
        Cursor cursor = queryForBatchesToDelete();

        try {
            return marshallToBatchIds(cursor);
        } finally {
            cursor.close();
        }
    }

    private Cursor queryForBatchesToDelete() {
        String[] projection = {DownloadContract.Batches._ID};
        String selection = DownloadContract.Batches.COLUMN_DELETED + " = ?";
        String[] selectionArgs = {"1"};

        Cursor cursor = resolver.query(downloadsUri, projection, selection, selectionArgs, null);

        if (cursor == null) {
            throw new BatchDeletionException();
        }

        return cursor;
    }

    private List<Long> marshallToBatchIds(Cursor cursor) {
        List<Long> batchIdsToDelete = new ArrayList<>();

        while (cursor.moveToNext()) {
            long id = cursor.getLong(0);
            batchIdsToDelete.add(id);
        }

        return batchIdsToDelete;
    }

    private void deleteFileAndDownloadsFor(Collection<FileDownloadInfo> downloads, List<Long> batchIdsToDelete) {
        for (FileDownloadInfo download : downloads) {
            if (batchIdsToDelete.contains(download.getBatchId())) {
                downloadDeleter.deleteFileAndDatabaseRow(download);
            }
        }
    }

    private void deleteBatchesFromDatabase(List<Long> batchIdsToDelete) {
        String selectionPlaceholders = QueryUtils.createSelectionPlaceholdersOfSize(batchIdsToDelete.size());
        String where = DownloadContract.Batches._ID + " IN (" + selectionPlaceholders + ")";
        String[] selectionArgs = StringUtils.toStringArray(batchIdsToDelete.toArray());
        resolver.delete(downloadsUri, where, selectionArgs);
    }

    private static class BatchDeletionException extends RuntimeException {
        public BatchDeletionException() {
            super("Failed to query for batches to delete");
        }
    }
}

@@ -109,32 +109,8 @@
 sor;
-%0Aimport android.net.Uri;
 %0A%0Aim
@@ -418,19 +418,36 @@
 e final 
-Uri
+DownloadsUriProvider
  downloa
@@ -451,16 +451,24 @@
 loadsUri
+Provider
 ;%0A%0A    B
@@ -701,16 +701,24 @@
 loadsUri
+Provider
  = downl
@@ -736,29 +736,8 @@
 ider
-.getAllDownloadsUri()
 ;%0A  
@@ -1578,16 +1578,40 @@
 loadsUri
+Provider.getBatchesUri()
 , projec
@@ -2750,24 +2750,24 @@
 toArray());%0A
-
         reso
@@ -2790,16 +2790,40 @@
 loadsUri
+Provider.getBatchesUri()
 , where,

use correct uri in BatchDeletionRepository