package com.cfto_leipzig;


import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.view.View;
import android.widget.Button;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.location.LocationServices;

public class mainActivity extends AppCompatActivity implements ConnectionCallbacks{

    private static final String LOG_KEY = "Main";
    /**
     * Provides the entry point to Google Play services.
     */
    protected GoogleApiClient mGoogleApiClient;

    /**
     * Represents a geographical location.
     */
    protected Location mLastLocation;

    public double LocationLongitude = 0.;
    public double LocationLatitude = 0.;
    private Controller cont;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainactivity);
        cont = new Controller(this);

        Button search = (Button) findViewById(R.id.button3);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mainActivity.this, display_probability.class));
            }
        });

        buildGoogleApiClient();
    }

    /**
     * Builds a GoogleApiClient. Uses the addApi() method to request the LocationServices API.
     */
    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addApi(LocationServices.API)
                .build();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }

    /**
     * Runs when a GoogleApiClient object successfully connects.
     */
    @Override
    public void onConnected(Bundle connectionHint) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (mLastLocation != null) {
            LocationLatitude = mLastLocation.getLatitude();
            LocationLongitude = mLastLocation.getLongitude();

            Log.i(LOG_KEY, ""+LocationLatitude);
            Log.i(LOG_KEY, ""+LocationLongitude);

            Log.i(LOG_KEY, cont.getNearestAirport(LocationLatitude, LocationLongitude));

        }
    }

    @Override
    public void onConnectionSuspended(int cause) {
        // The connection to Google Play services was lost for some reason. We call connect() to
        // attempt to re-establish the connection.
        mGoogleApiClient.connect();
    }
}

@@ -186,16 +186,48 @@
 il.Log;%0A
+import android.widget.EditText;%0A
 import a
@@ -856,16 +856,64 @@
 ation;%0A%0A
+    EditText ed;%0A    String nearestAirpotIATA;%0A%0A
     publ
@@ -1275,16 +1275,70 @@
 tton3);%0A
+        ed = (EditText) findViewById(R.id.editText);%0A%0A
         
@@ -2757,32 +2757,34 @@
 );%0A%0A            
+//
 Log.i(LOG_KEY, c
@@ -2841,24 +2841,161 @@
 ngitude));%0A%0A
+            nearestAirpotIATA = cont.getNearestAirport(LocationLatitude, LocationLongitude);%0A%0A            ed.setText(nearestAirpotIATA);%0A
         %7D%0A  

add airport to textedit