package com.example.android.sunshine.app;

/**
 * Created by Kerry on 2016/2/23.
 */

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
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.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 com.example.android.sunshine.app.data.WeatherContract;

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

    private static final int FORECAST_LOADER = 0;

    private static final String[] FORECAST_COLUMNS = {
            WeatherContract.WeatherEntry.TABLE_NAME + "." + WeatherContract.WeatherEntry._ID,
            WeatherContract.WeatherEntry.COLUMN_DATE,
            WeatherContract.WeatherEntry.COLUMN_SHORT_DESC,
            WeatherContract.WeatherEntry.COLUMN_MAX_TEMP,
            WeatherContract.WeatherEntry.COLUMN_MIN_TEMP,
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING,
            WeatherContract.WeatherEntry.COLUMN_WEATHER_ID,
            WeatherContract.LocationEntry.COLUMN_COORD_LAT,
            WeatherContract.LocationEntry.COLUMN_COORD_LONG
    };

    static final int COL_WEATHER_ID = 0;
    static final int COL_WEATHER_DATE = 1;
    static final int COL_WEATHER_DESC = 2;
    static final int COL_WEATHER_MAX_TEMP = 3;
    static final int COL_WEATHER_MIN_TEMP = 4;
    static final int COL_LOCATION_SETTING = 5;
    static final int COL_WEATHER_CONDITION_ID = 6;
    static final int COL_COORD_LAT = 7;
    static final int COL_COORD_LONG = 8;

    private ForecastAdapter mForecastAdapter;

    public interface Callback {
        public void onItemSelected(Uri dateUri);
    }

    public ForecastFragment() {

    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

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

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

            updateWeather();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

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

        mForecastAdapter = new ForecastAdapter(getActivity(), null, 0);

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

        ListView listView = (ListView) rootView.findViewById(R.id.listView_forecast);
        listView.setAdapter(mForecastAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Cursor cursor = (Cursor) adapterView.getItemAtPosition(position);
                if (cursor != null) {
                    String locationSetting = Utility.getPreferredLocation(getActivity());
                    ((Callback)getActivity()).onItemSelected(WeatherContract.WeatherEntry.buildWeatherLocationWithDate(locationSetting, cursor.getLong(COL_WEATHER_DATE)));
                }
            }
        });

        return rootView;
    }

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

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

    void onLocationChanged() {
        updateWeather();
        getLoaderManager().restartLoader(FORECAST_LOADER, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        String locationSetting = Utility.getPreferredLocation(getActivity());
        String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + " ASC";
        Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate(locationSetting, System.currentTimeMillis());

        return new CursorLoader(getActivity(), weatherForLocationUri, FORECAST_COLUMNS, null, null, sortOrder);
    }

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

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

@@ -83,39 +83,8 @@
 */%0A%0A
-import android.content.Intent;%0A
 impo
@@ -805,24 +805,228 @@
 %3CCursor%3E %7B%0A%0A
+    private ForecastAdapter mForecastAdapter;%0A%0A    private ListView mListView;%0A    private int mPosition = ListView.INVALID_POSITION;%0A%0A    private static final String SELECTED_KEY = %22selected_position%22;%0A%0A
     private 
@@ -2103,55 +2103,8 @@
 8;%0A%0A
-    private ForecastAdapter mForecastAdapter;%0A%0A
     
@@ -3166,18 +3166,10 @@
     
+m
 L
-istView l
 istV
@@ -3232,33 +3232,34 @@
 ecast);%0A        
-l
+mL
 istView.setAdapt
@@ -3288,17 +3288,18 @@
         
-l
+mL
 istView.
@@ -3899,46 +3899,486 @@
     
-%7D%0A        %7D);%0A%0A        return rootView
+    mPosition = position;%0A            %7D%0A        %7D);%0A%0A        if(savedInstanceState != null && savedInstanceState.containsKey(SELECTED_KEY)) %7B%0A            mPosition = savedInstanceState.getInt(SELECTED_KEY);%0A        %7D%0A        return rootView;%0A    %7D%0A%0A    @Override%0A    public void onSaveInstanceState(Bundle outState) %7B%0A        if(mPosition != ListView.INVALID_POSITION) %7B%0A            outState.putInt(SELECTED_KEY, mPosition);%0A        %7D%0A        super.onSaveInstanceState(outState)
 ;%0A  
@@ -5046,147 +5046,148 @@
 ing 
-locationSetting = Utility.getPreferredLocation(getActivity());%0A        String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + %22 ASC%22
+sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + %22 ASC%22;%0A%0A        String locationSetting = Utility.getPreferredLocation(getActivity())
 ;%0A  
@@ -5575,24 +5575,144 @@
 or(cursor);%0A
+        if(mPosition != ListView.INVALID_POSITION) %7B%0A            mListView.smoothScrollToPosition(mPosition);%0A        %7D%0A
     %7D%0A%0A    @

Save scroll position