package com.example.android.popularmoviesapp;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
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;

public class MainActivity extends AppCompatActivity {
//    ArrayAdapter<Integer> mPosterAdapter;
    MoviePosterAdapter mPosterAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        ImageView imageView = (ImageView) findViewById(R.id.photo_image_view);
        // load Picasso image
//        Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").into(imageView);

        // references to images
        Integer[] images = {
                R.drawable.picasso, R.drawable.interstellar,
                R.drawable.interstellar, R.drawable.picasso
        };

        mPosterAdapter = new MoviePosterAdapter(this, Arrays.asList(images));

        // attach adapter to GridView
        GridView gridView = (GridView) findViewById(R.id.grid_view_posters);
        gridView.setAdapter(mPosterAdapter);
    }

    @Override
    public void onStart() {
        super.onStart();
        (new FetchMovieData()).execute();
    }

    /**
     * Gets movie data with a http request.
     * Parses data as a JSON string on background thread and
     * publishes the result on the UI.
     */
    public class FetchMovieData extends AsyncTask<Void, Void, Void> {

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

        @Override
        protected Void doInBackground(Void... params) {

            HttpURLConnection urlConnection = null;
            BufferedReader reader = null;

            // will contain the raw JSON response as a string.
            String popularMovies = null;

            try {
                // https://www.themoviedb.org/
                String baseUrl = "http://api.themoviedb.org/3/movie/popular";
                String apiKey = "?api_key=" + BuildConfig.MOVIE_DB_API_KEY;
                URL url = new URL(baseUrl.concat(apiKey));

                // create the request to OpenWeatherMap, 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) {
                    // useful for debugging
                    buffer.append(line + "\n");
                }

                if (buffer.length() == 0) {
                    // stream was empty
                    return null;
                }
                popularMovies = buffer.toString();
                Log.d(LOG_TAG, "popularMovies: " + popularMovies);

            } catch (IOException e) {
                Log.e(LOG_TAG, "Error ", e);
                // If the code didn't successfully get the weather data
                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;
        }
    }
}

@@ -40,16 +40,40 @@
 esapp;%0A%0A
+import android.net.Uri;%0A
 import a
@@ -1406,24 +1406,112 @@
 onStart() %7B%0A
+        final String POPULAR = %22popular%22;%0A        final String TOP_RATED = %22top_rated%22;%0A
         supe
@@ -1562,16 +1562,25 @@
 execute(
+TOP_RATED
 );%0A    %7D
@@ -1791,20 +1791,22 @@
 yncTask%3C
-Void
+String
 , Void, 
@@ -1948,20 +1948,22 @@
 kground(
-Void
+String
 ... para
@@ -1965,24 +1965,127 @@
  params) %7B%0A%0A
+            // check%0A            if (params.length == 0) %7B%0A                return null;%0A            %7D%0A%0A
             
@@ -2169,72 +2169,8 @@
 ull;
-%0A%0A            // will contain the raw JSON response as a string.
 %0A   
@@ -2198,23 +2198,16 @@
 arMovies
- = null
 ;%0A%0A     
@@ -2286,22 +2286,29 @@
     
+final 
 String 
-baseUrl
+BASE_URL
  = %22
@@ -2341,23 +2341,16 @@
 3/movie/
-popular
 %22;%0A     
@@ -2364,38 +2364,210 @@
     
+final 
 String 
-apiKey
+API_PARAM
  = %22
-?
 api_key
-=%22 + 
+%22;%0A                Uri builtUri = Uri.parse(BASE_URL).buildUpon()%0A                        .appendPath(params%5B0%5D)%0A                        .appendQueryParameter(API_PARAM,
 Buil
@@ -2590,16 +2590,25 @@
 _API_KEY
+).build()
 ;%0A      
@@ -2640,29 +2640,73 @@
 RL(b
-aseUrl.concat(apiKey)
+uiltUri.toString());%0A                Log.d(LOG_TAG, %22url: %22 + url
 );%0A%0A
@@ -2750,22 +2750,12 @@
  to 
-OpenWeatherMap
+TMDb
 , an
@@ -2995,17 +2995,17 @@
  into a 
-S
+s
 tring%0A  
@@ -3909,60 +3909,27 @@
  // 
-If the code didn't successfully get the weather data
+no movie data found
 %0A   

Build URI and add param