package com.example.Maps05;

import android.os.Bundle;
import android.util.Log;

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


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
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.OnMapReadyCallback;
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.OnMarkerDragListener;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;

public class MainActivity extends Activity implements OnMapReadyCallback, OnMarkerDragListener
{
	
    private GoogleMap idmap;
    
    boolean markerClicked = false;
    PolylineOptions rectOptions;
    Polyline polyline;
    
    Marker prevMarker;
    
    Dialog dialog;
    EditText title,snippet;
    Button add,cancel;
    AlertDialog.Builder alertDialog;
    
    Marker toto; 
    
    List<Marker> markers = new ArrayList<Marker>();
    List<Polyline> polylines = new ArrayList<Polyline>();

    
    int INITIAL_STROKE_WIDTH_PX = 5;
    
    LatLng point01 = new LatLng(21, 57);
    LatLng point02 = new LatLng(45, 18);


    //markers.add(mMap.addMarker(markerOptions));
    
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_main);
	
			 initAddMarkerDialog();
		        initRemoveMarkerDialog();
			
		        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
		       mapFragment.getMapAsync(this);

		        
		      
		       
		       
		//	map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
			
			
			
	}
	

	


	@Override
    public void onMapReady(GoogleMap map) 
	{
        initInfoWindowClick(map);
        initMapClick(map);
        
        idmap = map;
        
        map.setOnMarkerDragListener(this);  //////// ????
        
       
        //toto = idmap.addMarker(new MarkerOptions().position(point01).title("Tutorialspoint.com"));
        //markers.add( toto);
        //toto = idmap.addMarker(new MarkerOptions().position(point02).title("Tutorialspoint.com"));
        
	       
      
     // Add a listener for polyline clicks that changes the clicked polyline's color.
        map.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener() {
            @Override
            public void onPolylineClick(final Polyline polyline) {
                // Flip the values of the red, green and blue components of the polyline's color.
                polyline.setColor(polyline.getColor() ^ 0x00ffffff);
                
                alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    	polyline.remove();
                    }
                });
                alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                alertDialog.show();
                
            }
});
        
        
       // Polyline line = googleMap.addPolyline(new PolylineOptions()
       // 	       .add(new LatLng(51.2, 0.1), new LatLng(51.7, 0.3))
       // 	       .width(5)
       // 	       .color(Color.RED));
        
    }

    public void initAddMarkerDialog(){
        dialog = new Dialog(this);
        dialog.setTitle("Add marker !");
        dialog.setContentView(R.layout.dialog_layout);
        title = (EditText)dialog.findViewById(R.id.title);
        snippet = (EditText)dialog.findViewById(R.id.snippet);
        add = (Button)dialog.findViewById(R.id.btn_add);
        cancel = (Button)dialog.findViewById(R.id.btn_cancel);
    }

    public void initRemoveMarkerDialog(){
        alertDialog = new AlertDialog.Builder(this);
        alertDialog.setTitle("Delete Marker !");
        alertDialog.setMessage("Do you want to delete the Marker !");
    }

    public  void initInfoWindowClick(GoogleMap googleMap)
    {
	        googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() 
	        {
		            @Override
		            public void onInfoWindowClick(final Marker marker) 
		            {
		                alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() 
		                {
			                    @Override
			                    public void onClick(DialogInterface dialog, int which) 
			                    {
			                        marker.remove();
			                    }
		                });
		                
		                alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener()
		                {
			                    @Override
			                    public void onClick(DialogInterface dialog, int which) 
			                    {
			                        dialog.dismiss();
			                    }
		                });
		                
		                alertDialog.show();
		            }
	        });
    }
    public void initMapClick(final GoogleMap googleMap)
    {
        googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() 
        {
	            @Override
	            public void onMapClick(final LatLng latLng) 
	            {
		                title.setText("");
		                snippet.setText("");
		                dialog.show();
		                add.setOnClickListener(new View.OnClickListener() 
		                {
			                    @Override
			                    public void onClick(View v) 
			                    {
			                        if (title.getText().toString().isEmpty() || snippet.getText().toString().isEmpty())
			                        {
			                            Toast.makeText(getApplicationContext(), "Text fields cannot be empty", Toast.LENGTH_SHORT).show();
			                        } 
			                        else 
			                        {
			                        	
			                        	//create new marker when user long clicks
			                           // map.addMarker(new MarkerOptions()
			                           // .position(arg0)
			                           // .draggable(true));
			                        	
			                        	
			                        	
			                        	
			                            Marker marker = googleMap.addMarker(new MarkerOptions()
			                            		.position(latLng)
			                            		.title(title.getText()
	                            				.toString()).snippet(snippet.getText().toString())
			                            		.draggable(true)
			                            		);
			                            markers.add( marker);
			                            
			                            if(markers.size() == 2)
			                            {
			                            	
			                            	marker = markers.get(0);
			                                point01 = new LatLng(marker.getPosition().latitude, marker.getPosition().longitude);
			                            
				                            marker = markers.get(1);
				                            point02 = new LatLng(marker.getPosition().latitude, marker.getPosition().longitude);
			                            
			                            	
			                            	  polyline = idmap.addPolyline(new PolylineOptions()
			                		                  .add(point01, point02)
			                		                  .width(INITIAL_STROKE_WIDTH_PX)
			                		                  .color(Color.BLUE)
			                		                  .geodesic(true)
			                		                  );
			                		  		  
			                		  		polyline.setClickable(true);
			                            }
			                            
			                            
			                            dialog.dismiss();
			                        }
			                    }
		                });
		                cancel.setOnClickListener(new View.OnClickListener() 
		                {
			                    @Override
			                    public void onClick(View v) 
			                    {
			                        dialog.dismiss();
			                    }
		                });
		                
		               
	            }
        });
    }
	
	
	
	

	@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();
		  			
		  		  polyline = idmap.addPolyline(new PolylineOptions()
		                  .add(point01, point02)
		                  .width(INITIAL_STROKE_WIDTH_PX)
		                  .color(Color.BLUE)
		                  .geodesic(true)
		                  );
		  		  
		  		polyline.setClickable(true);
		  			
		  			//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();
		  			polyline.remove();
		  			//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 void onMarkerDrag(Marker marker) {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void onMarkerDragEnd(Marker marker) {
		// TODO Auto-generated method stub
		 LatLng dragPosition = marker.getPosition();
	        double dragLat = dragPosition.latitude;
	        double dragLong = dragPosition.longitude;
	        Log.i("info", "on drag end :" + dragLat + " dragLong :" + dragLong);
	        Toast.makeText(getApplicationContext(), "Marker Dragged..!", Toast.LENGTH_LONG).show();
	}





	@Override
	public void onMarkerDragStart(Marker marker) {
		// TODO Auto-generated method stub
		
	}

	




	
	
	
	
}