package com.example.location;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

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.UiSettings;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
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;

//public class YouAreHereMapActivity extends FragmentActivity { //API10-
public class YouAreHereMapActivity extends Activity { //API11+
  private GoogleMap map;
  private Polyline polyLine;
  private ArrayList<LatLng> points = new ArrayList<LatLng>();
  private SharedPreferences.OnSharedPreferenceChangeListener prefListener;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_layout);
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    double lat = extras.getDouble("latitude");
    double lon = extras.getDouble("longitude");
    LatLng latLng = new LatLng(lat, lon);
    points.add(latLng);

    //SupportMapFragment mapFragment = ((SupportMapFragment) getSupportFragmentManager() // API10-
    MapFragment mapFragment = ((MapFragment) getFragmentManager() //API11+
        .findFragmentById(R.id.mapFragment));
    map = mapFragment.getMap();
    if (map == null) {
      return;
    }
    map.setMyLocationEnabled(true);

    map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
    CameraPosition pos = map.getCameraPosition();
    Marker pin = map.addMarker(new MarkerOptions().position(latLng)
        .title("You are HERE: " + pos.target)
        .snippet("zoom: " + pos.zoom + ", bearing: " + pos.bearing)
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));
    pin.setDraggable(false);
    lat = extras.getDouble("savedLat", 0);
    lon = extras.getDouble("savedLng", 0);
    if ((lat != 0) && (lon != 0)) {
      latLng = new LatLng(lat,lon);
      map.addMarker(new MarkerOptions().position(latLng)
          .title("Saved: " + pos.target)
          .snippet("zoom: " + pos.zoom + ", bearing: " + pos.bearing)
          .icon(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));  
    }

    map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
      @Override
      public void onMapClick(LatLng pos) {
        Toast.makeText(getBaseContext(), "Pos: " + pos, Toast.LENGTH_LONG)
            .show();
      }
    });

    map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
      @Override
      public void onMapLongClick(LatLng pos) {
        map.animateCamera(CameraUpdateFactory.newLatLng(pos));
        points.add(pos);
        PolylineOptions polylineOptions = new PolylineOptions();
        polylineOptions.width(4).color(0xff0000ff).addAll(points)
            .geodesic(true);
        if (YouAreHereMapActivity.this.polyLine != null) {
          YouAreHereMapActivity.this.polyLine.remove();
        }
        YouAreHereMapActivity.this.polyLine = map.addPolyline(polylineOptions);
      }
    });
    
    prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() {      
      @Override
      public void onSharedPreferenceChanged(SharedPreferences prefs,
          String key) {
        setMapOptions(prefs, key);
      }
    };
    PreferenceManager.getDefaultSharedPreferences(this)
      .registerOnSharedPreferenceChangeListener(prefListener);

  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuItem prefs = menu.add(0, 0, 0, "Preferences");
    prefs.setIcon(android.R.drawable.ic_menu_preferences);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    switch (item.getItemId()) {
      case 0:
        startActivity(new Intent("com.example.location.MAPPREFERENCES"));
    }
    return true;
  }
  
  private void setMapOptions(SharedPreferences prefs, String key) {
    if (key.equals("mapType")) {
      map.setMapType(Integer.parseInt(prefs.getString(key, "1")));
    }
    else {
      UiSettings uiSettings = map.getUiSettings();
      boolean value = prefs.getBoolean(key, true);
      if (key.equals("scrollGesturesEnabled")) {
        uiSettings.setScrollGesturesEnabled(value);
      }
      if (key.equals("rotateGesturesEnabled")) {
        uiSettings.setRotateGesturesEnabled(value);
      }
      if (key.equals("compassEnabled")) {
        uiSettings.setCompassEnabled(value);
      }
      if (key.equals("tiltGesturesEnabled")) {
        uiSettings.setTiltGesturesEnabled(value);
      }
      if (key.equals("zoomGesturesEnabled")) {
        uiSettings.setZoomGesturesEnabled(value);
      }
      if (key.equals("zoomControlsEnabled")) {
        uiSettings.setZoomControlsEnabled(value);
      }
      if (key.equals("myLocationEnabled")) {
        map.setMyLocationEnabled(value);
      }
      if (key.equals("myLocationButtonEnabled")) {
        uiSettings.setMyLocationButtonEnabled(value);
      }
    }

  }

}
