package com.vinnypalumbo.popularmovies;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;

/**
 * A placeholder fragment containing a simple view.
 */
public class MovieFragment extends Fragment {

    private ImageAdapter mMovieAdapter;

    public MovieFragment() {
    }

    MoviePoster[] moviePosters = {
            new MoviePoster(R.drawable.interstellar),
            new MoviePoster(R.drawable.interstellar),
            new MoviePoster(R.drawable.interstellar),
            new MoviePoster(R.drawable.interstellar),
            new MoviePoster(R.drawable.interstellar),
            new MoviePoster(R.drawable.interstellar),
            new MoviePoster(R.drawable.interstellar),
            new MoviePoster(R.drawable.interstellar),
            new MoviePoster(R.drawable.interstellar),
            new MoviePoster(R.drawable.interstellar),
            new MoviePoster(R.drawable.interstellar)
    };

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        // Add this line in order for this fragment to handle menu events
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Inflate the menu; this adds items to the action bar if it is present.
        inflater.inflate(R.menu.moviefragment, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml
        int id = item.getItemId();
        if(id == R.id.action_refresh){
            FetchMovieTask movieTask = new FetchMovieTask();
            movieTask.execute();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        mMovieAdapter = new ImageAdapter(
                getActivity(),
                Arrays.asList(moviePosters)
        );

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        // Get a reference to the ListView, and attach the Adapter to it
        GridView gridView = (GridView) rootView.findViewById(R.id.gridview_movie);
        gridView.setAdapter(mMovieAdapter);

        return rootView;
    }

    public class FetchMovieTask extends AsyncTask<Void, Void, Void> {

        private final String LOG_TAG = FetchMovieTask.class.getSimpleName();

        @Override protected Void doInBackground(Void... params) {
            // These two need to be declared outside the try/catch
            // so that they can be closed in the finally block.
            HttpURLConnection urlConnection = null;
            BufferedReader reader = null;

            // Will contain the raw JSON response as a string.
            String movieJsonStr = null;

            try {
                // Construct the URL for the TheMovieDB query
                // Possible parameters are avaiable at TMDB's movie API page, at
                // http://docs.themoviedb.apiary.io/#reference/discover/discovermovie
                URL url = new URL("http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc");

                // Create the request to TheMovieDB, and open the connection
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();

                // Read the input stream into a String
                InputStream inputStream = urlConnection.getInputStream();
                StringBuffer buffer = new StringBuffer();
                if (inputStream == null) {
                    // Nothing to do.
                    return null;
                }
                reader = new BufferedReader(new InputStreamReader(inputStream));

                String line;
                while ((line = reader.readLine()) != null) {
                    // Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
                    // But it does make debugging a *lot* easier if you print out the completed
                    // buffer for debugging.
                    buffer.append(line + "\n");
                }

                if (buffer.length() == 0) {
                    // Stream was empty.  No point in parsing.
                    return null;
                }
                movieJsonStr = buffer.toString();
            } catch (IOException e) {
                Log.e(LOG_TAG, "Error ", e);
                // If the code didn't successfully get the movie data, there's no point in attempting
                // to parse it.
                return null;
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (final IOException e) {
                        Log.e(LOG_TAG, "Error closing stream", e);
                    }
                }
            }
            return null;
        }
    }
}

@@ -34,16 +34,40 @@
 ovies;%0A%0A
+import android.net.Uri;%0A
 import a
@@ -87,16 +87,16 @@
 ncTask;%0A
-
 import a
@@ -2310,16 +2310,33 @@
 execute(
+%22popularity.desc%22
 );%0A     
@@ -3068,20 +3068,22 @@
 yncTask%3C
-Void
+String
 , Void, 
@@ -3221,25 +3221,200 @@
 und(
-Void... params) %7B
+String... params) %7B%0A            // If there's no zip code, there's nothing to look up.  Verify size of params.%0A            if (params.length == 0) %7B%0A                return null;%0A            %7D%0A
 %0A   
@@ -3987,16 +3987,17 @@
 ermovie%0A
+%0A
         
@@ -4004,16 +4004,18 @@
         
+//
 URL url 
@@ -4092,24 +4092,617 @@
 ty.desc%22);%0A%0A
+                final String FORECAST_BASE_URL = %22http://api.themoviedb.org/3/discover/movie?%22;%0A                final String SORT_PARAM = %22sort_by%22;%0A                final String APIKEY_PARAM = %22api_key%22;%0A%0A                Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon()%0A                    .appendQueryParameter(SORT_PARAM, params%5B0%5D)%0A                    .appendQueryParameter(APIKEY_PARAM, BuildConfig.THE_MOVIE_DB_API_KEY)%0A                    .build();%0A%0A                URL url = new URL(builtUri.toString());%0A%0A                Log.v(LOG_TAG, %22Built URI %22 + builtUri.toString());%0A%0A%0A%0A%0A%0A
             

Build URL with params