package gr.uoc.csd.hy439.positioning;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

import gr.uoc.csd.hy439.positioning.fingerprint.FingerprintService;
import gr.uoc.csd.hy439.positioning.fingerprint.FingerprintService.FingerprintBinder;
import gr.uoc.csd.hy439.positioning.fingerprint.PngOverlay;
import gr.uoc.csd.hy439.positioning.fingerprint.PointOverlay;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * @author katsarakis
 *
 */
public class FingerprintActivity extends MapActivity {
	protected MapView map;
	private PointOverlay pointOverlay;
	private PngOverlay floorPlanOverlay;
	private TextView latTextView, lngTextView;
	private Button registerButton, unregisterButton, singleUpdateButton;
	private FingerprintService fingerprintService;
	private boolean isListenerRegistered;
	
	private LocationListener locationListener= new LocationListener() {		
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			if (provider==FingerprintService.PROVIDER && status==LocationProvider.OUT_OF_SERVICE) {
				Log.d(this.getClass().getName(), FingerprintService.PROVIDER+ " out of service. Unregistering.");
				fingerprintService.removeUpdates(locationListener);
				isListenerRegistered= false;
			}
		}		
		@Override
		public void onProviderEnabled(String provider) {
			Log.d(this.getClass().getName(), provider+" enabled by the user");
		}		
		@Override
		public void onProviderDisabled(String provider) {
			Log.d(this.getClass().getName(), provider+" disabled by the user");
		}		
		@Override
		public void onLocationChanged(Location location) {
			latTextView.setText("latitude= "+location.getLatitude());
			lngTextView.setText("longitude= "+location.getLongitude());
			GeoPoint gp= new GeoPoint((int) Math.round(location.getLatitude()*1E6), (int) Math.round(location.getLongitude()*1E6));
			pointOverlay.setGeoPoint(gp);
			map.invalidate();
			map.getController().animateTo(gp);
			Log.d(this.getClass().getName(), "Got new Location!");
		}
	};
	
	public ServiceConnection fingerprintServiceConnection = new ServiceConnection() {		
		public void onServiceConnected(ComponentName className, IBinder service) {
			FingerprintBinder binder= (FingerprintBinder) service;			
			fingerprintService = binder.getService();
			//fingerprintService.requestLocationUpdates(0, 0, locationListener);
			Log.d(this.getClass().getName(), "Connected to the FingerprintService");
		}
		public void onServiceDisconnected(ComponentName arg0) {
			fingerprintService= null;
			Log.d(this.getClass().getName(), "Disconnected from the FingerprintService");
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.debug_fingerprint_view);
		map = (MapView) findViewById(R.id.fingerprintMapView);
		map.setBuiltInZoomControls(true);
		map.setSatellite(true);
		map.getController().setZoom(21);
		map.getOverlays().clear();
		
		Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.floorplan_rightscale_michalis_overlay);
		floorPlanOverlay= new PngOverlay(map, bm);
		map.getOverlays().add(floorPlanOverlay);
		map.invalidate();
		
		pointOverlay= new PointOverlay(map, new GeoPoint(25072565, 35305052));
		map.getOverlays().add(pointOverlay);
		Log.d(this.getClass().getName(), "Hi! I am launched!");
		
		latTextView= (TextView) findViewById(R.id.fingerprintLatTextView);
		lngTextView= (TextView) findViewById(R.id.fingerprintLngTextView);
		registerButton= (Button) findViewById(R.id.requestUpdatesFromFingerprintServiceButton);
		unregisterButton= (Button) findViewById(R.id.unregisterFromFingerprintServiceButton);
		singleUpdateButton= (Button) findViewById(R.id.requestSingleUpdateFromFingerprintServiceButton);
		
		Intent intent = new Intent(this, FingerprintService.class);
		bindService(intent, fingerprintServiceConnection, Context.BIND_AUTO_CREATE);
		isListenerRegistered= false;
		
		registerButton.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				if (fingerprintService != null) {
					fingerprintService.requestLocationUpdates(0, 0, locationListener);
					isListenerRegistered= true;
				}
			}
		});
		
		unregisterButton.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				if (fingerprintService != null) {
					fingerprintService.removeUpdates(locationListener);
					isListenerRegistered= false;
				}
			}
		});
		
		singleUpdateButton.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				if (fingerprintService != null) {
					fingerprintService.requestSingleUpdate(locationListener, null);
				}								
			}
		});
	}
	@Override
	protected void onDestroy() {
		if (isListenerRegistered)
			fingerprintService.removeUpdates(locationListener);
		unbindService(fingerprintServiceConnection);
		super.onDestroy();
	}
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
}
