package gr.mybook.mytouch;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.location.Geocoder;
import android.os.Bundle;
import android.widget.Toast;
import android.app.Activity;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity implements OnMapClickListener{
	private static final int NORMAL = GoogleMap.MAP_TYPE_NORMAL;
	private GoogleMap map;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
		map.setMapType(NORMAL);
		map.setOnMapClickListener(this);
/*		
        // geocoding
        Geocoder geoCoder = new Geocoder(getBaseContext(),Locale.getDefault());
        try {
        	List<android.location.Address> addresses = geoCoder.getFromLocationName("TEI of Piraeus", 5);
        	if (addresses.size()>0) {
        		double latitude = addresses.get(0).getLatitude();
        		double longitude = addresses.get(0).getLongitude();
        		MarkerOptions marker = new MarkerOptions().position(
        				new LatLng(latitude, longitude))
        				.title("TEI OF PIRAEUS")
        				.snippet("MSc in Applied Information Systems");
        		marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
        		map.addMarker(marker);
        	}
        	
        } catch (IOException e) {
        	e.printStackTrace();
        }*/
 	}
	
	public void onMapClick(LatLng point) {
    	MarkerOptions marker = new MarkerOptions().position(new LatLng(point.latitude, point.longitude)).title("New Marker");
        map.addMarker(marker);
        double lati = point.latitude;
        double longi=point.longitude;
 //       Toast.makeText(getBaseContext(), lati+" and "+longi, Toast.LENGTH_LONG).show();
        // reverse geocoding
		Geocoder geoCoder = new Geocoder(getBaseContext(),Locale.getDefault());
		try{
			List<android.location.Address> addresses = geoCoder.getFromLocation(lati, longi, 1);
			String add="";
			if (addresses.size()>0) {
				for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();i++)
					add += addresses.get(0).getAddressLine(i)+ "\n";
			}
			Toast.makeText(getBaseContext(),  add,  Toast.LENGTH_LONG).show();
		} catch (IOException e) {
			e.printStackTrace();
		}
     }
}
