package com.busstopalarm;

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

import org.json.JSONException;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.ZoomControls;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * @author Orkhan Muradov, David Nufer
 * 
 * This class creates a google map which shows current user location and draws 
 * bus route user requested.
 * 
 */

public class MapPage extends MapActivity {

	private static final String TAG = "inMapPage";
	// This number is arbitrary - this is used in startActivityForResult() and
	// finishActivity() calls to determine whether the user backed out of 
	// confirmation page to the map page, or confirmed the alarm.
	// If it is the latter, then we can finish the map page.
	public static final int MAP_CONFIRM_TRANSITION = 123;
	
	private MapController mapController;
	private MapView mapView;
	private LocationManager lm;
	private String routeID;
	private List<Overlay> mapOverlays;
	private ItemizedOverlayHelper currentLocOverlay;

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	/**
	 * Called when activity is first created in Map page
	 * (non-Javadoc)
	 * @see com.google.android.maps.MapActivity#onCreate(android.os.Bundle)
	 * @param @param Bundle which holds the current state (info)
	 */
	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.map);
		//RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.mapview);
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		mapController = mapView.getController();
		mapController.setZoom(13);

		//instantiates gps service
		lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000,
				10.0f, new GPSUpdateHandler());

		//array of overlay items
		mapOverlays = mapView.getOverlays();

		routeID = getIntent().getStringExtra("routeID");
		DataFetcher df = new DataFetcher();
		BusRoute busRoute;
		try {
			// draw routes on map
			busRoute = df.getBusRouteById(Integer.parseInt(routeID.split("_")[1]), true);
			for (Polyline p : busRoute.getPolylines()) {
				PolylineOverlay po = new PolylineOverlay(p);
				mapOverlays.add(po);
			}

			Drawable drawable = getApplicationContext().getResources().getDrawable(R.drawable.busstopicon);
			ItemizedOverlayHelper itemizedoverlay = new ItemizedOverlayHelper(this, drawable);

			for (BusStop bs : busRoute.getBusStops()) {
				BusStopOverlayItem ov = new BusStopOverlayItem(bs);
				itemizedoverlay.addOverlay(ov);
			}
			mapOverlays.add(itemizedoverlay);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			Toast t = Toast.makeText(this, "Error occured while trying to draw bus route", Toast.LENGTH_LONG);
			t.show();
		} catch (IOException e) {
			e.printStackTrace();
			Toast t = Toast.makeText(this, "Error occured while trying to draw bus route", Toast.LENGTH_LONG);
			t.show();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.v(TAG, "onActivityResult" + requestCode + " " + resultCode);
		if (requestCode == MAP_CONFIRM_TRANSITION) {
			if (resultCode == RESULT_OK) {
				// Finish the old main page if we have already set the alarm.
				setResult(RESULT_OK);
				finishActivity(MainPage.MAIN_CONFIRM_TRANSITION);
				finish();
			}
		}
		// else, we got here from backing out of confirmation page, we do not
		// finish the map page.	
	}
	
	public void fillData() {
		setContentView(mapView);
	}

	/**
	 * This method is needed in order to use GPS location of the user
	 * 
	 * @param 
	 * @return returns boolean false
	 */
	@Override
	protected boolean isLocationDisplayed() {
		return super.isLocationDisplayed();
	}

	/**
	 * Grabs user's location coordinates and displays user's location
	 */
	public class GPSUpdateHandler implements LocationListener {

		public void onLocationChanged(Location location) {
			double lat = location.getLatitude();
			double lon = location.getLongitude();
			GeoPoint point = new GeoPoint((int) (lat*1E6), (int) (lon*1E6));
			mapView.getController().animateTo(point);
		}
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}

		public void onProviderDisabled(String provider) {
		}

		public void onProviderEnabled(String provider) {
		}
	}

	/**
	 * 
	 * This method adds button to pop-up setting menu
	 *    	 
	 * @param menu that pops us when pressing menu button
	 * @return returns boolean true
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0,1, 1, "Go back");
		menu.add(0,2, 2, "Settings");
		return true;
	}

	/**
	 * This method returns to Main page when go back is pressed.
	 * And exits the program when exit button is pressed
	 * 
	 * @param takes menuitem as parameter
	 * @return returns boolean true
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case 1:
			final Intent i = new Intent(this, MainPage.class);
			startActivity(i);
			finish();
			break;
		case 2:
			final Intent j = new Intent(this, SettingsPage.class);
			startActivity(j);
			break;
		}


		return true;
	}
}

@@ -2250,16 +2250,18 @@
 om(13);%0A
+%09%09
 %0A%09%09//ins
@@ -2970,16 +2970,84 @@
 picon);%0A
+%09%09%09getIntent().putExtra(%22busroutedesc%22, busRoute.getDescription());%0A
 %09%09%09Itemi

add bus route description to the intent to use in confirmationPage