package gr.mybook.mygps;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
//import android.widget.TextView;

public class MainActivity extends Activity {
    private static final long MINIMUM_DISTANCE_FOR_UPDATES = 1; // in Meters
    private static final long MINIMUM_TIME_FOR_UPDATES = 1000; // in Milliseconds
    protected LocationManager locationManager;
//    TextView txt;
    private GoogleMap map;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
//		txt = (TextView) findViewById(R.id.textView);
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(
				                LocationManager.GPS_PROVIDER,
				                MINIMUM_TIME_FOR_UPDATES,
				                MINIMUM_DISTANCE_FOR_UPDATES,
				                new MyLocationListener()
				        );
		Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
/*		        if (location != null) {
		            String message = String.format(
		                    "Current Location \n Longitude: %1$s \n Latitude: %2$s",
		                    location.getLongitude(), location.getLatitude()
		            );
		            txt.setText(message);
		        }*/
		double latitude = location.getLatitude();
		double longitude = location.getLongitude();
		try{
		map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
		MarkerOptions marker = new MarkerOptions().position(
				new LatLng(latitude, longitude))
				.title("ACROPOLIS")
				.snippet("National Hellenic Museum");
		map.addMarker(marker);
		// move the camera to the position with a zoom level
				CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude, longitude)).zoom(15).build();
				map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
				} catch (Exception e) {
					e.printStackTrace();
				}
	}
	
    private class MyLocationListener implements LocationListener {
        public void onLocationChanged(Location location) {
            String message = String.format(
                    "New Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude()
            );
//            txt.setText(message);
        }
        public void onStatusChanged(String s, int i, Bundle b) {
//        	txt.setText("Provider status changed");
        }
        public void onProviderDisabled(String s) {
//        	txt.setText("Provider disabled by the user. GPS turned off");
         }
        public void onProviderEnabled(String s) {
//        	txt.setText("Provider enabled by the user. GPS turned on");
        }
    }
}
