module Workstation
  module Auth0Login
    include PageObject

    # migration form elements
    text_field(:migration_email, id: "signup-email")
    text_field(:migration_password, id: "signup-password")
    h2(:activation_confirm, text: "Activate Single Sign On")
    div(:reset_confirm, class: "auth0-lock-name")
    button(:email_next, id: "btn-signup-email")
    button(:password_next, id: "btn-get-password")
    button(:resend_email, id: "btn-resend-verify")
    checkbox(:privacy_confirm, id: "confirm-privacy")
    button(:privacy_agree, id: "btn-privacy-agree")
    # auth0 guardian
    link(:auth0_already_download, class: "auth0-lock-link")
    button(:auth0_submit, class: "auth0-lock-submit")
    span(:back, class: "auth0-lock-back-button")
    text_field(:auth0_mfa_code, class: "auth0-mfa-code")
    checkbox(:auth0_record_code, class: "auth0-mfa-checkbox")
    div(:auth0_lock, class: "auth0-lock-body-content")
    div(:auth0_confirmation, class: "auth0-mfa-confirmation")
    link(:auth0_continue, class: "auth0-lock-link")
    # sms
    link(:sms, text: "SMS")
    text_field(:sms_phonenumber, name: "phoneNumber")
    text_field(:sms_code, name: "code")
    button(:sms_location, name: "location")
    text_field(:sms_country_search, class: ["auth0-lock-input", "auth0-lock-input-search"])
    div(:sms_country_result, class: "auth0-lock-list-code")

    # google authenticator
    link(:google_authenticator, text: "Google Authenticator")
    text_field(:passcode, class: "auth0-lock-input")
    div(:image_container, class: "auth0-mfa-qr")
    img(:qr_code_uri) do
      image_container_element.image_element
    end

    div(:auth0_error_alert, class: ["auth0-global-message", "auth0-global-message-error"])
    div(:auth0_success_alert, class: ["auth0-global-message", "auth0-global-message-success"])

    def google_migrate
      google_authenticator
      wait_until { passcode_element.wait_until(&:present?) }
      # Line extracts the secret key from otpauth URI taken from QR code image element
      passcode_element.set(
        Support::OtpGenerator.generate_otp(
          # We are looking for <img data-qr-data="otpauth://" src="bla">
          # attribute data-qr-data contains the otpauth URI for QR code image
          # In order to generate OTP, we would need to get the secret parameter from the otpauth URI
          Addressable::URI.parse(qr_code_uri_element.attribute("data-qr-data")).query_values["secret"]
        )
      )
      auth0_submit
      check_auth0_record_code
      auth0_submit
      auth0_continue
    end

    def google_login(secret)
      wait_until { auth0_spinner_element.when_not_present }
      wait_until { passcode_element.wait_until(&:present?) }
      passcode_element.set(Support::OtpGenerator.generate_otp(secret))
      auth0_submit
    end

    # rubocop:disable Metrics/AbcSize
    def sms_migrate(phone_number)
      # We can accees all received sms using Twilio's API
      sms
      wait_until { sms_phonenumber_element.wait_until(&:present?) }
      # UI needs the phone number without the country code
      # assuming that we are going to use only US numbers country code is 1
      sms_phonenumber_element.set(phone_number.to_s[1..-1])
      auth0_submit
      wait_until { passcode_element.wait_until(&:present?) }
      passcode_element.set(Support::TwilioClient.read_sms_code(phone_number: "+" + phone_number.to_s))
      auth0_submit
      if auth0_lock_element.element.exist?
        wait_until(5) { auth0_lock_element.when_not_present }
      end
      check_auth0_record_code
      auth0_submit
      auth0_continue
    end
    # rubocop:enable Metrics/AbcSize

    def sms_login(phone_number)
      wait_until { auth0_spinner_element.when_not_present }
      passcode_element.set(Support::TwilioClient.read_sms_code(phone_number: "+" + phone_number.to_s))
      auth0_submit
    end

    def guardian_login(secret)
      wait_until { auth0_spinner_element.when_not_present }
      auth0_already_download
      wait_until { passcode_element.wait_until(&:present?) }
      passcode_element.set(Support::OtpGenerator.generate_otp(secret))
      auth0_submit
    end

    def reset_password
      elements(class: "auth0-lock-input").each do |field|
        field.send_keys("Test123test!")
      end
      auth0_submit
    end
  end
end
