package com.example.android.sunshine.app;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
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.AdapterView;
import android.widget.ListView;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v4.content.CursorLoader;

import com.example.android.sunshine.app.data.WeatherContract;


/**
 * A placeholder fragment containing a simple view.
 */
public class ForecastFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
    public static final String FORECAST_STRING = "FORECAST_STRING";
    private static final int FORECAST_LOADER_ID = 0;

    private ForecastAdapter mForecastAdapter;

    public ForecastFragment() {
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getLoaderManager().initLoader(FORECAST_LOADER_ID, savedInstanceState, this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        mForecastAdapter = new ForecastAdapter(getActivity(), null, 0);
        ListView lv = (ListView) rootView.findViewById(R.id.listview_forecast);
        if (lv == null) {
            return rootView;
        }
        lv.setAdapter(mForecastAdapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //String forecast = mForecastAdapter.getItem(position);
                Intent i = new Intent(getActivity(), DetailActivity.class);
                //i.putExtra(FORECAST_STRING, forecast);
                startActivity(i);
            }
        });
        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
        updateWeather();
    }

    private void updateWeather() {
        FetchWeatherTask fwt = new FetchWeatherTask(getActivity());
        fwt.execute(Utility.getPreferredLocation(getActivity()));
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.forecast_fragment, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_refresh:
                updateWeather();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        final Uri weatherUri = WeatherContract.WeatherEntry.buildWeatherLocation(
                Utility.getPreferredLocation(getActivity()));
        final String[] projection = null, selectionArgs = null;
        final String selection = null;
        final String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + " ASC";
        return new CursorLoader(
                getActivity(),
                weatherUri,
                projection,
                selection,
                selectionArgs,
                sortOrder);
    }

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

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mForecastAdapter.swapCursor(null);
    }
}

@@ -865,16 +865,17 @@
 TRING%22;%0A
+%0A
     priv
@@ -918,16 +918,1608 @@
 _ID = 0;
+%0A    private static final String%5B%5D FORECAST_COLUMNS = %7B%0A            // In this case the id needs to be fully qualified with a table name, since%0A            // the content provider joins the location & weather tables in the background%0A            // (both have an _id column)%0A            // On the one hand, that's annoying.  On the other, you can search the weather table%0A            // using the location set by the user, which is only in the Location table.%0A            // So the convenience is worth it.%0A            WeatherContract.WeatherEntry.TABLE_NAME + %22.%22 + WeatherContract.WeatherEntry._ID,%0A            WeatherContract.WeatherEntry.COLUMN_DATE,%0A            WeatherContract.WeatherEntry.COLUMN_SHORT_DESC,%0A            WeatherContract.WeatherEntry.COLUMN_MAX_TEMP,%0A            WeatherContract.WeatherEntry.COLUMN_MIN_TEMP,%0A            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING,%0A            WeatherContract.WeatherEntry.COLUMN_WEATHER_ID,%0A            WeatherContract.LocationEntry.COLUMN_COORD_LAT,%0A            WeatherContract.LocationEntry.COLUMN_COORD_LONG%0A    %7D;%0A%0A    // These indices are tied to FORECAST_COLUMNS.  If FORECAST_COLUMNS changes, these%0A    // must change.%0A    static final int COL_WEATHER_ID = 0;%0A    static final int COL_WEATHER_DATE = 1;%0A    static final int COL_WEATHER_DESC = 2;%0A    static final int COL_WEATHER_MAX_TEMP = 3;%0A    static final int COL_WEATHER_MIN_TEMP = 4;%0A    static final int COL_LOCATION_SETTING = 5;%0A    static final int COL_WEATHER_CONDITION_ID = 6;%0A    static final int COL_COORD_LAT = 7;%0A    static final int COL_COORD_LONG = 8;
 %0A%0A    pr

Add provided constants for projections