package com.example.android.moviebox.utilities;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
import android.util.Log;

import com.example.android.moviebox.data.MoviesContract;

import static com.example.android.moviebox.DetailActivity.FETCH_MOVIE_WITH_ID_LOADER_ID;
import static com.example.android.moviebox.MainActivity.FETCH_ALL_MOVIES_DB_LOADER_ID;


public class FetchFromDbTask implements LoaderManager.LoaderCallbacks<Cursor> {

    private static final String TAG = FetchFromDbTask.class.getSimpleName();
    private FetchMovieFromDbCallback mCallback;
    private Context mContext;
    private String mMovieId;

    public FetchFromDbTask(FetchMovieFromDbCallback callback, Context context) {
        this.mCallback = callback;
        this.mContext = context;
    }

    public FetchFromDbTask(FetchMovieFromDbCallback callback, Context context, String mMovieId) {
        this.mCallback = callback;
        this.mContext = context;
        this.mMovieId = mMovieId;
    }

    public interface FetchMovieFromDbCallback {
        void swapCursor(Cursor newCursor);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {

        final Uri uri;

        switch (loaderId) {
            case FETCH_MOVIE_WITH_ID_LOADER_ID:
                uri = MoviesContract.MoviesEntry.CONTENT_URI.buildUpon()
                        .appendPath(mMovieId)
                        .build();
                break;
            case FETCH_ALL_MOVIES_DB_LOADER_ID:
                uri = MoviesContract.MoviesEntry.CONTENT_URI;
                break;
            default:
                throw new UnsupportedOperationException("Unknow loader id: " + loaderId);
        }

        return new AsyncTaskLoader<Cursor>(mContext) {
            Cursor mMovieData;

            @Override
            protected void onStartLoading() {
                if (mMovieData != null) {
                    deliverResult(mMovieData);
                } else {
                    forceLoad();
                }
            }

            @Override
            public Cursor loadInBackground() {
                try {
                    return mContext.getContentResolver().query(
                            uri,
                            null,
                            null,
                            null,
                            null);

                } catch (Exception e) {
                    Log.e(TAG, "Failed to asynchronously load data.");
                    e.printStackTrace();
                    return null;
                }
            }

            @Override
            public void deliverResult(Cursor data) {
                if(data == null) {
                    Log.d(TAG, "Data null");
                }
                mMovieData = data;
                super.deliverResult(data);
            }
        };
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        mCallback.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }


}

@@ -235,25 +235,22 @@
 content.
-AsyncTask
+Cursor
 Loader;%0A
@@ -294,33 +294,8 @@
 der;
-%0Aimport android.util.Log;
 %0A%0Aim
@@ -1597,328 +1597,48 @@
 ();%0A
-                break;%0A            case FETCH_ALL_MOVIES_DB_LOADER_ID:%0A                uri = MoviesContract.MoviesEntry.CONTENT_URI;%0A                break;%0A            default:%0A                throw new UnsupportedOperationException(%22Unknow loader id: %22 + loaderId);%0A        %7D%0A%0A        return new AsyncTaskLoader%3CCursor%3E
+%0A                return new CursorLoader
 (mCo
@@ -1638,27 +1638,25 @@
 der(mContext
-) %7B
+,
 %0A           
@@ -1660,28 +1660,9 @@
     
-Cursor mMovieData;%0A%0A
+ 
     
@@ -1664,34 +1664,28 @@
             
- @Override
+uri,
 %0A           
@@ -1689,83 +1689,25 @@
     
-protected void onStartLoading() %7B%0A                if (mMovieData !=
+           
  null
-) %7B
+,
 %0A   
@@ -1727,59 +1727,17 @@
     
-deliverResult(mMovieData);%0A                %7D else %7B
+    null,
 %0A   
@@ -1757,25 +1757,18 @@
     
-forceLoad();%0A    
+    null,%0A
     
@@ -1767,34 +1767,32 @@
 ll,%0A            
-%7D%0A
             %7D%0A%0A 
@@ -1779,33 +1779,38 @@
                 
-%7D
+null);
 %0A%0A            @O
@@ -1811,155 +1811,156 @@
     
-@Override%0A            public Cursor loadInBackground() %7B%0A                try %7B%0A                    return mContext.getContentResolver().query(%0A    
+case FETCH_ALL_MOVIES_DB_LOADER_ID:%0A                uri = MoviesContract.MoviesEntry.CONTENT_URI;%0A                return new CursorLoader(mContext,%0A
     
@@ -2000,36 +2000,32 @@
                 
-    
 null,%0A          
@@ -2034,38 +2034,30 @@
             
- 
-   
 null,%0A
-    
             
@@ -2094,36 +2094,31 @@
             
-    
 null);%0A
-%0A
             
@@ -2121,489 +2121,109 @@
     
-    %7D catch (Exception e) %7B%0A                    Log.e(TAG, %22Failed to asynchronously load data.%22);%0A                    e.printStackTrace();%0A                    return null;%0A                %7D%0A            %7D%0A%0A            @Override%0A            public void deliverResult(Cursor data) %7B%0A                if(data == null) %7B%0A                    Log.d(TAG, %22Data null%22);%0A                %7D%0A                mMovieData = data;%0A                super.deliverResult(data);%0A            %7D%0A        %7D;
+default:%0A                throw new RuntimeException(%22Loader Not Implemented: %22 + loaderId);%0A        %7D
 %0A   
@@ -2417,23 +2417,58 @@
 %3E loader) %7B%0A
+        mCallback.swapCursor(null);
 %0A    %7D%0A%0A%0A%7D%0A

change db loader