package sms;

import java.io.IOException;
import java.util.Vector;
import javax.bluetooth.*;

/**
 * Trieda pre najdenie sluzieb na zariadeni
 * @author Richard Scepko
 */
public class ServicesSearch {
    /**
     * Jedinecne ID pre transport suborov
     */
    static final UUID OBEX_FILE_TRANSFER = new UUID(0x1106);
    /**
     * Jedinecne ID pre push
     */
    static final UUID OBEX_OBJECT_PUSH = new UUID(0x1105);
    /**
     * Vektor najdenych sluzieb na zaradeni
     */
    public Vector serviceFound = new Vector();
    /**
     * Objekt triedy DeviceDiscovery pre pristup k najdenym zariadeniam
     */
    private DeviceDiscovery deviceDiscovery;

    /**
     * Konstruktor triedy
     * @param deviceDiscovery Objekt triedy DeviceDiscovery pre pristup k najdenym zariadeniam
     */
    public ServicesSearch(DeviceDiscovery deviceDiscovery){
        this.deviceDiscovery = deviceDiscovery;
    }
    /**
     * Spusti hladanie sluzieb
     * @param args Adresa servera
     * @param deviceIndex Index vybraneho zariadenia s listu zariadeni
     * @throws IOException
     * @throws InterruptedException
     */
    public void run(String[] args, int deviceIndex) throws IOException, InterruptedException{
        serviceFound.clear();

        UUID serviceUUID = OBEX_OBJECT_PUSH;
        if ((args != null) && (args.length > 0)) {
            serviceUUID = new UUID(args[0], false);
        }

        final Object serviceSearchCompletedEvent = new Object();

        DiscoveryListener listener = new DiscoveryListener() {

            public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
            }

            public void inquiryCompleted(int discType) {
            }

            /**
             * Vnutorna trieda pre najdenie sluzieb na jednom zariadeni
             */
            public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
                for (int i = 0; i < servRecord.length; i++) {
                    String url = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
                    if (url == null) {
                        continue;
                    }
                    //nasla sa sluzba
                    serviceFound.add(url);
                    DataElement serviceName = servRecord[i].getAttributeValue(0x0100);
                    if (serviceName != null) {
                        System.out.println("service " + serviceName.getValue() + " found " + url);
                    } else {
                        System.out.println("service found " + url);
                    }
                }
            }
            /**
             * Vnutorna metoda, ktora sa vyvola po uspesnom dokonceni hladania
             */
            public void serviceSearchCompleted(int transID, int respCode) {
                System.out.println("service search completed!");
                synchronized(serviceSearchCompletedEvent){
                    serviceSearchCompletedEvent.notifyAll();
                }
            }

        };

        UUID[] searchUuidSet = new UUID[] { serviceUUID };
        int[] attrIDs =  new int[] {
                0x0100 // Service name
        };

        RemoteDevice btDevice = (RemoteDevice)this.deviceDiscovery.devicesDiscovered.get(deviceIndex);

        synchronized(serviceSearchCompletedEvent) {
            System.out.println("search services on " + btDevice.getBluetoothAddress() + " " + btDevice.getFriendlyName(false));
            LocalDevice.getLocalDevice().getDiscoveryAgent().searchServices(attrIDs, searchUuidSet, btDevice, listener);
            serviceSearchCompletedEvent.wait();
        }


    }
}
