/**
 * 
 */
package org.javarosa.user.api;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Item;

import org.javarosa.core.api.State;
import org.javarosa.core.services.locale.Localization;
import org.javarosa.j2me.view.J2MEDisplay;
import org.javarosa.user.api.transitions.LoginStateTransitions;
import org.javarosa.user.model.User;
import org.javarosa.user.view.LoginForm;

/**
 * @author ctsims
 *
 */
public class LoginState implements State<LoginStateTransitions>, CommandListener {
	
	LoginStateTransitions transitions;
	LoginForm view;
	
	public LoginState() {
		
	}

	public void enter(LoginStateTransitions transitions) {
		this.transitions = transitions;
		view = new LoginForm();
		view.setCommandListener(this);
		view.setPasswordMode(AddUserState.PASSWORD_FORMAT_NUMERIC);
	}
	
	public void start() {
		J2MEDisplay.getDisplay().setCurrent(view);
	}

	/*
	 * listener for the String Item (login button)
	 * 
	 * @see
	 * javax.microedition.lcdui.ItemCommandListener#commandAction(javax.microedition
	 * .lcdui.Command, javax.microedition.lcdui.Item)
	 */
	public void commandAction(Command c, Item item) {
		if (c == LoginForm.CMD_LOGIN_BUTTON) {

			// user trying to login, must be validated..
			if (this.view.validateUser()) {
				transitions.loggedIn(view.getLoggedInUser());
				return;

			}
			showError(Localization.get("activity.login.loginincorrect"), Localization.get("activity.login.tryagain"), this);

		}

		// #if javarosa.login.demobutton
		else if (c == LoginForm.CMD_DEMO_BUTTON) {
			showError(Localization.get("activity.login.demomode"), Localization.get("activity.login.demomode.intro"), this);
		}
		// #endif

	}

	// listener for the display
	public void commandAction(Command c, Displayable d) {
		if (c == LoginForm.CMD_CANCEL_LOGIN) {
			transitions.exit();
		} else if (c == LoginForm.CMD_LOGIN_BUTTON) {
			if (this.view.validateUser()) {
				transitions.loggedIn(view.getLoggedInUser());
				return;
			}
			showError(Localization.get("activity.login.loginincorrect"), Localization.get("activity.login.tryagain"));

		}

		// #if javarosa.login.demobutton
		else if (c == LoginForm.CMD_DEMO_BUTTON) {
			showError(Localization.get("activity.login.demomode"), Localization.get("activity.login.demomode.intro"), this);
		} else if (d instanceof Alert) {
			// returning from demo mode warning popup
			User u = new User();
			u.setUsername(User.DEMO_USER); // NOTE: Using a user type as a
			// username also!
			u.setUserType(User.DEMO_USER);
			transitions.loggedIn(u);
		}
		// #endif
	}

	private void showError(String s, String message) {
		showError(s, message, this);
	}

	private void showError(String s, String message, CommandListener cl) {
		J2MEDisplay.showError(s, message);
	}

}

@@ -719,24 +719,57 @@
 m view;%0D%0A%09%0D%0A
+%09Alert demoModeAlert = null;%0D%0A%09%0D%0A
 %09public Logi
@@ -1548,32 +1548,44 @@
 rn;%0D%0A%0D%0A%09%09%09%7D%0D%0A%09%09%09
+J2MEDisplay.
 showError(Locali
@@ -1672,22 +1672,16 @@
 yagain%22)
-, this
 );%0D%0A%0D%0A%09%09
@@ -1762,32 +1762,60 @@
 O_BUTTON) %7B%0D%0A%09%09%09
+demoModeAlert = J2MEDisplay.
 showError(Locali
@@ -1895,32 +1895,38 @@
 emomode.intro%22),
+ null,
  this);%0D%0A%09%09%7D%0D%0A%09%09
@@ -2250,24 +2250,36 @@
 ;%0D%0A%09%09%09%7D%0D%0A%09%09%09
+J2MEDisplay.
 showError(Lo
@@ -2468,16 +2468,44 @@
 ) %7B%0D%0A%09%09%09
+demoModeAlert = J2MEDisplay.
 showErro
@@ -2601,16 +2601,22 @@
 intro%22),
+ null,
  this);%0D
@@ -2635,19 +2635,19 @@
  (d 
-instanceof 
+== demoMode
 Aler
@@ -2904,215 +2904,7 @@
 %0D%0A%0D%0A
-%09private void showError(String s, String message) %7B%0D%0A%09%09showError(s, message, this);%0D%0A%09%7D%0D%0A%0D%0A%09private void showError(String s, String message, CommandListener cl) %7B%0D%0A%09%09J2MEDisplay.showError(s, message);%0D%0A%09%7D%0D%0A%0D%0A
 %7D%0D%0A

fix broken demo mode warning popup in login activity