package com.example.android.popularmovies;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.example.android.popularmovies.data.MovieContract;
import com.example.android.popularmovies.data.MovieContract.MovieEntry;
import com.example.android.popularmovies.sync.MoviesSyncAdapter;


/**
 * A placeholder fragment containing a simple view.
 */
public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {


    public interface Callback {
        void onItemSelected(Uri uri, boolean firstDisplay);
    }

    private BroadcastReceiver receiver;
    private MoviesAdapter mMoviesAdapter;
    private TmdbAccess tmdbAccess;

    private static final int MOVIES_LOADER = 0;


    private static final String[] MOVIE_COLUMNS = {
            MovieEntry._ID,
            MovieEntry.COLUMN_POSTER_PATH,
            MovieEntry.COLUMN_TITLE
    };

    // These indices are tied to MOVIE_COLUMNS.  If MOVIE_COLUMNS changes, these
    // must change.
    static final int COL_MOVIE_ID = 0;
    static final int COL_POSTER_PATH = 1;
    static final int COL_TITLE = 2;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.e("PopularMovies", "onCreate " + getClass().getSimpleName());
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);

        // The CursorAdapter will take data from our cursor and populate the GridView
        // However, we cannot use FLAG_AUTO_REQUERY since it is deprecated, so we will end
        // up with an empty list the first time we run.
        tmdbAccess = new TmdbAccess(getContext());
        mMoviesAdapter = new MoviesAdapter(getActivity(), null, 0, tmdbAccess);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.e("PopularMovies", "onCreateView " + getClass().getSimpleName());
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        // Get a reference to the ListView, and attach this adapter to it.
        GridView gridView = (GridView) rootView.findViewById(R.id.gridView_discovery);
        gridView.setAdapter(mMoviesAdapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Cursor cursor = (Cursor) parent.getItemAtPosition(position);
                if (cursor != null) {
                    Uri uri = MovieEntry.buildMovieDetailUri(cursor.getLong(COL_MOVIE_ID));
                    ((Callback) getActivity()).onItemSelected(uri, false);
                }
            }
        });
        return rootView;
    }


    @Override
    public void onStart() {
        receiver = new InternetReceiver(getActivity()) {
            @Override
            protected void refresh() {
                syncDB();
            }
        };

        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        getActivity().registerReceiver(receiver, filter);
        super.onStart();
    }

    @Override
    public void onStop() {
        getActivity().unregisterReceiver(receiver);
        super.onStop();
    }




    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Log.e("PopularMovies", "onActivityCreated " + getClass().getSimpleName());
        getLoaderManager().initLoader(MOVIES_LOADER, null, this);
        super.onActivityCreated(savedInstanceState);
    }


    public void onSettingsChange() {
        //Offline 'popularity.desc' and 'vote_average.desc' sort order will display the display
        // the favorites in the order chosen.
        deleteUnfavorites(getContext());
        if (((InternetReceiver)receiver).isConnected())
            syncDB();
        getLoaderManager().restartLoader(MOVIES_LOADER, null, this);
    }


    public void syncDB() {
        MoviesSyncAdapter.syncImmediately(getActivity());
    }

    private static void deleteUnfavorites(Context context) {
        //Need to delete Trailer and Reviews entry first to avoid foreign key conflict
        //Need to delete Trailer and Reviews, because 'on delete cascade does not seems to work'
        context.getContentResolver().delete(MovieContract.TrailerEntry.CONTENT_URI, null, null);
        context.getContentResolver().delete(MovieContract.ReviewEntry.CONTENT_URI, null, null);
        context.getContentResolver().delete(MovieEntry.CONTENT_URI, null, null);
    }


    private Uri buildMoviesUri() {
        String sortBy=Utility.getSortOrderPreferences(getContext());
        if (sortBy.equals(getString(R.string.pref_sort_order_favorite)))
            return MovieEntry.buildMoviesFavoriteUri();
        return MovieEntry.buildMoviesSortByUri(sortBy);
    }


    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        Log.e("PopularMovies", "onCreateLoader " + getClass().getSimpleName());
        return new CursorLoader(getActivity(),
                buildMoviesUri(),
                MOVIE_COLUMNS,
                null,
                null,
                null);
    }



    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        Log.e("PopularMovies", "onLoadFinished " + getClass().getSimpleName());
        mMoviesAdapter.swapCursor(data);
        //To avoid 'java.lang.IllegalStateException: Can not perform this action inside of onLoadFinished'
        Handler handler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                ((Callback) getActivity()).onItemSelected(mMoviesAdapter.getmUriFirstItem(), true);
            }
        });

    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        Log.e("PopularMovies", "onLoaderReset " + getClass().getSimpleName());
        mMoviesAdapter.swapCursor(null);
    }


}

@@ -64,94 +64,15 @@
 ent.
-BroadcastReceiver;%0Aimport android.content.Context;%0Aimport android.content.IntentFilter
+Context
 ;%0Aim
@@ -133,32 +133,64 @@
 ctivityManager;%0A
+import android.net.NetworkInfo;%0A
 import android.n
@@ -1070,48 +1070,8 @@
  %7D%0A%0A
-    private BroadcastReceiver receiver;%0A
     
@@ -3221,524 +3221,8 @@
 %7D%0A%0A%0A
-    @Override%0A    public void onStart() %7B%0A        receiver = new InternetReceiver(getActivity()) %7B%0A            @Override%0A            protected void refresh() %7B%0A                syncDB();%0A            %7D%0A        %7D;%0A%0A        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);%0A        getActivity().registerReceiver(receiver, filter);%0A        super.onStart();%0A    %7D%0A%0A    @Override%0A    public void onStop() %7B%0A        getActivity().unregisterReceiver(receiver);%0A        super.onStop();%0A    %7D%0A%0A%0A
 %0A%0A  
@@ -3742,37 +3742,8 @@
 if (
-((InternetReceiver)receiver).
 isCo
@@ -3875,24 +3875,24 @@
  syncDB() %7B%0A
-
         Movi
@@ -3936,32 +3936,429 @@
 vity());%0A    %7D%0A%0A
+%0A    public boolean isConnected() %7B%0A        ConnectivityManager connectivityManager = (ConnectivityManager) getContext().getSystemService%0A                (Context.CONNECTIVITY_SERVICE);%0A        NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();%0A        boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();%0A        return isConnected;%0A    %7D%0A%0A
     private stat

Remove Internet BroadcastReceiver for now