package com.novoda.downloadmanager.lib;

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

import com.novoda.notils.string.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class DownloadsRepository {

    private final ContentResolver contentResolver;
    private final DownloadInfoCreator downloadInfoCreator;
    private final DownloadsUriProvider downloadsUriProvider;

    public DownloadsRepository(ContentResolver contentResolver, DownloadInfoCreator downloadInfoCreator, DownloadsUriProvider downloadsUriProvider) {
        this.contentResolver = contentResolver;
        this.downloadInfoCreator = downloadInfoCreator;
        this.downloadsUriProvider = downloadsUriProvider;
    }

    public List<FileDownloadInfo> getAllDownloads() {
        Cursor downloadsCursor = contentResolver.query(
                downloadsUriProvider.getAllDownloadsUri(),
                null,
                null,
                null,
                DownloadContract.Batches._ID + " ASC");

        try {
            List<FileDownloadInfo> downloads = new ArrayList<>();
            FileDownloadInfo.Reader reader = new FileDownloadInfo.Reader(contentResolver, downloadsCursor);

            while (downloadsCursor.moveToNext()) {
                downloads.add(downloadInfoCreator.create(reader));
            }

            return downloads;
        } finally {
            downloadsCursor.close();
        }
    }

    public FileDownloadInfo getDownloadFor(long id) {
        Uri uri = ContentUris.withAppendedId(downloadsUriProvider.getAllDownloadsUri(), id);
        Cursor downloadsCursor = contentResolver.query(uri, null, null, null, null);
        try {
            downloadsCursor.moveToFirst();
            FileDownloadInfo.Reader reader = new FileDownloadInfo.Reader(contentResolver, downloadsCursor);
            return downloadInfoCreator.create(reader);
        } finally {
            downloadsCursor.close();
        }
    }

    public FileDownloadInfo.ControlStatus getDownloadInfoControlStatusFor(long id) {
        FileDownloadInfo.ControlStatus.Reader reader = new FileDownloadInfo.ControlStatus.Reader(contentResolver, downloadsUriProvider);
        return downloadInfoCreator.create(reader, id);
    }

    public void moveDownloadsStatusTo(List<Long> ids, int status) {
        ContentValues values = new ContentValues(1);
        values.put(DownloadContract.Downloads.COLUMN_STATUS, status);

        String where = DownloadContract.Downloads._ID + " IN (" + getPlaceHoldersOf(ids.size()) + ")";
        String[] selectionArgs = getStringArrayFrom(ids.toArray());
        contentResolver.update(downloadsUriProvider.getAllDownloadsUri(), values, where, selectionArgs);
    }

    private String getPlaceHoldersOf(int size) {
        String[] questionMarks = new String[size];
        for (int i = 0; i < size; i++) {
            questionMarks[i] = "?";
        }

        return StringUtils.join(Arrays.asList(questionMarks), ", ");
    }

    private String[] getStringArrayFrom(Object[] objects) {
        int length = objects.length;
        String[] stringArray = new String[length];
        for (int i = 0; i < length; i++) {
            stringArray[i] = String.valueOf(objects[i]);
        }
        return stringArray;
    }

    interface DownloadInfoCreator {
        FileDownloadInfo create(FileDownloadInfo.Reader reader);

        FileDownloadInfo.ControlStatus create(FileDownloadInfo.ControlStatus.Reader reader, long id);
    }

}

@@ -241,49 +241,65 @@
 ing.
-String
+Query
 Utils;%0A
-%0A
 import 
-java.util.ArrayList;
+com.novoda.notils.string.StringUtils;%0A
 %0Aimp
@@ -317,17 +317,20 @@
 il.Array
-s
+List
 ;%0Aimport
@@ -2653,33 +2653,56 @@
 IN (%22 + 
-get
+QueryUtils.createSelection
 Place
-H
+h
 oldersOf
 (ids.siz
@@ -2693,16 +2693,20 @@
 oldersOf
+Size
 (ids.siz
@@ -2750,19 +2750,30 @@
 nArgs = 
-get
+StringUtils.to
 StringAr
@@ -2775,20 +2775,16 @@
 ingArray
-From
 (ids.toA
@@ -2908,565 +2908,8 @@
  %7D%0A%0A
-    private String getPlaceHoldersOf(int size) %7B%0A        String%5B%5D questionMarks = new String%5Bsize%5D;%0A        for (int i = 0; i %3C size; i++) %7B%0A            questionMarks%5Bi%5D = %22?%22;%0A        %7D%0A%0A        return StringUtils.join(Arrays.asList(questionMarks), %22, %22);%0A    %7D%0A%0A    private String%5B%5D getStringArrayFrom(Object%5B%5D objects) %7B%0A        int length = objects.length;%0A        String%5B%5D stringArray = new String%5Blength%5D;%0A        for (int i = 0; i %3C length; i++) %7B%0A            stringArray%5Bi%5D = String.valueOf(objects%5Bi%5D);%0A        %7D%0A        return stringArray;%0A    %7D%0A%0A
     

Replace methods in DownloadRepository with same from NoTils