package com.example.Maps04;

import android.os.Bundle;

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


import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.location.Geocoder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.identity.intents.Address;
import com.google.android.gms.maps.CameraUpdate;
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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.android.gms.vision.barcode.Barcode.GeoPoint;


import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;

public class MainActivity extends Activity implements OnMapClickListener, OnMapLongClickListener, OnMarkerClickListener
{
	
    private GoogleMap map;
    
    boolean markerClicked = false;
    PolylineOptions rectOptions;
    Polyline polyline;
    
    Marker prevMarker;
    
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_main);
	
			
			
			map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
			
			map.setMyLocationEnabled(true);
			map.getUiSettings().setZoomControlsEnabled(true);
			map.getUiSettings().setCompassEnabled(true);
			map.getUiSettings().setMyLocationButtonEnabled(true);
			
			map.getUiSettings().setCompassEnabled(true);
	
			map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
			
			map.setOnMapClickListener(this);
			map.setOnMapLongClickListener(this);
			map.setOnMarkerClickListener(this);

			markerClicked = false; 
			
			
	}
	

	
	
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
			getMenuInflater().inflate(R.menu.activity_main, menu);
			return true;
	}
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) 
	{
       
		//googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		//googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
		//googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
		//googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
		//googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
		
	        switch (item.getItemId())
	        {
		        case R.id.menu_remind:
		  			Toast.makeText(getApplicationContext(),"Remind",Toast.LENGTH_SHORT).show();
		  			map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		  		
		  			//Intent Intent04 = new Intent(MainActivity.this, NewActivity.class);
		  			//startActivity(Intent04);
			        return true;
		        
		  		case R.id.menu_repeat:
		  			Toast.makeText(getApplicationContext(),"Repeat",Toast.LENGTH_SHORT).show();
		  			map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
		  			//Intent Intent05 = new Intent(MainActivity.this, Control_Service.class);
		  			//startActivity(Intent05);
			        return true;
			        
		  		case R.id.menu_about:
		  			Toast.makeText(getApplicationContext(),"CAbout",Toast.LENGTH_SHORT).show();
		  			map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
			        return true;
		
		  		case R.id.menu_quit:
		  			Toast.makeText(getApplicationContext(),"Salut on ferme la boutique...",Toast.LENGTH_SHORT).show();
		  			finish();
			        return true;    
		          
		  		default:
		          return super.onOptionsItemSelected(item);
             }
    }

	@Override
	public boolean onMarkerClick(Marker marker)
	{
		/*
		if(markerClicked){
			
			if(polyline != null){
				polyline.remove();
				polyline = null;
			}
			
			rectOptions.add(marker.getPosition());
			rectOptions.color(Color.RED);
			polyline = map.addPolyline(rectOptions);
		}else{
			if(polyline != null){
				polyline.remove();
				polyline = null;
			}
			
			rectOptions = new PolylineOptions().add(marker.getPosition());
			markerClicked = true;
		}
		
		return true; 
		*/
		
		//aa= marker.getPosition().latitude;
        //bb=marker.getPosition().longitude;
        if (prevMarker != null) {
            //Set prevMarker back to default color
            prevMarker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
        }

        //leave Marker default color if re-click current Marker
        if (!marker.equals(prevMarker)) {
            marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
            prevMarker = marker;
        }
        prevMarker = marker;
        return false;
		
	
	}

	@Override
	public void onMapLongClick(LatLng point)
	{
		// TODO Auto-generated method stub
		map.addMarker(new MarkerOptions().position(point).title(point.toString()));
		
		markerClicked = false; 
	}

	@Override
	public void onMapClick(LatLng point)
	{
		// TODO Auto-generated method stub
		map.animateCamera(CameraUpdateFactory.newLatLng(point));
		
		markerClicked = false; 
		
	}
	
	
	
}