package fr.tvbarthel.apps.sayitfromthesky;


import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.LatLng;
import com.google.maps.android.SphericalUtil;

public class SayItFragment extends Fragment implements SayItMapFragment.ISayItMapFragment {

    private static float DEFAULT_VALUE_ZOOM = 15f;
    private static float DELTA_DISTANCE_IN_METER = 5f;
    private static String BUNDLE_KEY_LOCATION = "SayItFragment.Bundle.Key.Location";
    private static String BUNDLE_KEY_ZOOM = "SayItFragment.Bundle.Key.Zoom";

    private SayItMapFragment mMapFragment;
    private GoogleMap mGoogleMap;
    private Location mLastKnownLocation;
    private float mLastKnownZoom;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_say_it, container, false);
        mLastKnownZoom = DEFAULT_VALUE_ZOOM;

        if (savedInstanceState != null) {
            mLastKnownLocation = savedInstanceState.getParcelable(BUNDLE_KEY_LOCATION);
            mLastKnownZoom = savedInstanceState.getFloat(BUNDLE_KEY_ZOOM, DEFAULT_VALUE_ZOOM);
        }

        mMapFragment = new SayItMapFragment(this);
        getChildFragmentManager().beginTransaction().add(R.id.fragment_say_it_map_container, mMapFragment, "fragmentTagMap").commit();

        return view;
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable(BUNDLE_KEY_LOCATION, mLastKnownLocation);
        if (mGoogleMap != null) {
            outState.putFloat(BUNDLE_KEY_ZOOM, mGoogleMap.getCameraPosition().zoom);
        }

    }

    @Override
    public void onMapReady() {
        if (mGoogleMap == null) {
            mGoogleMap = mMapFragment.getMap();
            if (mGoogleMap != null) {
                //the map is now active and can be manipulated
                mGoogleMap.setMyLocationEnabled(true);
                final UiSettings uiSettings = mGoogleMap.getUiSettings();
                uiSettings.setCompassEnabled(false);
                uiSettings.setZoomControlsEnabled(false);

                //try to restore last known location
                if (mLastKnownLocation != null) {
                    initMapLocation();
                }

                mGoogleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
                    @Override
                    public void onMyLocationChange(Location location) {
                        //first location
                        if (mLastKnownLocation == null) {
                            mLastKnownLocation = location;
                            initMapLocation();
                        } else if (isLocationIfOutdated(location)) {
                            setNewLocation(location);
                        }
                    }
                });

            }
        }
    }

    private void initMapLocation() {
        mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()), mLastKnownZoom));
    }

    private boolean isLocationIfOutdated(Location candidateLocation) {
        final LatLng candidateLatLng = new LatLng(candidateLocation.getLatitude(), candidateLocation.getLongitude());
        final LatLng currentLatLng = new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude());
        final double distance = SphericalUtil.computeDistanceBetween(candidateLatLng, currentLatLng);
        return distance > DELTA_DISTANCE_IN_METER;
    }

    private void setNewLocation(Location newLocation) {
        //TODO look at the elapsed time ?
        mLastKnownLocation = newLocation;
        mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(
                new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude())));
    }

}

@@ -138,16 +138,41 @@
 agment;%0A
+import android.util.Log;%0A
 import a
@@ -978,24 +978,61 @@
 wnLocation;%0A
+    private LatLng mLastKnownLatLng;%0A
     private 
@@ -1352,33 +1352,35 @@
 ) %7B%0A            
-m
+set
 LastKnownLocatio
@@ -1380,18 +1380,27 @@
 Location
- =
+((Location)
  savedIn
@@ -1445,16 +1445,17 @@
 OCATION)
+)
 ;%0A      
@@ -3047,33 +3047,35 @@
                 
-m
+set
 LastKnownLocatio
@@ -3079,19 +3079,17 @@
 tion
- = 
+(
 location
 ;%0A  
@@ -3080,24 +3080,25 @@
 ion(location
+)
 ;%0A          
@@ -3698,202 +3698,42 @@
 g = 
-new LatLng(candidateLocation.getLatitude(), candidateLocation.getLongitude());%0A        final LatLng currentLatLng = new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()
+locationToLatLng(candidateLocation
 );%0A 
@@ -3821,15 +3821,18 @@
 ng, 
-current
+mLastKnown
 LatL
@@ -3992,33 +3992,81 @@
  time ?%0A        
-m
+Log.d(%22argonne%22, %22set new location%22);%0A        set
 LastKnownLocatio
@@ -4066,19 +4066,17 @@
 Location
- = 
+(
 newLocat
@@ -4074,24 +4074,25 @@
 (newLocation
+)
 ;%0A        mG
@@ -4148,81 +4148,410 @@
 Lng(
+mLastKnownLatLng));
 %0A    
+%7D%0A%0A
     
-        new LatLng(mLastKnownLocation.getLatitude(), mLastKnownL
+private void setLastKnownLocation(Location location) %7B%0A        if (location != null) %7B%0A            Log.d(%22argonne%22, %22set last known location%22);%0A            mLastKnownLocation = location;%0A            mLastKnownLatLng = locationToLatLng(location);%0A        %7D%0A    %7D%0A%0A    private LatLng locationToLatLng(Location location) %7B%0A        return new LatLng(location.getLatitude(), l
 ocat
@@ -4569,18 +4569,16 @@
 itude())
-))
 ;%0A    %7D%0A

Store last known LatLng