
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;
import org.netbeans.microedition.lcdui.LoginScreen;


/**
 * @author jose
 */
public class logIn extends MIDlet {

    public void startApp() {
        d = Display.getDisplay(this);

        loginScreen = new LoginScreen(Display.getDisplay(this));
        loginScreen.setLabelTexts("Username", "Password");
        loginScreen.setTitle("Login screen");
        loginScreen.addCommand(LoginScreen.LOGIN_COMMAND);


        loginScreen.setPassword("juan");
        loginScreen.setUsername("juan");


        exitCommand = new Command("Exit", Command.EXIT, 0);

        backCommand = new Command("Volver", Command.BACK, 0);


        loginScreen.addCommand(LoginScreen.LOGIN_COMMAND);
        loginScreen.addCommand(exitCommand);

        loginScreen.setCommandListener(new CommandListener() {

            public void commandAction(Command c, Displayable s) {
                if (c == LoginScreen.LOGIN_COMMAND) {
                    try {
                        Authenticate(loginScreen.getUsername(), loginScreen.getPassword());
                    } catch ( Exception e){}

                }
                if (c == exitCommand){
                    notifyDestroyed();
                }
            }
        });

        loginScreen.setBGColor(-3355444);
        loginScreen.setFGColor(0);
        loginScreen.setUseLoginButton(false);
        loginScreen.setLoginButtonText("Login");
        d.setCurrent(loginScreen);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
    private Command exitCommand, backCommand;
    private LoginScreen loginScreen;
    private Display d;


    public Form Authenticate(String username, String password){

        Form form = new Form("Bienvenido");

        conexionBluetooth cb = new conexionBluetooth(this,d);
        cb.sendToServer(username+";"+password);
        
        form.addCommand(backCommand);

        form.setCommandListener(new CommandListener() {

            public void commandAction(Command c, Displayable e) {
                d.setCurrent(loginScreen);
            }
        });

        return form;
    }

    TextField nombreField;
    TextField usernameField;
    TextField passwordField;
    int idNombre, idUsername, idPassword;

    public void mainMenu(){
        d.setCurrent(loginScreen);
    }
}
