package com.beepscore.android.sunshine;

import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.os.Bundle;
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.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

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.ArrayList;
import java.util.List;


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

    public ForecastFragment() {
    }
    private final String LOG_TAG = ForecastFragment.class.getSimpleName();

    List<String> weekForecast = new ArrayList<String>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View fragmentForecastView = inflater.inflate(R.layout.fragment_forecast, container, false);
        setHasOptionsMenu(true);

        configureList();

        // adapter creates views for each list item
        ListAdapter adapter = new ArrayAdapter<String>(getActivity(),
                R.layout.list_item_forecast,
                R.id.list_item_forecast_textview,
                weekForecast);

        ListView listView = (ListView)fragmentForecastView.findViewById(R.id.listview_forecast);
        listView.setAdapter(adapter);

        FetchWeatherTask fetchWeatherTask = new FetchWeatherTask();
        String urlString  = "http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7";
        fetchWeatherTask.execute(urlString);

        return fragmentForecastView;
    }

    private void configureList() {
        weekForecast.add("Today - Sunny - 88/63");
        weekForecast.add("Tomorrow - Foggy - 70/46");
        weekForecast.add("Weds - Cloudy - 72/63");
        weekForecast.add("Thurs - Rainy - 64/51");
        weekForecast.add("Fri - Foggy - 70/46");
        weekForecast.add("Sat - Sunny - 76/68");
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Inflate the menu; this adds items to the action bar if it is present.
        // http://stackoverflow.com/questions/15653737/oncreateoptionsmenu-inside-fragments
        inflater.inflate(R.menu.forecastfragment, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_refresh) {
            Log.d(LOG_TAG, "action_refresh clicked");
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    ////////////////////////////////////////////////////////////////////////////

    // https://developer.android.com/guide/components/processes-and-threads.html#Threads
    // http://stackoverflow.com/questions/9671546/asynctask-android-example?rq=1
    private class FetchWeatherTask extends AsyncTask<String, Void, String> {

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

        /** The system calls this on a worker (background) thread
         *  @param params first and only element is url string to make request to web service
         *  @return json response from web service. return null if no input.
         */
        @Override
        protected String doInBackground(String... params) {

            String urlString = params[0];

            // 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 forecastJsonStr = null;

            try {
                // Construct the URL for the OpenWeatherMap query
                // Possible parameters are available at OWM's forecast API page, at
                // http://openweathermap.org/API#forecast
                URL url = new URL(urlString);

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

                // If call connect on main thread app will crash
                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;
                }
                forecastJsonStr = buffer.toString();

            } catch (IOException e) {
                Log.e(LOG_TAG, "Error ", e);
                // If the code didn't successfully get the weather data, there's no point in attempting
                // to parse it.
                forecastJsonStr = 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 forecastJsonStr;
        }

        /** Use to update UI. The system calls this on the UI thread.
         *  @param forecastJsonStr is the result returned from doInBackground()
         */
        @Override
        protected void onPostExecute(String forecastJsonStr) {
            super.onPostExecute(forecastJsonStr);
            // TODO: parse forecastJsonStr in background? Show results in fragment view
            Log.d(LOG_TAG, forecastJsonStr);
        }

    }

}

@@ -777,24 +777,56 @@
 Fragment %7B%0A%0A
+    // public empty constructor%0A
     public F
@@ -845,22 +845,23 @@
 ent() %7B%0A
-
     %7D%0A
+%0A
     priv
@@ -982,24 +982,222 @@
 String%3E();%0A%0A
+    @Override%0A    // onCreate is called before onCreateView%0A    public void onCreate(Bundle savedInstanceState) %7B%0A        super.onCreate(savedInstanceState);%0A        setHasOptionsMenu(true);%0A    %7D%0A%0A
     @Overrid
@@ -1323,32 +1323,32 @@
 nstanceState) %7B%0A
+
         View fra
@@ -1434,41 +1434,8 @@
 se);
-%0A        setHasOptionsMenu(true);
 %0A%0A  

Revise ForecastFragment to more closely match Lesson 2 video. Override onCreate and move setHasOptionsMenu from onCreateView to onCreate.