
package org.apache.jackrabbit.oak.bark.web.login

import org.apache.jackrabbit.oak.bark.web.BaseTemplatePage
import org.apache.wicket.Component
import org.apache.wicket.markup.html.form.Button
import org.apache.wicket.markup.html.form.Form
import org.apache.wicket.markup.html.form.PasswordTextField
import org.apache.wicket.markup.html.form.RequiredTextField
import org.apache.wicket.markup.html.panel.FeedbackPanel
import org.apache.wicket.model.PropertyModel

class Login extends BaseTemplatePage {

  var user: String = "";
  var pass: String = "";

  def buildForm(): Component = {
    val form = new Form[Void]("loginForm");
    form.setOutputMarkupId(true);

    val u = new RequiredTextField[String]("u", new PropertyModel[String](
      this, "user"))
    val p = new PasswordTextField("p", new PropertyModel[String](
      this, "pass"));

    val submit = new Button("submit") {
      override def onSubmit() {
        val user: String = u.getModelObject;
        val pass: String = p.getModelObject;
        getS.login(user, pass) match {
          case Right(u) ⇒ {
            continueToOriginalDestination();
            setResponsePage(getApplication().getHomePage());
          }
          case Left(u) ⇒ {
            error(u);
          }
        }
      }
    };

    form.add(u);
    form.add(p);
    form.add(submit);
    form.setDefaultButton(submit);
    return form;
  }

  setStatelessHint(true);
  add(buildForm());
  add(new FeedbackPanel("feedback"));

}