#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-1876" 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("./contextFailure.png")
  
    @driver.quit
  end
  
  before(:each) do
    puts "---Patient Context Acceptance Test---"
  end
  
  context "Scheduling Manager Patient Context" do

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

    before(:all) do
      #Stage some data for a Veteran Request
      @facility = Facility.new
      @facility.populateAndSaveFacility

      @patient = Patient.new
      @patient.populateAndSavePatient

      @ar = AppointmentRequest.new
      @ar.populateAndSaveDefaultAppointmentRequest
    end

    after(:all) do
      #Remove the staged data
      puts "remove patient appointment request #{@ar.appointment_request_id}"
      @ar.deleteRecordById(@ar.appointment_request_id)
    end
    
    it 'correctly changes context from EWL item: AC #SCV-1908; Test #SCV-1912' do
  		puts 'correctly changes context from EWl item Test'
  	
  		@sm.footer_waitlist_btn_element.click
  	
  		sleep 3

      @driver.execute_script("$('#ewl-table tbody tr:contains(\"ELEVEN,PATIENTNC\") a').eq(0).trigger('tap')")

      sleep 3

      puts "arePatientDetailsValid: #{@sm.arePatientDetailsValid("ELEVEN", "PATIENT", "Apr 07, 1935", "79", "MALE", "666-00-0011")}"


      expect(@sm.arePatientDetailsValid("ELEVEN", "PATIENT", "Apr 07, 1935", "79", "MALE", "666-00-0011")).to eq(true)

      @sm.process_request

      sleep 3

      puts " --patient name displayed in context: #{@sm.patient_info_name}"
      expect(@sm.patient_info_name).to eq("ELEVEN, PATIENT")

      puts " --patient ssn displayed in context: #{@sm.patient_info_ssn}"
      expect(@sm.patient_info_ssn).to eq("666-00-0011")

  	end


  	it 'correctly changes context from Near list item: AC #SCV-1908; Test #SCV-1912' do
  		puts 'correctly changes context from Near list item Test'

      @sm.footer_waitlist_btn_element.click

      sleep 3

      @sm.nearlist_header_element.click

      @driver.execute_script("$('#near-table tbody tr:contains(\"SEVEN,PATIENT50 -03/01/2014\") a').trigger('tap')")

      sleep 3

      puts "arePatientDetailsValid: #{@sm.arePatientDetailsValid("SEVEN", "PATIENT", "Apr 07, 1935", "79", "MALE", "666-00-0007")}"


      expect(@sm.arePatientDetailsValid("SEVEN", "PATIENT", "Apr 07, 1935", "79", "MALE", "666-00-0007")).to eq(true)

      @sm.process_request

      sleep 3

      puts " --patient name displayed in context: #{@sm.patient_info_name}"
      expect(@sm.patient_info_name).to eq("SEVEN, PATIENT")

      puts " --patient ssn displayed in context: #{@sm.patient_info_ssn}"
      expect(@sm.patient_info_ssn).to eq("666-00-0007")

    end


    it 'correctly changes context from Recall list item: AC #SCV-1908; Test #SCV-1912' do
      puts 'correctly changes context from Recall list item Test'

      @sm.footer_waitlist_btn_element.click

      sleep 3

      @driver.execute_script("$('.ui-block-sidebar').scrollTop(1000000)")

      @sm.recalllist_header_element.click

      @driver.execute_script("$('#recall-table tbody tr:contains(\"BCMA,EIGHTEEN-PATIENTCARDIOLOGY05/26/2013\") a').trigger('tap')")

      sleep 3

      puts "arePatientDetailsValid: #{@sm.arePatientDetailsValid("BCMA", "EIGHTEEN-PATIENT", "Apr 07, 1935", "79", "MALE", "666-33-0018")}"


      expect(@sm.arePatientDetailsValid("BCMA", "EIGHTEEN-PATIENT", "Apr 07, 1935", "79", "MALE", "666-33-0018")).to eq(true)

      @sm.process_request

      sleep 3

      puts " --patient name displayed in context: #{@sm.patient_info_name}"
      expect(@sm.patient_info_name).to eq("BCMA, EIGHTEEN-PATIENT")

      puts " --patient ssn displayed in context: #{@sm.patient_info_ssn}"
      expect(@sm.patient_info_ssn).to eq("666-33-0018")


    end


    it 'correctly changes context from veteran requests item: AC #SCV-1910; Test #SCV-1914' do
      puts 'correctly changes context from veteran request item Test'

      @sm.footer_veteran_requests_btn_element.click

      sleep 3

      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"
      @driver.execute_script("$('#appointment-requests-table tbody tr:contains(\"" + request_string + "\") a').trigger('tap')")

      sleep 3

      puts "arePatientDetailsValid: #{@sm.arePatientDetailsValid("ONE", "PATIENT", "Apr 07, 1935", "79", "MALE", "666-00-0001")}"

      expect(@sm.arePatientDetailsValid("ONE", "PATIENT", "Apr 07, 1935", "79", "MALE", "666-00-0001")).to eq(true)

      @sm.process_request

      sleep 3

      puts " --patient name displayed in context: #{@sm.patient_info_name}"
      expect(@sm.patient_info_name).to eq("ONE, PATIENT")

      puts " --patient ssn displayed in context: #{@sm.patient_info_ssn}"
      expect(@sm.patient_info_ssn).to eq("666-00-0001")
    end

    it 'correctly shows patient information on consults details: AC #SCV-1909; Test #SCV-1913' do
      puts 'correctly shows patient information on consults details Test'

      @sm.footer_consults_btn_element.click

      sleep 3

      @sm.consults_specialty_field_element.send_keys 'Rad'

      sleep 1

      @sm.consults_rad_service_element.click

      sleep 3

      @driver.execute_script("$('#consults-filter-table tbody tr:contains(\"TEN,PATIENTPENDING09/10/2013\") a').trigger('tap')")

      sleep 3

      puts "arePatientDetailsValid: #{@sm.arePatientDetailsValid("TEN", "PATIENT", "Apr 07, 1935", "79", "MALE", "666-00-0010")}"

      expect(@sm.arePatientDetailsValid("TEN", "PATIENT", "Apr 07, 1935", "79", "MALE", "666-00-0010")).to eq(true)
    end
  end
end