package br.com.objectos.testing.ui.login.iter7;

import br.com.objectos.way.ui.AbstractUiAction2;
import br.com.objectos.way.ui.Action;
import br.com.objectos.way.ui.Ui;

@Ui
abstract class LoginAction7 extends AbstractUiAction2<String, String> {

  abstract UiHomeActivity7 homeActivity();
  abstract UiLoginForm7 form();

  LoginAction7() {
  }

  @Override
  public Action execute(String login, String password) {
    if (loginValid(login, password)) {
      return homeActivity().setUser(findUserByLogin(login))
          .and(homeActivity().startActivity());
    } else {
      return form().setErrorMessage("Invalid credentials!");
    }
  }

  private UiUser findUserByLogin(String login) {
    User user = AutoUser.of("Mr. " + login);
    return UiUser.from(user);
  }

  private boolean loginValid(String login, String password) {
    return login.equals("foo") && password.equals("bar");
  }

}