package brockbadgers.flock.Services;

import android.content.Context;
import android.content.Intent;
import android.app.Service;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.support.annotation.Nullable;

import brockbadgers.flock.R;

/**
 * Created by Peter on 9/17/2016.
 */
@SuppressWarnings("ResourceType")
public class GPS_Service extends Service {

    private LocationListener listener;
    private LocationManager locationManager;

    @Nullable
    @Override
    public IBinder onBind(Intent intent){
        return null;
    }

    @Override
    public void onCreate(){
        listener = new LocationListener(){
            @Override
            public void onLocationChanged(Location location){
                //Publish to the DB
                SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                String ref = sharedPref.getString(getString(R.string.user_id), null);
                location.getLongitude();
                location.getLatitude();
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {
                Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
            }

        };

        locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, listener);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if(locationManager != null){
            locationManager.removeUpdates(listener);
        }
    }


}

@@ -456,16 +456,151 @@
 ullable;
+%0Aimport android.util.Log;%0A%0Aimport com.google.firebase.database.DatabaseReference;%0Aimport com.google.firebase.database.FirebaseDatabase;
 %0A%0Aimport
@@ -621,16 +621,39 @@
 flock.R;
+%0Aimport classes.Person;
 %0A%0A/**%0A *
@@ -1366,32 +1366,83 @@
 %0A               
+ Person person = new Person(location.getLatitude(),
  location.getLon
@@ -1445,24 +1445,25 @@
 tLongitude()
+)
 ;%0A          
@@ -1460,32 +1460,314 @@
                 
+person.setId(ref);%0A                DatabaseReference database = FirebaseDatabase.getInstance().getReference();%0A                database.child(%22users%22).child(person.getId()).setValue(person);%0A                Log.d(%22Long%22, %22%22+location.getLongitude());%0A                Log.d(%22Lat%22, %22%22+
 location.getLati
@@ -1772,16 +1772,17 @@
 titude()
+)
 ;%0A      

Add the database calls to update the GPS values