package com.platypii.baseline.data;

import com.google.android.gms.maps.model.LatLng;
import com.platypii.baseline.data.measurements.MLocation;
import com.platypii.baseline.util.Util;

/**
 * A class to log, analyze, and provide feedback on flight data
 */
public class MyFlightManager {

    public static LatLng homeLoc;

    /**
     * Computes the estimated time to ground based on current location + velocity
     */
    public static double timeToGround() {
        double timeToGround = -MyAltimeter.altitude / MyAltimeter.climb;
        if(!Util.isReal(timeToGround) || timeToGround < 0.01 || Math.abs(MyAltimeter.climb) < 0.05 || 24 * 60 * 60 < timeToGround) {
            // return NaN if we don't have an accurate landing location (climbing, very close to ground, very long estimate, etc)
            return Double.NaN;
        } else {
            return timeToGround;
        }
    }

    /**
     * Computes the estimated landing location based on current location + velocity
     */
    public static LatLng getLandingLocation() {
        // Compute time to ground
        double timeToGround = timeToGround();
        if(Util.isReal(timeToGround)) {
            final MLocation loc = MyLocationManager.lastLoc;
            // Compute horizontal distance traveled at current velocity for timeToGround seconds
            double groundDistance = timeToGround * loc.groundSpeed();
            double bearing = loc.bearing();

            // Compute estimated landing location
            final MLocation currentLocation = MyLocationManager.lastLoc;
            final LatLng landingLocation = currentLocation.moveDirection(bearing, groundDistance);

            // Log.d("FlightManager", currentLocation + " -> " + landingLocation + " (" + groundDistance + "m, " + bearing + "°)");

            return landingLocation;
        } else {
            return null;
        }
    }

}
@@ -387,35 +387,34 @@
 current 
-location + velocity
+altitude and climb
 %0A     */
@@ -507,16 +507,21 @@
 altitude
+AGL()
  / MyAlt
@@ -976,17 +976,19 @@
 ocation 
-+
+and
  velocit
@@ -1079,32 +1079,38 @@
  ground%0A        
+final 
 double timeToGro
@@ -1167,16 +1167,53 @@
 oGround)
+ && MyLocationManager.lastLoc != null
 ) %7B%0A    
@@ -1236,19 +1236,31 @@
 ocation 
-loc
+currentLocation
  = MyLoc
@@ -1273,32 +1273,33 @@
 anager.lastLoc;%0A
+%0A
             // C
@@ -1430,19 +1430,31 @@
 round * 
-loc
+currentLocation
 .groundS
@@ -1490,19 +1490,31 @@
 aring = 
-loc
+currentLocation
 .bearing
@@ -1572,81 +1572,8 @@
 ion%0A
-            final MLocation currentLocation = MyLocationManager.lastLoc;%0A
     

Fix timeToGround by using AGL