require_relative "_config"

class ProviderLogin
  attr_accessor :authServices

  def initialize(driver)
    @driver = driver
    @authServices = "/AuthorizationServices/logout"
  end

  def loginWithCode (username, password, facilityCode, facilityName)
    wait = Selenium::WebDriver::Wait.new(:timeout => 80)
    @driver.get($acp_url + @authServices)
    @driver.get($acp_url + "/" + $acp_app)
    wait.until { @driver.find_element(:id, "name-c").displayed? }

    @driver.find_element(:id, "name-c").send_keys username
    @driver.find_element(:id, "password").send_keys password
    @driver.execute_script("document.getElementById('facilityCode').value = '"+ facilityCode + "';")
    @driver.execute_script("document.getElementById('facilityName').value = '"+ facilityName + "';")
    @driver.execute_script("document.getElementById('va-hospital').value = '"+ facilityName + "';")
    @driver.find_element(:id, 'loginButton').click

    # accept SUD if present
    !5.times{
      if (element_present?(:id, "portal-staff-user-disclaimer"))
        @driver.find_element(:id, "accept-btn").click()
        break
      end
        sleep 1
    }
  end

  def loginAsStaff01()
    loginWithCode('zztest.staff01', 'pass', '688', 'WASHINGTON')
  end

  def loginAsScvtest2()
    loginWithCode('zztest.scvtest2', 'pass', '777', 'TEST VAMC 3')
  end
  
  def loginAsCprs1234()
    loginWithCode('cprs1234', 'cprs4321$', '777', 'TEST VAMC 3')
  end
end
