package com.example.distance01;

import android.support.v7.app.ActionBarActivity;
import android.util.FloatMath;
import android.util.Log;

import java.text.DecimalFormat;

import com.google.android.gms.maps.model.LatLng;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

//http://andrew.hedges.name/experiments/haversine/

// https://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

// http://www.sanfoundry.com/java-android-program-start-stop-service/

public class MainActivity extends Activity 
{

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		double lat1 = 38.898556;
		double lon1 = -77.037852;
		double lat2 = 38.897147;
		double lon2 = -77.043934;
		
		double dlon = lon2 - lon1;
		double dlat = lat2 - lat1;
		//		a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2
		//		c = 2 * atan2( sqrt(a), sqrt(1-a) )
		//		d = R * c (where R is the radius of the Earth) 
		
		float res = distance(38.898556, -77.037852, 38.897147, -77.043934); 
		
		double aaa =  gps2m(38.898556, -77.037852, 38.897147, -77.043934); 
		
		
		
		LatLng xxx = new LatLng( lat1, lon1);
		LatLng yyy = new LatLng( lat2, lon2);
		
		double bravo = CalculationByDistance(xxx, yyy);
		
		String toto = "alpha";
		
	/*	
        	Location mylocation = new Location("");
        	Location dest_location = new Location("");
     
        	String lat = lat_dest.getText().toString();
        	String lon = lon_dest.getText().toString();
     
        	dest_location.setLatitude(Double.parseDouble(lat));
        	dest_location.setLongitude(Double.parseDouble(lon));
   
        	Double my_loc = 0.00;
        	mylocation.setLatitude(my_loc);
        	mylocation.setLongitude(my_loc);
     
        	Double distance = mylocation.distanceTo(dest_location);//in meters
     */
		
	}

	
	public float distance (double d, double e, double f, double g ) 
	{
		
		
	    double earthRadius = 3958.75;
	    double latDiff = Math.toRadians(f-d);
	    double lngDiff = Math.toRadians(g-e);
	    double a = Math.sin(latDiff /2) * Math.sin(latDiff /2) +  Math.cos(Math.toRadians(d)) * Math.cos(Math.toRadians(f)) *   Math.sin(lngDiff /2) * Math.sin(lngDiff /2);
	    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
	    double distance = earthRadius * c;

	    int meterConversion = 1609;

	    return new Float(distance * meterConversion).floatValue();
	}
	
/*	
	public  double distance_in_meter(double lat1, double lon1, double lat2, double lon2) 
	{
	    double R = 6371000f; // Radius of the earth in m
	    double dLat = (lat1 - lat2) * Math.PI / 180f;
	    double dLon = (lon1 - lon2) * Math.PI / 180f;
	    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
	            Math.cos(latlong1.latitude * Math.PI / 180f) * Math.cos(latlong2.latitude * Math.PI / 180f) *
	                    Math.sin(dLon/2) * Math.sin(dLon/2);
	    double c = 2f * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
	    double d = R * c;
	    return d;
	}
*/	
	
	
	private double gps2m(double lat_a, double lng_a, double lat_b, double lng_b) 
	{
	    double pk = (float) (180/3.14169);
	    double a1 = lat_a / pk;
	    double a2 = lng_a / pk;
	    double b1 = lat_b / pk;
	    double b2 = lng_b / pk;

	    double t1 = Math.cos(a1) * Math.cos(a2) * Math.cos(b1) * Math.cos(b2);
	    double t2 = Math.cos(a1) * Math.sin(a2) * Math.cos(b1) * Math.sin(b2);
	    double t3 = Math.sin(a1) * Math.sin(b1);
	    double tt = Math.acos(t1 + t2 + t3);

	    return 6366000*tt;
	}
	
	public double CalculationByDistance(LatLng StartP, LatLng EndP) {
        int Radius=6371;//radius of earth in Km         
        double lat1 = StartP.latitude;
        double lat2 = EndP.latitude;
        double lon1 = StartP.longitude;
        double lon2 = EndP.longitude;
        double dLat = Math.toRadians(lat2-lat1);
        double dLon = Math.toRadians(lon2-lon1);
        double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
        Math.sin(dLon/2) * Math.sin(dLon/2);
        double c = 2 * Math.asin(Math.sqrt(a));
        double valueResult= Radius*c;
        double km=valueResult/1;
        DecimalFormat newFormat = new DecimalFormat("####");
        int kmInDec =  Integer.valueOf(newFormat.format(km));
        double meter=valueResult%1000;
        int  meterInDec= Integer.valueOf(newFormat.format(meter));
        Log.i("Radius Value",""+valueResult+"   KM  "+kmInDec+" Meter   "+meterInDec);

        return Radius * c;
     }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.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();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
