package com.udacity.sunshine.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.udacity.sunshine.R;
import com.udacity.sunshine.Utility;
import com.udacity.sunshine.fragment.DetailFragment;
import com.udacity.sunshine.fragment.ForecastFragment;
import com.udacity.sunshine.fragment.ForecastFragment.Callback;
import com.udacity.sunshine.sync.SunshineSyncAdapter;

public class MainActivity extends AppCompatActivity implements Callback{

    private final String TAG = this.getClass().getSimpleName();
    private String mLocation;
    private boolean mTwoPane;
    private static final String DETAILFRAGMENT_TAG = "DFTAG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLocation = Utility.getPreferredLocation(this);
        setContentView(R.layout.activity_main);

        if (findViewById(R.id.weather_detail_container) != null) {
            // The detail container view will be present only in the large-screen layouts
            // (res/layout-sw600dp). If this view is present, then the activity should be
            // in two-pane mode.
            mTwoPane = true;
            // In two-pane mode, show the detail view in this activity by
            // adding or replacing the detail fragment using a
            // fragment transaction.
            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.weather_detail_container, new DetailFragment(), DETAILFRAGMENT_TAG)
                        .commit();
            }
        } else {
            mTwoPane = false;
            getSupportActionBar().setElevation(0f);
        }

        ForecastFragment forecastFragment = (ForecastFragment) getSupportFragmentManager()
                .findFragmentById(R.id.fragment_forecast);
        forecastFragment.setUseTodayLayout(!mTwoPane);

        SunshineSyncAdapter.initializeSyncAdapter(this);

        getSupportActionBar().setLogo(R.drawable.ic_logo);
        getSupportActionBar().setDisplayOptions(
                ActionBar.DISPLAY_SHOW_HOME|ActionBar.DISPLAY_USE_LOGO);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void onResume() {
        super.onResume();
        String location = Utility.getPreferredLocation(this);
        // update the location in our second pane using the fragment manager
        if (location != null && !location.equals(mLocation)) {
            ForecastFragment ff =
                    (ForecastFragment) getSupportFragmentManager().findFragmentByTag(DETAILFRAGMENT_TAG);
            if (null != ff) {
                ff.onLocationChanged();
            }

            // on big screen
            DetailFragment df = (DetailFragment) getSupportFragmentManager()
                    .findFragmentByTag(DETAILFRAGMENT_TAG);
            if (null != df) {
                df.onLocationChanged(location);
            }
            mLocation = location;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            startActivity(new Intent(this, SettingsActivity.class));
            return true;
        }
//        if (id == R.id.action_map) {
//            openPreferredLocationInMap();
//            return true;
//        }
        return super.onOptionsItemSelected(item);
    }

//    private void openPreferredLocationInMap() {
//        String location = Utility.getPreferredLocation(this);
//
//        // Using the URI scheme for showing a location found on a map.  This super-handy
//        // intent can is detailed in the "Common Intents" page of Android's developer site:
//        // http://developer.android.com/guide/components/intents-common.html#Maps
//
//        Uri geoLocation = Uri.parse("geo:0,0?")
//                .buildUpon()
//                .appendQueryParameter("q", location)
//                .build();
//
//        Intent intent = new Intent(Intent.ACTION_VIEW);
//        intent.setData(geoLocation);
//
//        if (intent.resolveActivity(getPackageManager()) != null) {
//            startActivity(intent);
//        } else {
//            Log.d(TAG, "Couldn't call " + location + ", no receiving apps installed!");
//        }
//    }

    @Override
    public void onItemSelected(Uri dateUri) {
        if (mTwoPane) {
            // In two-pane mode, show the detail view in this activity by
            // adding or replacing the detail fragment using a
            // fragment transaction.
            Bundle args = new Bundle();
            args.putParcelable(DetailFragment.DETAIL_URI, dateUri);

            DetailFragment fragment = new DetailFragment();
            fragment.setArguments(args);

            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.weather_detail_container, fragment, DETAILFRAGMENT_TAG)
                    .commit();
        } else {
            Intent intent = new Intent(this, DetailActivity.class)
                    .setData(dateUri);
            startActivity(intent);
        }
    }
}

@@ -3586,1072 +3586,58 @@
   %7D%0A
-//        if (id == R.id.action_map) %7B%0A//            openPreferredLocationInMap();%0A//            return true;%0A//        %7D%0A        return super.onOptionsItemSelected(item);%0A    %7D%0A%0A//    private void openPreferredLocationInMap() %7B%0A//        String location = Utility.getPreferredLocation(this);%0A//%0A//        // Using the URI scheme for showing a location found on a map.  This super-handy%0A//        // intent can is detailed in the %22Common Intents%22 page of Android's developer site:%0A//        // http://developer.android.com/guide/components/intents-common.html#Maps%0A//%0A//        Uri geoLocation = Uri.parse(%22geo:0,0?%22)%0A//                .buildUpon()%0A//                .appendQueryParameter(%22q%22, location)%0A//                .build();%0A//%0A//        Intent intent = new Intent(Intent.ACTION_VIEW);%0A//        intent.setData(geoLocation);%0A//%0A//        if (intent.resolveActivity(getPackageManager()) != null) %7B%0A//            startActivity(intent);%0A//        %7D else %7B%0A//            Log.d(TAG, %22Couldn't call %22 + location + %22, no receiving apps installed!%22);%0A//        %7D%0A//
+        return super.onOptionsItemSelected(item);%0A
     

delete some comments