#This file tests the VA staff scheduling manager's veteran requests page.
#TODO: modify this class to use a real ORM gem - the db related calls are a hack

require "selenium-webdriver"
require "rspec"
require "./pages/utilities"
require "./pages/provider_login"
require "./pages/search"
require "./pages/veteran_requests"
require "./models/appointment_request"
require "./models/facility"
require "./models/patient"
require 'rubygems'
require 'mysql'

include RSpec::Expectations

describe "Veteran Requests: Story# SCV-1685" do

  before(:all) do
    
    @facility = Facility.new
    @facility.populateAndSaveFacility
    
    @patient = Patient.new
    @patient.populateAndSavePatient

    @ar = AppointmentRequest.new
    @ar.populateAndSaveDefaultAppointmentRequest

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

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

    @vr = VeteranRequests.new(@driver)
    @vr.openPage(@driver)
  end
  
  after(:all) do

    #delete staged test data from the database
    @ar.deleteRecordById(@ar.appointment_request_id)

    @driver.save_screenshot("./loginFailure.png")
    @driver.quit
    #@verification_errors.should == []

  end

  before(:each) do
    puts "---Veteran Requests Acceptance Test---"
  end

  let(:test_patient){{'name' => 'one, patient', 'patient_id' => 'P001', 'ssn' => '666-00-0001'}}

  it 'displays an indicator on the request overview page noting that the veteran is requesting a phone call: AC #SCV-1758; Test #SCV-1765' do
    puts " -displays an indicator on the request details page noting that the veteran is requesting a phone call test (1)"
    
    @vr.footer_veteran_requests_btn_element.click
    
    sleep 3
    
    @driver.execute_script("$('.ui-block-sidebar').scrollTop(1000000)")
    
    last_updated = DateTime.parse(@ar.last_updated_date).strftime('%m/%d/%Y %H:%M:%S')
			
		request_string = test_patient['name'] + 'TEST VAMC 3' + 'Primary Care' + 'Submitted' + last_updated
			
		puts " -selecting an appointment request: #{request_string}"
		@driver.execute_script("$('#appointment-requests-table tbody tr:contains(\"" + request_string + "\") a').trigger('tap')")
		
		sleep 5
		
    expect(@vr.isFirstPhoneRequestedIndicatorDisplayed()).to eq(true)

  end

  it 'displays text on the request overview page indicating that the veteran did not supply appointment request date options: AC #SCV-1759; Test #SCV-1764' do
    puts " -displays an indicator on the request details page noting that the veteran is requesting a phone call test (2)"
    @driver.execute_script("$('.section-inner-wrapper').scrollTop(1000000)")
    sleep 5
    expect(@vr.first_view_option_date_2_element.text).to eq("No Date Selected")
    expect(@vr.first_view_option_date_3_element.text).to eq("No Date Selected")
  end

  it 'displays an indicator on the request details page noting that the veteran is requesting a phone call: AC #SCV-1758; Test #SCV-1765' do
    puts " -displays an indicator on the request details page noting that the veteran is requesting a phone call test (3)"
    @vr.first_process_button_element.click
    wait_for_ajax(60,2)
    expect(@vr.isSecondPhoneRequestedIndicatorDisplayed()).to eq(true)

  end

  it 'displays text on the request detail page indicating that the veteran did not supply appointment request date options: AC #SCV-1759; Test #SCV-1764' do
    puts " -displays an indicator on the request details page noting that the veteran is requesting a phone call test (4)"
    expect(@vr.second_view_option_date_2_element.text).to eq("No Date Selected")
    expect(@vr.second_view_option_date_3_element.text).to eq("No Date Selected")

  end

end