package lemming.auth;

import lemming.HomePage;
import lemming.ui.page.EmptyBasePage;
import lemming.ui.panel.FeedbackPanel;
import lemming.user.User;
import lemming.user.UserDao;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.value.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The sign-in page of the application.
 */
public class SignInPage extends EmptyBasePage {
    /**
     * Determines if a deserialized file is compatible with this class.
     */
    private static final long serialVersionUID = 1L;

    /**
     * A logger named corresponding to this class.
     */
    private static final Logger logger = LoggerFactory.getLogger(SignInPage.class);

    /**
     * Creates a sign-in page.
     */
    public SignInPage() {
        add(new FeedbackPanel("feedbackPanel"));
        add(new SignInForm("signInForm"));
    }

    /**
     * A stateless form which authenticates a user.
     */
    private class SignInForm extends Form<String> {
        /**
         * Determines if a deserialized file is compatible with this class.
         */
        private static final long serialVersionUID = 1L;

        /**
         * Constant used for username property.
         */
        private static final String USERNAME = "username";

        /**
         * Constant used for password property.
         */
        private static final String PASSWORD = "password";

        /**
         * Saves the input of text and password fields.
         */
        private final ValueMap properties = new ValueMap();

        /**
         * Creates a sign-in form.
         * 
         * @param id
         *            ID of a sign-in form
         */
        public SignInForm(String id) {
            super(id);
            add(new TextField<String>("username", new PropertyModel<String>(properties, USERNAME), String.class));
            add(new PasswordTextField("password", new PropertyModel<String>(properties, PASSWORD)));
        }

        /**
         * Submits the form input to authenticate a user.
         */
        @Override
        protected void onSubmit() {
            if (WebSession.get().signIn(properties.getString(USERNAME), properties.getString(PASSWORD))) {
                continueToOriginalDestination();
                setResponsePage(HomePage.class);
            } else {
                User matchingUser = new UserDao().findByUsername(properties.getString(USERNAME));

                if (matchingUser instanceof User) {
                    if (matchingUser.getEnabled()) {
                        error(getString("SignInPage.passwordWrongMessage"));
                        logger.info("Password wrong for user " + properties.getString(USERNAME) + ".");
                    } else {
                        error(getString("SignInPage.userNotEnabledMessage"));
                        logger.info("User not enabled: " + properties.getString(USERNAME) + ".");
                    }
                } else {
                    error(getString("SignInPage.userUnknownMessage"));
                    logger.info("User unknown: " + properties.getString(USERNAME) + ".");
                }
            }
        }
    }
}

@@ -215,20 +215,33 @@
 ml.form.
-Form
+PasswordTextField
 ;%0Aimport
@@ -276,33 +276,29 @@
 ml.form.
-PasswordTextField
+StatelessForm
 ;%0Aimport
@@ -375,16 +375,24 @@
 t.model.
+Compound
 Property
@@ -402,54 +402,8 @@
 el;%0A
-import org.apache.wicket.util.value.ValueMap;%0A
 impo
@@ -690,24 +690,164 @@
 nUID = 1L;%0A%0A
+    /**%0A     * Username string.%0A     */%0A    private String username;%0A%0A    /**%0A     * Password string.%0A     */%0A    private String password;%0A%0A
     /**%0A    
@@ -1148,17 +1148,398 @@
 InForm%22)
-)
+.setDefaultModel(new CompoundPropertyModel(this)));%0A        setStatelessHint(true);%0A    %7D%0A%0A    /**%0A     * Returns the username string.%0A     *%0A     * @return A string.%0A     */%0A    public String getUsername() %7B%0A        return username;%0A    %7D%0A%0A    /**%0A     * Returns the password string.%0A     *%0A     * @return A string.%0A     */%0A    public String getPassword() %7B%0A        return password
 ;%0A    %7D%0A
@@ -1648,20 +1648,21 @@
 nds 
-Form%3CString%3E
+StatelessForm
  %7B%0A 
@@ -1822,413 +1822,8 @@
 L;%0A%0A
-        /**%0A         * Constant used for username property.%0A         */%0A        private static final String USERNAME = %22username%22;%0A%0A        /**%0A         * Constant used for password property.%0A         */%0A        private static final String PASSWORD = %22password%22;%0A%0A        /**%0A         * Saves the input of text and password fields.%0A         */%0A        private final ValueMap properties = new ValueMap();%0A%0A
     
@@ -1901,30 +1901,8 @@
 m id
-%0A         *           
  ID 
@@ -2045,71 +2045,8 @@
 ame%22
-, new PropertyModel%3CString%3E(properties, USERNAME), String.class
 ));%0A
@@ -2097,57 +2097,8 @@
 ord%22
-, new PropertyModel%3CString%3E(properties, PASSWORD)
 ));%0A
@@ -2281,76 +2281,148 @@
 t().
-signIn(properties.getString(USERNAME), properties.getString(PASSWORD
+isTemporary()) %7B%0A                WebSession.get().bind();%0A            %7D%0A%0A            if (WebSession.get().signIn(getUsername(), getPassword(
 ))) 
@@ -2611,37 +2611,20 @@
 ame(
-properties.getString(USERNAME
+getUsername(
 ));%0A
@@ -2875,37 +2875,20 @@
 %22 + 
-properties.getString(USERNAME
+getUsername(
 ) + 
@@ -3063,37 +3063,20 @@
 %22 + 
-properties.getString(USERNAME
+getUsername(
 ) + 
@@ -3254,37 +3254,20 @@
 %22 + 
-properties.getString(USERNAME
+getUsername(
 ) + 

Make the sign in page stateless for better usability.