module YouTube
  class LoginPage < GoogleSSOPage
    # This is a google login url that avoids security checks for automation and allows 2FA
    page_url "https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?redirect_uri=https"\
      "%3A%2F%2Fdevelopers.google.com%2Foauthplayground&prompt=consent&response_type=code&client_id"\
      "=407408718192.apps.googleusercontent.com&scope=email&access_type=offline&flowName=GeneralOAuthFlow"

    def login
      username = ENV['YOUTUBE_CMS_USER']
      password = ENV['YOUTUBE_CMS_PASS']
      auth_key = ENV['YOUTUBE_CMS_AUTH_TOKEN']
      google_sso_login(username, password, auth_key)
    end

    def failure_message
      return nil unless login_failure? || !logged_in?
      if !login_failure_element.nil? && login_failure_element.element.present? # short-circuit when el returns nil
        "Login failure message! -- #{login_failure_element.text} -- Check credentials in .env!"
      else
        "Unknown login failure! Current URL: #{current_url}"
      end
    end
  end
end
