package de.uulm.mi.ubicom.proximity.proximity_periphery_bluetooth_test.actors;

import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

import de.uulm.mi.ubicom.proximity.lib.BroadcastActor;
import de.uulm.mi.ubicom.proximity.proximity_periphery_bluetooth_test.reactors.BluetoothServiceReactor;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Parcelable;


public class BluetoothServiceActor extends BroadcastActor<BluetoothServiceReactor>{

	private final ConcurrentHashMap<BluetoothDevice,Vector<String>> devices;
	private volatile int servicesDiscovered = 0;
	public BluetoothServiceActor(BluetoothServiceReactor reactor) {
		super(reactor);
		devices = new ConcurrentHashMap<BluetoothDevice,Vector<String>>();
		
		
	}

	@Override
	public void onReceive(Context context, Intent intent) {
	     if(intent.getAction().equals(BluetoothDevice.ACTION_FOUND)) {
	    	 //device found
	    	 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
	    	 devices.put(device, new Vector<String>());
	    	 device.fetchUuidsWithSdp();
	    	 reactor.onDeviceFound(device);
	    	 
	     }else if(intent.getAction().equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
	    	 //device discovery finished
	    	 BluetoothDevice[] deviceArray = new BluetoothDevice[devices.keySet().size()];
	    	 deviceArray = devices.keySet().toArray(deviceArray);
	    	 reactor.onDeviceDiscoveryFinished(deviceArray);
	    	 
	     }else if(intent.getAction().equals(BluetoothDevice.ACTION_UUID)){
	    	 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
	    	 Vector<String> uuids = devices.get(device);
	         Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
	         if (uuidExtra != null){
	        	 for (int i=0; i<uuidExtra.length; i++) {
	  	           uuids.add(uuidExtra[i].toString());
	  	         }	 
	         }
	         reactor.onServicesDiscovered(device, uuids);
	         if (servicesDiscovered==devices.size()){
	        	 reactor.onServiceDiscoveryFinished(devices);
	         }
	         servicesDiscovered++;
	     }else if(intent.getAction().equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)){
	    	 reactor.onServiceDiscoveryStarted();
	     }
	       
	        
		
	}
	
	public void register(Activity a){
		IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
	    filter.addAction(BluetoothDevice.ACTION_UUID);
	    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
	    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
	    a.registerReceiver(this, filter);
	}
	


}

@@ -1161,24 +1161,126 @@
 RA_DEVICE);%0A
+%09    %09 if (devices.get(device) != null)%7B%0A%09    %09%09 //device already discovered%0A%09    %09%09 return;%0A%09    %09 %7D%0A
 %09    %09 devic
@@ -2252,24 +2252,56 @@
 ce, uuids);%0A
+%09         servicesDiscovered++;%0A
 %09         if
@@ -2404,28 +2404,17 @@
         
- %7D%0A%09        
+%09
  service
@@ -2420,27 +2420,41 @@
 esDiscovered
-++;
+ = 0;%0A%09         %7D
 %0A%09     %7Delse

handle uuids?