#This file tests the VA staff scheduling manager's get 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-1970" 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("./get_desired-date-error.png")

    @driver.quit
  end

  before(:each) do
    puts "--- Get Desired Date Acceptance Test---"
  end

  context "Scheduling Manager Get Desired Date" do

    it 'clerk reselection not needed: AC: #SCV-1993; Test #SCV-1997' do

      if (@sm.clear_patient_context_element.visible?)
        @sm.clear_patient_context
      end

      expect(@sm.no_patient_selected_message_element.text).to eq("No Patient Selected")

      @sm.openClinicMenu

      sleep 2

      if (!@sm.clinic1_check_element.visible?)

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

      end

      todaysDate = (DateTime.now + 1).strftime('%m%d%Y')
      @sm.desired_date_element.send_keys todaysDate
      @sm.desired_date_element.send_keys("")
      
      sleep 1

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

      sleep 3

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

      @sm.appointment_button

      expect(@sm.test_vamc3_element.text).to eq("TEST VAMC 3")

      @sm.findPatientTen

      expect(@sm.test_vamc3_element.text).to eq("TEST VAMC 3")

    end

    it 'validation of desired date performed: AC: #SCV-1991; Test #SCV-1995' do

      if (@sm.clear_patient_context_element.visible?)
        @sm.clear_patient_context
      end

      expect(@sm.no_patient_selected_message_element.text).to eq("No Patient Selected")

      @sm.openClinicMenu

      sleep 2

      if (!@sm.clinic1_check_element.visible?)

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

      end

      todaysDate = (DateTime.now + 1).strftime('%m%d%Y')
      
      @sm.desired_date_element.send_keys todaysDate

      sleep 1

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

      sleep 3

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

      @sm.appointment_button

      expect(@sm.test_vamc3_element.text).to eq("TEST VAMC 3")

      @sm.findPatientTen

      expect(@sm.test_vamc3_element.text).to eq("TEST VAMC 3")

      @sm.pickAvailableSlot

      expect(@sm.sdd_popup_element.visible?).to eq(true)

      @sm.close_btn

    end

    it 'desired date entry: AC: #SCV-1992; Test #SCV-1996'do


      if (@sm.clear_patient_context_element.visible?)
        @sm.clear_patient_context
      end

      expect(@sm.no_patient_selected_message_element.text).to eq("No Patient Selected")

      @sm.openClinicMenu

      sleep 2

      if (!@sm.clinic1_check_element.visible?)

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

      end

      todaysDate = (DateTime.now + 1).strftime('%m%d%Y')
      @sm.desired_date_element.send_keys todaysDate
      @sm.desired_date_element.send_keys("")
      
      sleep 1

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

      sleep 3

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

      @sm.appointment_button

      expect(@sm.test_vamc3_element.text).to eq("TEST VAMC 3")

      @sm.findPatientTen

      expect(@sm.test_vamc3_element.text).to eq("TEST VAMC 3")

      @sm.calendar_available_slot_elements[0].click
      
      sleep 1

      expect(@sm.sdd_popup_element.visible?).to eq(true)

      inputDate = (DateTime.now + 1).strftime('%m%d%Y')
      @sm.correct_desired_date_element.send_keys inputDate
      @sm.correct_desired_date_element.send_keys("")

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

      sleep 2

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

    end
    
  end


  end
