#This file tests the VA staff scheduling manager's patient search functionality


require "selenium-webdriver"
require "rspec"
require "./pages/utilities"
require "./pages/provider_login"
require "./pages/search"
require "./pages/scheduling_manager"
require "./models/appointment_request"
require "./models/facility"
require "./models/patient"
require 'rubygems'
require 'mysql'
include RSpec::Expectations


describe "Scheduling Manager: Story# SCV-1733" do

  before(:all) do

    @driver = Selenium::WebDriver.for :firefox
    @driver.manage().window().maximize()
    @base_url = "http://localhost"
    @driver.manage.timeouts.implicit_wait = 5

    @login = ProviderLogin.new @driver
    @login.loginAsCprs1234()

    @sm = SchedulingManager.new(@driver)
    @sm.openPage(@driver)
  end
  
  after(:all) do
  	@driver.save_screenshot("./loginFailure.png")
  
    @driver.quit
  end
  
  before(:each) do
    puts "---Patient Search Acceptance Test---"
  end
  
  context "Scheduling Manager Patient Search" do
  	it 'hides any previous patient context information when patient search is open: AC #SCV-1896; Test #SCV-1902' do
  		puts ' -hides patient context when search is open test'
  	
  		@sm.patient_search_btn
  	
  		sleep 2
  		
  		expect(@sm.isPatientSearchDisplayed).to eq(true)
  		
  		puts " --previous patient context information visible: #{@sm.patient_info_element.visible?}"
  		expect(@sm.patient_info_element.visible?).to eq(false)
  	end
  	
  	it 'displays patient information in a list: AC #SCV-1898; Test #SCV-1904' do
  		puts ' -displays patient information in a list test'
  	
  		@sm.patient_search_input_element.send_keys 'Ten'
  		@sm.patient_search_input_element.send_keys :return
  		
  		sleep 1
  		
  		results = @driver.find_elements(:xpath, '//*[@id="patient-list"]/li')
  		
  		puts " --results displayed: #{results.size}"
  		expect(results.size).to eq(4)

  		ten_patient = @driver.find_element(:xpath, '//*[@id="patient-list"]/li[4]')

      puts " --patient displayed in list: #{ten_patient.text}"
  		expect(ten_patient.text).to eq("TEN, PATIENT")
  	end
  	
  	it 'displays a confirmation screen when a patient is selected: AC #SCV-1899; Test #SCV-1905' do
  		puts " -displays confirmation screen test"
  	
  		ten_patient = @driver.find_element(:xpath, '//*[@id="patient-list"]/li[4]/div/div/a')
  		
  		ten_patient.click
  		
  		sleep 1
  		
  		puts " --confirmation screen displayed: #{@sm.confirmation_popup_element.visible?}"
  		expect(@sm.confirmation_popup_element.visible?).to be(true)
  		
  		puts " --last name displayed: #{@sm.confirm_last_name}"
  		expect(@sm.confirm_last_name).to eq("Last TEN")
  		
  		puts " --first name displayed: #{@sm.confirm_first_name}"
  		expect(@sm.confirm_first_name).to eq("First PATIENT")
  		
  		puts " --date of birth displayed: #{@sm.confirm_dob}"
  		expect(@sm.confirm_dob).to eq("DOB 04/07/1935")
  		
  		puts " --age displayed: #{@sm.confirm_age}"
  		expect(@sm.confirm_age).to eq("Age 79")
  		
  		puts " --gender displayed: #{@sm.confirm_gender}"
  		expect(@sm.confirm_gender).to eq("Gender Male")
  		
  		puts " --location displayed: #{@sm.confirm_location}"
  		expect(@sm.confirm_location).to eq("Location 7A GEN MED, 736")
  		
  		puts " --ssn displayed: #{@sm.confirm_ssn}"
  		expect(@sm.confirm_ssn).to eq("SSN 666-00-0010")
  	end
  	
  	it 'returns to the list when the close button is selected: AC #SCV-1900; Test #SCV-1906' do
  		puts " -close button returns to list test"
  	
  		@driver.find_element(:xpath, '//*[@id="confirmation"]/a').click
  		
  		sleep 1
  		
  		puts " --confirmation screen displayed: #{@sm.confirmation_popup_element.visible?}"
  		expect(@sm.confirmation_popup_element.visible?).to be(false)
  		
  		results = @driver.find_elements(:xpath, '//*[@id="patient-list"]/li')
  		
  		puts " --results displayed: #{results.size}"
  		expect(results.size).to eq(4)
  		
  		ten_patient = @driver.find_element(:xpath, '//*[@id="patient-list"]/li[4]')

      puts " --patient displayed in list: #{ten_patient.text}"
  		expect(ten_patient.text).to eq("TEN, PATIENT")
  	end
  	
  	it 'brings the patient into focus when the select button is selected: AC #SCV-1900; Test #SCV-1906' do
  		puts " -brings patient into focus test"
  	
  		ten_patient = @driver.find_element(:xpath, '//*[@id="patient-list"]/li[4]/div/div/a')
  		
  		ten_patient.click
  		
  		sleep 2
  		
  		@driver.find_element(:id, 'confirm-button').click
  		
  		sleep 3
  		
  		puts " --patient name displayed in context: #{@sm.patient_info_name}"
  		expect(@sm.patient_info_name).to eq("TEN, PATIENT")
  		
  		puts " --patient ssn displayed in context: #{@sm.patient_info_ssn}"
  		expect(@sm.patient_info_ssn).to eq("666-00-0010")
  	end
  	
  	it 'only displays the patient context on the booking page: AC #SCV-1901; Test #SCV-1907' do
  		puts " -only displays patient context on booking page test"
  	
  		puts " --patient info visible on Booking page: #{@sm.patient_info_element.visible?}"
  		expect(@sm.patient_info_element.visible?).to eq(true)
  		
  		@sm.footer_waitlist_btn
  		
  		sleep 2
  		
  		puts " --patient info visible on Wait List page: #{@sm.patient_info_element.visible?}"
  		expect(@sm.patient_info_element.visible?).to eq(false)
  		
  		@sm.footer_consults_btn
  		
  		sleep 2
  		
  		puts " --patient info visible on Consults page: #{@sm.patient_info_element.visible?}"
  		expect(@sm.patient_info_element.visible?).to eq(false)
  		
  		@sm.footer_veteran_requests_btn
  		
  		sleep 2
  		
  		puts " --patient info visible on Veteran Requests page: #{@sm.patient_info_element.visible?}"
  		expect(@sm.patient_info_element.visible?).to eq(false)
  	end
  	
  	it 'displays the previous context and app location if the user closes out of the search without making a selection: AC #SCV-1897; Test #SCV-1924' do
  		puts " -closing out of search without making a selection test"
  		
  		@sm.footer_booking_btn
  		
  		sleep 2
  		
  		puts " --patient info before opening search: #{@sm.patient_info_name}"
  		expect(@sm.patient_info_name).to eq("TEN, PATIENT")
  		
  		puts " --app is on booking page: #{@sm.active_booking_btn_element.visible?}"
  		expect(@sm.active_booking_btn_element.visible?).to eq(true)
  		
  		@sm.patient_search_btn
  	
  		sleep 2
  		
  		@sm.patient_search_input_element.send_keys 'Ten'
  		@sm.patient_search_input_element.send_keys :return
  		
  		puts " --closing the patient search"
  		@sm.patient_search_close_btn
  		
  		sleep 2
  		
  		puts " --patient info after opening search: #{@sm.patient_info_name}"
  		expect(@sm.patient_info_name).to eq("TEN, PATIENT")
  		
  		puts " --app is on booking page: #{@sm.active_booking_btn_element.visible?}"
  		expect(@sm.active_booking_btn_element.visible?).to eq(true)
  	end
  end
end