#This file tests the VA staff scheduling manager's 'container' functionality (nav buttons, patient display,
# about menu, etc.).
#

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


describe "Scheduling Manager: Story# SCV-1587, SCV-1732, SCV-1685" do

  before(:all) do

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

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

    @sm = SchedulingManager.new(@driver)
    @sm.openPage(@driver)
    
    @desired_date = (DateTime.now + 10).strftime('%m/%d/%Y')
  end
  
  after(:all) do
  	@driver.save_screenshot("./scheduling-manager-error.png")
  
    @driver.quit
  end
  
  before(:each) do
    puts "---Scheduling Manager Acceptance Test---"
  end
  
  context "Scheduling Manager Left Panel Navigation" do
    
    before(:all) do
      @sm.openLeftPanel
    end

    before(:each) do
      puts "---Left Panel Navigation Test---"
    end

    after(:all) do
      @sm.closeLeftPanel
    end
    
    context 'About button' do

      it 'displays an about button and about section when pushing on the about button: AC# SCV-1662, SCV-1663; Test# SCV-1665, SCV-1664' do
        puts " -displays an about button test"
        @sm.openAboutPopup()
        expect(@sm.getAboutPopupTitleText()).to eq("About")
      end
      
      it 'displays the about section containing the name of the application and the current version: AC# SCV-1663; Test# SCV-1664' do
        puts " -displays the about section containing the name of the application and the current version test" 
        expect(@sm.getAppName()).to eq("Scheduling Manager")
        expect(@sm.getAppVersion()).to include("v") #purposely not specifying a version so that this test won't break with every version change
      end
      
      it 'displays the about section containing a brief overview of the application: AC# SCV-1663; Test# SCV-1664' do
        puts " -displays the about section containing a brief overview of the application test" 
        @sm.about_app_overview
        expect(@sm.about_app_overview).to include("Clerk", "book", "appointment", "waitlists", "consults") #purposely not specifying a version so that this test won't break with every version change
      end

      it 'displays the about section containing a brief list of information specific to the application: AC# SCV-1663; Test# SCV-1664' do
        puts " -displays the about section containing a brief list of information specific to the application test"
        @sm.about_accessing_app_description
        expect(@sm.about_accessing_app_description).to include("web based", "accessed through the Launchpad") #purposely somewhat vague so that this test won't break with every text change
        expect(@sm.about_booking_heading).to eq("Booking") 
        expect(@sm.about_booking_selecting_patient_heading).to eq("Selecting a Patient") 
        expect(@sm.about_booking_selecting_specialty_heading).to eq("Selecting a Specialty, Clinic and Patient") 
        expect(@sm.about_booking_calendar_month_view_heading).to eq("Calendar Month View") 
        expect(@sm.about_booking_appointment_heading).to eq("Booking an Appointment") 
        expect(@sm.about_cancelling_apointment_heading).to eq("Canceling an Appointment") 
        expect(@sm.about_wait_list_heading).to eq("Waitlist") 
        expect(@sm.about_consult_orders_heading).to eq("Consult Orders") 
        expect(@sm.about_vaac_heading).to eq("Veteran Requests")
      end

      it 'displays the about section containing a link to the user guide: AC# SCV-1663; Test# SCV-1664' do
        puts " -displays the about section containing a link to the user guide test" 
        expect(@sm.user_guide_element.attribute('href')).to include("Training", "Guide") #purposely not specifying a version so that this test won't break with every version change
      end

      it 'displays the about section containing a Done or close button to close the about section: AC# SCV-1663; Test# SCV-1664' do
        puts " -displays the about section containing a Done or close button to close the about section test" 
        expect(@sm.about_done_button_element.attribute('title')).to include("Close", "About", "Dialog") #purposely not specifying a version so that this test won't break with every version change
        @sm.about_done_button
        @sm.left_navigation_button
        puts "  -about box closed"
        sleep 3
        #test to see if the about navigation menu element is present as this element is not shown when the about dialog is open.
        expect(@sm.getAboutNavMenuText()).to eq("About")
      end

    end #end about button

    context 'Exit button' do
      
      before(:all) do
        @sm.openLeftPanel
      end

      after(:all) do
        @sm.reenterApp()
        @login.loginAsCprs1234()
        @sm = SchedulingManager.new(@driver)
        @sm.openPage(@driver)
      end

      it 'displays an exit button' do
        puts ' -displays an exit button'
        expect(@sm.exit_button_element).to be_visible
      end

      it 'displays an exit popup when clicked with 2 options' do
        puts 'displays an exit popup with Logout and Launchpad options when clicked'
        @sm.exit_button
        ["Logout", "Launchpad"].each do |item|
          expect(contains_link_item(@sm.exit_popup_element.element, item)).to be true
        end
        # expect(contains_link_item(@sm.exit_popup_element.element, "Logout")).to be true
        # expect(contains_link_item(@sm.exit_popup_element.element, "Launchpad")).to be true
      end

      it 'allows user to close exit popup by clicking on [x] button' do
        @sm.closeExitPopup
        expect(@sm.exit_popup_element).to_not be_visible
      end

      context 'allows user to exit the application' do

        before(:each) do
          @sm.reenterApp()
          @sm = SchedulingManager.new(@driver)
          @sm.openPage(@driver)
          @sm.openLeftPanel()
          @sm.exit_button
        end

        it 'allows user to return to launchpad and back to app without being logged out' do
          click_link_item(@sm.exit_popup_element.element, "Launchpad")
          sleep 10
          expect(@sm.current_url).to_not include($acp_app)
        end

        it 'allows user to return to launchpad logged out, login required to reenter' do
          click_link_item(@sm.exit_popup_element.element, "Logout")
          sleep 10
          expect(@sm.current_url).to_not include($acp_app)
          sleep 10
          @sm.reenterApp()
          sleep 10
          expect(@sm.current_url).to include("/AuthorizationServices")
        end
      end

    end #end Exit Button


  end #End Left Panel Navigation

  context "Scheduling Manager footer navigation" do

    before(:each) do
      puts "---Footer Navigation Test---"
    end

    it 'displays the Veteran Requests navigation button: AC #SCV-1761; Test #SCV-1762' do
      puts " -displays the Veteran Requests navigation button test"
      expect(@sm.getNavBtnLabel('Veteran Requests')).to eq("Veteran Requests")
    end

  end #end footer navigation tests

  context "Scheduling Manager App" do

    it 'displays the patient context information: AC #SCV-1770, SCV-1771, SCV-1772; Test #SCV-1775, SCV-1774, SCV-1773' do
      puts " -displays the patient context information test"
      # @sm.updatePatientContextByName('ten, patient')
      sleep 5
      @sm.findPatientTen()
      sleep 5
      #until the patient search is completed, the fact that we are getting the patient name, dob, age, and ssn is more than what was there previously
      #TODO: update this method when the patient search is added to SCV.
      expect(@sm.isPatientNameFormatted()).to eq(true)
      expect(@sm.getPatientDOB()).to_not eq(nil)
      expect(@sm.isDOBFormatted()).to eq(true)
      expect(@sm.getPatientAge()).to_not eq(nil)
      expect(@sm.isPatientAgeValid()).to eq(true)
      expect(@sm.getPatientGender()).to_not eq(nil)
      expect(@sm.isPatientSSNFormatted()).to eq(true) #verifies format
    end

    it 'displays patient detail information: AC #SCV-1801; Test #SCV-1803' do
      puts " -displays patient detail information test"
      
      @sm.patient_contact_btn_element.click
      sleep 7
      expect(@sm.isPatientEmailFormatted()).to eq(true)
      expect(@sm.isPatientHomePhoneFormatted()).to eq(true)
      expect(@sm.isPatientWorkPhoneFormatted()).to eq(true)
      expect(@sm.isPatientAddressFormatted).to eq(true)
      
      @sm.patient_details_done_btn_element.click
    end

    it 'allows a user to clear the patient context: AC #SCV-1800; Test #SCV-1802' do
      puts " -allows a user to clear the patient context test"
      @sm.clear_patient_context_element.click
      expect(@sm.no_patient_selected_message).to eq("No Patient Selected")
    end

  end
end