module OA
  class LoginPage < GoogleSSOPage
    page_url "/please_login_v2.php"

    main(:login_form, class: "login")
    text_field(:username, name: "username")
    # text_field(:password, name: "password")
    radio_button_group(:remember_me, name: "remember_me")
    button(:login_with_google, "data-provider": "google-apps")
    cell(:message_text) { login_form_element.cell_element }
    div(:error_message, class: "error-message")
    button(:login_again, text: "Click here to login Again")

    def login_with_user_data(user)
      login_with user[:email], user[:password]
    end

    def login_with(email, password)
      login_again if login_again?
      self.username = email
      login_with_google
      google_sso_login(email, password, ENV["OA_AUTOMATION_USER_GOOGLE_AUTHENTICATOR_KEY"])
    end

    def on_login_form?
      on_form = false
      on_form = true if login_form? || error_message_element.present?
      on_form
    end
  end
end
