/* 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.bluetooth.*;

/**
 * 
 * @author Joseph Paul Cohen
 *
 */
public class RemoteDeviceDiscovery {

	private static boolean debug = false;
	
    public final static Set<RemoteDevice> devicesDiscovered = new HashSet<RemoteDevice>();

    public static void runDiscovery(boolean verbose) throws IOException, InterruptedException{
    	System.out.println("#" + "Searching for devices");
    	RemoteDeviceDiscovery.findDevices();
    	for (RemoteDevice d : devicesDiscovered){
    		
    		System.out.println("-," + deviceName(d,verbose));
    	}
    	
    	System.out.println("#" + "Found " + devicesDiscovered.size() + " device(s)");
    }
    
    
    
    public static void findDevices() throws IOException, InterruptedException {

        final Object inquiryCompletedEvent = new Object();

        devicesDiscovered.clear();

        DiscoveryListener listener = new DiscoveryListener() {

            public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
                if (debug) System.out.println("#" + "Device " + btDevice.getBluetoothAddress() + " found");
                devicesDiscovered.add(btDevice);
                try {
                	if (debug) System.out.println("#" + "     name " + btDevice.getFriendlyName(false));
                } catch (IOException cantGetDeviceName) {
                }
            }

            public void inquiryCompleted(int discType) {
            	if (debug) System.out.println("#" + "Device Inquiry completed!");
                synchronized(inquiryCompletedEvent){
                    inquiryCompletedEvent.notifyAll();
                }
            }

            public void serviceSearchCompleted(int transID, int respCode) {
            }

            public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
            	if (debug) System.out.println("#" + "servicesDiscovered");
            }
        };

        synchronized(inquiryCompletedEvent) {
            
        	//ServiceRecord.
        	
        	LocalDevice ld = LocalDevice.getLocalDevice();
        	//ld.getRecord()
        	
        	if (debug) System.out.println("#My Name is:" + ld.getFriendlyName());
        	
//        	boolean started = LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.LIAC, listener);
//            if (started) {
//            	if (debug) System.out.println("wait for device inquiry to complete...");
//                inquiryCompletedEvent.wait();
//                if (debug) System.out.println(devicesDiscovered.size() +  " device(s) found");
//            }
        	
        	
        	boolean started = LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, listener);
            if (started) {
            	if (debug) System.out.println("#" + "wait for device inquiry to complete...");
                inquiryCompletedEvent.wait();
                if (debug) System.out.println("#" + devicesDiscovered.size() +  " device(s) found");
            }
        }
        
    }

    public static Set<RemoteDevice> getDevices(){
    	
    	return devicesDiscovered;
    }
    
    public static String deviceName(RemoteDevice d, boolean verbose){
    	
    	String address = d.getBluetoothAddress();
		
		String name = "";
		try{
			name = d.getFriendlyName(false);
		}catch (IOException e){
			if (verbose) System.out.println("#Error: " + e.getMessage());
			try{
				name = d.getFriendlyName(false);
			}catch (IOException e2){
    			if (verbose) System.out.println("#Error: " + e2.getMessage());
			}
			
		}
		
		
		return 	(new Date()).getTime() + ", \"" +
				BluCatUtil.clean(address) + "\", " +
				"\"" + BluCatUtil.clean(name) + "\", " +
				"Trusted:" + d.isTrustedDevice() + ", " +
				"Encrypted:" + d.isEncrypted();
    }
    
    
}