#This file tests the VA staff scheduling manager's desired date 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-1754" do

  before(:all) do

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

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

    @sm = SchedulingManager.new(@driver)
    @sm.openPage(@driver)
  end
  
  after(:all) do
  	@driver.save_screenshot("./desired-date-error.png")
  
    @driver.quit
  end
  
  before(:each) do
    puts "---Desired Date Acceptance Test---"
  end
  
  context "Scheduling Manager Desired Date" 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 'allows a user to select a desired date from the date picker if it doesn\'t exist: AC: #SCV-1860; Test #SCV-1862' do
		#pending "test code implementation"
		sleep 1

		todaysDate = (DateTime.now).strftime('%m/%d/%Y')

		@sm.openClinicMenu

		#select a date through the date picker
		@sm.date_picker_btn

		sleep 2

		#@driver.execute_script('$("#set-btn").trigger("click");')
		@sm.date_picker_set_btn

		expect(@sm.desired_date).to eq(todaysDate)

		puts " -value of the Desired Date field: #{@sm.desired_date}"

		@sm.close_btn
	end

	it 'requires a desired date to view availability: AC: #SCV-1859; Test #SCV-1861' do

		sleep 1
		
		todaysDate = (DateTime.now + 1).strftime('%m%d%Y')

		@sm.openClinicMenu

		sleep 2

		@driver.execute_script('$("#clinic-0").trigger("click");')

		sleep 2

		expect(@sm.appointment_button_element.visible?).to eq(false)

		@sm.desired_date_element.send_keys todaysDate

		sleep 1

		@driver.execute_script('$("#clinic-1").trigger("click");')

		sleep 1

		expect(@sm.appointment_button_element.visible?).to eq(true)
	end

	it 'should only be allowed to select a date within 90 days from today. : AC: #SCV-1962; Test #SCV-1963' do

		sleep 1
		
		dateWithin90Days = (DateTime.now + 90).strftime('%m%d%Y')
		datePast90Days = (DateTime.now + 91).strftime('%m%d%Y')

		@sm.openClinicMenu

		sleep 2

		@driver.execute_script('$("#clinic-0").trigger("click");')

		@sm.desired_date_element.send_keys datePast90Days

		@driver.execute_script("$('#desired-date').trigger('blur');")

		sleep 2

		expect(@sm.appointment_button_element.visible?).to eq(false)

		@driver.execute_script("$('#desired-date').val('');")

		@sm.desired_date_element.send_keys dateWithin90Days

		@driver.execute_script('$("#clinic-1").trigger("click");')

		sleep 3

		expect(@sm.appointment_button_element.visible?).to eq(true)
	end

	it 'should not be allowed to select a date in the past. : AC: #SCV-1960; Test #SCV-1961' do

		sleep 1
		
		dateWithin90Days = (DateTime.now + 1).strftime('%m%d%Y')
		dateInThePast = (DateTime.now - 1).strftime('%m%d%Y')

		@sm.openClinicMenu

		sleep 2

		@driver.execute_script('$("#clinic-0").trigger("click");')

		@sm.desired_date_element.send_keys dateInThePast

		@driver.execute_script("$('#desired-date').trigger('blur');")

		sleep 2

		expect(@sm.appointment_button_element.visible?).to eq(false)

		@driver.execute_script("$('#desired-date').val('');")

		@sm.desired_date_element.send_keys dateWithin90Days

		@driver.execute_script('$("#clinic-1").trigger("click");')

		sleep 3

		expect(@sm.appointment_button_element.visible?).to eq(true)
	end

	it 'displays the desired date from a Veteran Request: AC #SCV-1848; Test #SCV-1850' do
		puts " -displays the desired date from a Veteran Request test"
    
	   	sleep 1

	   	@sm.footer_veteran_requests_btn

		sleep 5
	
		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')")

	   	@desired_date = (DateTime.now + 10).strftime('%m/%d/%Y')

	   	sleep 3
	   	
		@driver.execute_script("$('.section-inner-wrapper').scrollTop(1000000)")
		puts " -veteran request preferred date: #{@sm.option_date_1_element.text}"
 	   	expect(@sm.option_date_1_element.text).to eq(@desired_date + " PM")
    
		sleep 3

		@driver.execute_script("$('.section-inner-wrapper').scrollTop(0)")

		@sm.process_request

		sleep 1
    
	   	@sm.openClinicMenu

	   	puts " -desired date: #{@sm.desired_date}"
	  	expect(@sm.desired_date).to eq(@desired_date)
	
		@sm.close_btn
		@sm.clear_patient_context
	end
		
	it 'displays the desired date from an EWL item: AC #SCV-1848; Test #SCV-1850' do
		sleep 1
	
		@sm.footer_waitlist_btn

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

		puts " -EWL desired date: #{@sm.ewl_desired_date}"
		expect(@sm.ewl_desired_date).to eq("08/05/2014")
		
		sleep 3
	
		@sm.process_request
	
		sleep 2
	
		@sm.openClinicMenu
	
		puts " -desired date: #{@sm.desired_date}"
		expect(@sm.desired_date).to eq("08/05/2014")
	
		@sm.close_btn
		@sm.clear_patient_context
	end

  	it 'displays the desired date from a NEAR list item: AC #SCV-1848; Test #SCV-1850' do 
		@sm.footer_waitlist_btn
	
		sleep 2
	
		@driver.find_element(:xpath, '//*[@id="near"]/h3/a').click
	
		@driver.execute_script("$('#near-table tbody tr:contains(\"SEVEN,PATIENT50 -03/01/2014\") a').trigger('tap')")
	
		sleep 4

		puts " -NEAR request date: #{@sm.near_desired_date}"
		expect(@sm.near_desired_date).to eq("03/01/2014")
	
		sleep 3
	
		@sm.process_request
	
		sleep 2
	
		@sm.openClinicMenu
	
		puts " -desired date: #{@sm.desired_date}"
		expect(@sm.desired_date).to eq("03/01/2014")
	
		@sm.close_btn
		@sm.clear_patient_context
	end
	
	it 'displays the desired date from a Recall list item: AC #SCV-1848; Test #SCV-1850' do
		sleep 1

		@sm.footer_waitlist_btn
	
		sleep 6

		@driver.execute_script("$('.ui-block-sidebar').scrollTop(1000000)")
		@driver.find_element(:xpath, '//*[@id="recall"]/h3/a').click
	
		@driver.execute_script("$('#recall-table tbody tr:contains(\"BCMA,EIGHTEEN-PATIENTCARDIOLOGY05/26/2013\") a').trigger('tap')")
	
		sleep 4

		puts " -Recall date: #{@sm.recall_desired_date}"
		expect(@sm.near_desired_date).to eq("05/26/2013")
	
		sleep 3
	
		@sm.process_request
	
		sleep 1
	
		@sm.openClinicMenu
	
		puts " -desired date: #{@sm.desired_date}"
		expect(@sm.desired_date).to eq("05/26/2013")
	
		@sm.close_btn
		@sm.clear_patient_context
	end
		
	it 'doesn\'t allow a user to modify an existing desired date: AC #SCV-1857; Test #SCV-1858' do
		sleep 1
	
		@sm.footer_waitlist_btn
	
		sleep 6
	
		@driver.execute_script("$('#ewl-table tbody tr:contains(\"ELEVEN,PATIENTNC\") a').eq(0).trigger('tap')")
	
		sleep 5
	
		@sm.process_request
	
		sleep 2
	
		@sm.openClinicMenu
	
		puts " -desired date field is disabled: #{@sm.desired_date_element.attribute("disabled")}"
		expect(@sm.desired_date_element.attribute("disabled")).to eq("true")
	
		puts " -date picker button is disabled: #{@sm.date_picker_btn_element.attribute("disabled")}"
		expect(@sm.date_picker_btn_element.attribute("disabled")).to eq("true")
	
		@sm.close_btn
		@sm.clear_patient_context
	end
		
	it 'allows a user to enter a desired date in the text field if it doesn\'t exist: AC: #SCV-1860; Test #SCV-1862' do
		sleep 1
		
		@sm.footer_booking_btn
		
		sleep 2

		@sm.openClinicMenu
	
		puts " -entering the numbers 05000004 into the Desired Date field"
		@sm.desired_date_element.send_keys '05000004'
	
		puts " -value of the Desired Date field: #{@sm.desired_date}"
		expect(@sm.desired_date).to eq('05/19/2014')
	
		@sm.close_btn
	end

  end
end