package net.benhui.btgallery.discovery_gui;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.bluetooth.*;
import java.util.*;
import net.benhui.btgallery.Util;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import net.benhui.btgallery.discovery_bt.*;
import net.benhui.btgallery.discovery_gui.*;


/**
 *
 * <p>Title: MIDlet that demostrate Bluetooth Device and Service discovery</p>
 * <p>Description: See DiscoveryImpl for Bluetooth related code.</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * @author Ben Hui (www.benhui.net)
 * @version 1.0
 *
 * LICENSE:
 * This code is licensed under GPL. (See http://www.gnu.org/copyleft/gpl.html)
 *
 */
public class Discovery_MIDlet extends MIDlet implements CommandListener
{
  // commonly used singleton object
  public static Discovery_MIDlet instance;
  public static Display display;

  // GUI component/screen
  public DeviceDiscoveryUI deviceDiscoveryUI = null;
  public ServiceDiscoveryUI serviceDiscoveryUI = null;

  // index of the currently selected RemoteDevice, obtained from DeviceDiscoveryUI
  public static int selectedDevice = -1;

  // Bluetooth logic component
  public static DiscoveryImpl bluetooth = null;


  public Discovery_MIDlet()
  {
    instance = this;
  }

  /**
   * Implements MIDlet lifecycle
   */
  public void startApp()
  {
    display = Display.getDisplay(this);

    deviceDiscoveryUI = new DeviceDiscoveryUI();
    serviceDiscoveryUI = new ServiceDiscoveryUI();

    bluetooth = new DiscoveryImpl();

    // show list of device.
    // it is an empty list at start up
    deviceDiscoveryUI.showui();
    display.setCurrent( deviceDiscoveryUI );
  }

  /**
   * Implements MIDlet lifecycle
   */
  public void pauseApp() {
  }

  /**
   * Implements MIDlet lifecycle
   */
  public void destroyApp(boolean unconditional) {
  }

  /**
   *  exit MIDlet
   */
  public static void quitApp() {
    instance.destroyApp(true);
    instance.notifyDestroyed();
    instance = null;
  }




  /**
   * Handle user action and input.
   * All GUI Command are directed to this function for simplicity of this demo.
   * @param c
   * @param d
   */
  public void commandAction(Command c, Displayable d)
  {
    if ( d == deviceDiscoveryUI && c.getLabel().equals("Discover Devices") )
    {
      // clear previous founding first
      bluetooth.devices.removeAllElements();
      bluetooth.deviceClasses.removeAllElements();

      // perform discovery
      bluetooth.doDiscoverDevice();

      // tell user to wait
      deviceDiscoveryUI.setMsg("[Please Wait...]");


    } else if ( d == deviceDiscoveryUI && c.getLabel().equals("Discover Services") )
    {
      // remove all existing records first
      bluetooth.services.removeAllElements();

      selectedDevice = deviceDiscoveryUI.getSelectedIndex();

      // make sure user did select a device from the GUI list
      if ( selectedDevice == -1 || selectedDevice >= bluetooth.devices.size() )
      {
        alert( "No device selected", deviceDiscoveryUI );
        return;
      }


      RemoteDevice remoteDevice = (RemoteDevice) bluetooth.devices.elementAt( selectedDevice );

      // perform service discovery
      bluetooth.doDiscoverService(remoteDevice);

    } else if ( d == deviceDiscoveryUI && c.getLabel().equals("Exit") )
    {
      // exit application
      quitApp();

    } else if ( d == serviceDiscoveryUI && c.getLabel().equals("Back") )
    {
      // go back to DeviceDiscoveryUI screen
      display.setCurrent( deviceDiscoveryUI );

    }
  }

  /**
   * An utility function to display a log message
   * @param s String
   */
  public static void log(String s)
  {
    System.out.println(s);
  }

  /**
   * An utility function that show a alert box that display an exception message.
   * @param e
   * @param next_screen
   */
  public static void alert( Exception e, Screen next_screen )
  {
    Alert alert = new Alert( "Benhui.net", "Exception: "+e.getClass().getName()+" "+e.getMessage(), null, AlertType.INFO );
    alert.setTimeout( Alert.FOREVER );
    display.setCurrent( alert, next_screen );

  }

  /**
   * An utility function that show a alert box that display a message.
   *
   * @param m String
   * @param next_screen Screen
   */
  public static void alert( String m, Screen next_screen )
  {
    Alert alert = new Alert( "Benhui.net", m, null, AlertType.INFO );
    alert.setTimeout( Alert.FOREVER );
    display.setCurrent( alert, next_screen );

  }

}
