package simonov.pk.myapplication;

import android.location.Location;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

import java.util.Calendar;

public class MainActivity  extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {


    private final String LOG_TAG ="LaurenceTestApp";
    private TextView locationView;
    private TextView latidudeView;
    private TextView longitudeView;
    private TextView timeView;
    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;
    private Location mLastLocation;

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

        locationView = (TextView) findViewById(R.id.location_view);
        latidudeView = (TextView) findViewById(R.id.latitude_view);
        longitudeView = (TextView) findViewById(R.id.longitude_view);
        timeView = (TextView) findViewById(R.id.time_view);

        buildGoogleApiClient();
    }

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Connect the client.
        mGoogleApiClient.connect();
    }

    @Override
    protected void onStop() {
        // Disconnecting the client invalidates it.
        if (mGoogleApiClient.isConnected()){
            mGoogleApiClient.disconnect();
        }
        super.onStop();
    }

    @Override
    public void onConnected(Bundle bundle) {

//        mLocationRequest = LocationRequest.create();
//        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
//        mLocationRequest.setInterval(10); // Update location every second

        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (mLastLocation != null) {
            fillViews(mLastLocation);
        }
//        .requestLocationUpdates(
//                mGoogleApiClient,
//                mLocationRequest,
//                this);

    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.i(LOG_TAG, "GoogleApiClient connection has been suspend");
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.i(LOG_TAG, "GoogleApiClient connection has failed");
    }

    private void fillViews(Location location) {
        Log.i(LOG_TAG, location.toString());
        locationView.setText(location.toString());
        latidudeView.setText(String.valueOf(location.getLatitude()));
        longitudeView.setText(String.valueOf(location.getLongitude()));
        int hour = Calendar.getInstance().get(Calendar.HOUR);
        int minute = Calendar.getInstance().get(Calendar.MINUTE);
        int second = Calendar.getInstance().get(Calendar.SECOND);
        timeView.setText(hour + ":" + minute + ":" + second);
    }

    @Override
    public void onLocationChanged(Location location) {
        fillViews(location);

//        locationView.setText(Double.toString(location.getLatitude()));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
@@ -2259,18 +2259,16 @@
 dle) %7B%0A%0A
-//
         
@@ -2312,18 +2312,16 @@
 eate();%0A
-//
         
@@ -2390,18 +2390,16 @@
 URACY);%0A
-//
         
@@ -2429,16 +2429,18 @@
 erval(10
+00
 ); // Up
@@ -2478,24 +2478,8 @@
     
- mLastLocation =
  Loc
@@ -2512,139 +2512,8 @@
 nApi
-.getLastLocation(mGoogleApiClient);%0A        if (mLastLocation != null) %7B%0A            fillViews(mLastLocation);%0A        %7D%0A//        
 .req
@@ -2533,18 +2533,16 @@
 pdates(%0A
-//
         
@@ -2567,18 +2567,16 @@
 Client,%0A
-//
         
@@ -2601,18 +2601,16 @@
 equest,%0A
-//
         
@@ -2624,17 +2624,16 @@
  this);%0A
-%0A
     %7D%0A%0A 

Implement onLocation Changed Callback