package com.example.Maps03;

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.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.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;


public class MainActivity extends Activity 
{
	Button button;
	EditText textReport;
    DAOtextReport daOtextReport;
    
    Marker marker, m1;
  
    private GoogleMap map;
    
    boolean markerClicked = false;
    PolylineOptions rectOptions;
    Polyline polyline;
    
    
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_main);
	
			daOtextReport = new DAOtextReport(this);
			
			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(new GoogleMap.OnMapClickListener() {

		        @Override
		        public void onMapClick(LatLng point)
		        {
		        	Toast.makeText(getApplicationContext(), "Tic", Toast.LENGTH_SHORT).show();
		        	
		            // TODO Auto-generated method stub
		           // lstLatLngs.add(point);
//		            map.clear();
//		            map.addMarker(new MarkerOptions().position(point));
		            
		            
//		             m1 = map.addMarker(new MarkerOptions()
//		                    .position(new LatLng(38.609556, -1.139637))
//		                    .anchor(0.5f, 0.5f)
//		                    .title("Title1")
//		                    .snippet("Snippet1")
//		                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_flag)));
		            
		            
		           /* 
		            if (marker != null) {
		                marker.remove();
		            }
		            marker = map.addMarker(new MarkerOptions()
		                    .position(point)
		                    .icon(BitmapDescriptorFactory
		                            .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
		          */                  
		            
		        }
		        
		    });
			
			
			map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener()
			{
		            @Override
		            public void onMapLongClick (LatLng point)
		            {
		            	Toast.makeText(getApplicationContext(), "Long click", Toast.LENGTH_SHORT).show();
		            	
		             
		            	   map.addMarker(new MarkerOptions().position(point).title(point.toString()));
		            	// markerClicked = false;
		            	
		            }
		     });
			
			map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
	        {

	            @Override
	            public boolean onMarkerClick(Marker arg0)
	            {
	            	Toast.makeText(getApplicationContext(), "Inside", Toast.LENGTH_SHORT).show();
	            	
	            	
	            	
			            	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;
	            }
	            
	        });
			
			
	}
	
	public void textReportOnClicked(View view) 
	{
			Toast.makeText(getApplicationContext(), "Added to DB", Toast.LENGTH_SHORT).show();

			Random_generator msr = new Random_generator();
		
			String report = msr.generateRandomString();
			//String report = textReport.getText().toString();
			
        	long result = daOtextReport.addReport(new TextReport("junjunguo.com", report, 63.4187362, 10.4387621));

        	if (result >= 0)
        	{
        		Toast.makeText(getApplicationContext(), "succsefully add report to row " + result, Toast.LENGTH_SHORT).show();
        		textReport.getText().clear();
        	} 
        	else 
        	{
        		Toast.makeText(getApplicationContext(), "not succeed ! please try again !", Toast.LENGTH_SHORT).show();
        	}
        	
    }
	
	
	
	public void remplir_db()
	{
		long result = daOtextReport.addReport(new TextReport("junjunguo.com", "report", 63.4187362, 10.4387621));
	}

	@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);
		  			m1.remove();
		  			//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);
             }
    }
	
	
	
}