package fi.cie.chiru.servicefusionar.serviceApi;

import java.util.Vector;

import android.location.Location;

import fi.cie.chiru.servicefusionar.ServiceFusionSetup;
import fi.cie.chiru.servicefusionar.calendar.ServiceFusionCalendar;
import fi.cie.chiru.servicefusionar.sensors.Sensors;

public class ServiceManager 
{
	private static final String LOG_TAG = "ServiceManager";
	private ServiceFusionSetup setup;
	private Vector<ServiceApplication> serviceApplications;
	private MovieManager movieManager;
	private MusicManager musicManager;
	private ServiceFusionCalendar calendar;
	private Sensors sensors = null;
	
	private boolean locationset = false;

	public ServiceManager(ServiceFusionSetup setup)
	{
		this.setup = setup;
		serviceApplications = new Vector<ServiceApplication>();
	}
	
	public ServiceFusionSetup getSetup()
	{
		return setup;
	}
	
	public MovieManager getMovieManager()
	{
		return movieManager;
	}
	
	public ServiceFusionCalendar getCalendar()
	{
		return calendar;
	}
	
	public MusicManager getMusicManager()
	{
		return musicManager;
	}
	
	public Sensors getSensors()
	{
		return sensors;
	}
	
	public void createApplications()
	{
		sensors = new Sensors(this);
		
		SceneParser parser = new SceneParser();
		serviceApplications = parser.parseFile(this, "serviceFusion.txt");
		movieManager = new MovieManager(this);
		musicManager = new MusicManager(this);
		calendar = new ServiceFusionCalendar(this);
		
	}
	
	public ServiceApplication getApplication(String appName)
	{
		ServiceApplication serviceApp = null;
		
		for(int i=0; i<serviceApplications.size(); i++)
		{
		    serviceApp = serviceApplications.elementAt(i);
		    
			if(serviceApplications.elementAt(i).getName().compareTo(appName)==0)
		        return serviceApp;
			else
				serviceApp = null;
		}
		return serviceApp;
	}
	
	public void setVisibilityToAllApplications(boolean visible)
	{
		for(int i=0; i<serviceApplications.size(); i++)
		{
			if(serviceApplications.elementAt(i).getName().contains("Infobubble"))
				continue;
			
			serviceApplications.elementAt(i).setvisible(visible);
		}
		
		calendar.visible(visible);
	
	}
	
	public void geoLocationEstablished(Location location)
	{
		if (!locationset)
		{
			for(int i=0; i<serviceApplications.size(); i++)
			{
				serviceApplications.elementAt(i).servicePlaceFromLocation(location);
			}
			locationset = true;	
		}
	}
	
	public void onDestroy()
	{
		musicManager.onDestroy();
		sensors.onDestroy();
	}
}

@@ -102,16 +102,41 @@
 ocation;
+%0Aimport android.util.Log;
 %0A%0Aimport
@@ -648,35 +648,40 @@
 ate 
-boolean locationset = false
+Location previousLocation = null
 ;%0A%0A%09
@@ -2219,20 +2219,32 @@
 if (
-!locationset
+previousLocation == null
 )%0A%09%09
@@ -2386,32 +2386,519 @@
 %0A%09%09%09
-locationset = true;%09%0A%09%09%7D
+previousLocation = new Location(%22PreviousGpsLocation%22);%0A%09%09%09previousLocation.set(location);%0A%09%09%7D%0A%09%09else%0A%09%09%7B%0A%09%09%09float distance = location.distanceTo(previousLocation);%0A%09%09%09//Log.i(LOG_TAG, %22DistanceTo: %22 + distance);%0A%09%09%09if (distance %3E 20)%0A%09%09%09%7B%0A%09%09%09%09Log.i(LOG_TAG,  %22Setting new positions for services, distance was: %22 + distance);%0A%09%09%09%09for(int i=0; i%3CserviceApplications.size(); i++)%0A%09%09%09%09%7B%0A%09%09%09%09%09serviceApplications.elementAt(i).servicePlaceFromLocation(location);%0A%09%09%09%09%7D%0A%09%09%09%09previousLocation.set(location);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09
 %0A%09%7D%0A

Update service positions if device have moved more than 20 meters.