#This class represents the Veteran Requests web page (i.e. formerly the VAAC page)

require "page-object"
require "spec_helper"
require "./spec/spec_helper"
require "./models/appointment_request"

class VeteranRequests
  include PageObject

  #page-object components. The name of the component is the first parameter, where to find the element on the page is the second parameter
  link(:veteran_requests_btn, :id => 'footer-veteran-requests-btn')

  span(:veteran_phone_call_rqst1, :xpath => '//*[@id="inner-content"]/section[2]/div/div/div/ul[5]/li[1]/span[2]')
  span(:veteran_phone_call_rqst2, :xpath => '//*[@id="VAAC-request-content"]/ul[1]/li[1]/span[2]')
  span(:veteran_phone_call_rqst3, :xpath => '//*[@id="book-appt-content"]/div[3]/p/span[2]')

  table(:appointment_requests_table, :id => 'appointment-requests-table')

  button(:first_process_button, :xpath => '//*[@id="process-request"]')
  text_field(:search_service_specialty_input_field, :xpath => '//*[@id="calendar-specialty-input"]')
  link(:gen_medicine_specialty, :xpath => '//*[@id="calendar-specialty"]/li[4]/div/div/a')
  span(:clinics_span, :xpath => '//*[@id="calendar-clinic-button"]/span[1]/span[1]')
  span(:gen_med_clinic_span, :xpath => '//*[@id="calendar-clinic-menu"]/div/div[1]/label/span/span[1]')
  checkbox(:gen_med_clinic_chkbx, :xpath => '//*[@id="clinic-0"]')
  span(:view_clinic_availability_span, :xpath => '//*[@id="appointment-button"]/a/span/span')

	list_item(:first_view_option_date_2, :xpath => '//*[contains(@class, "option-date2")]')
  list_item(:first_view_option_date_3, :xpath => '//*[contains(@class, "option-date3")]')
  list_item(:second_view_option_date_2, :xpath => '//*[@id="VAAC-request-content"]/ul[2]/li/span[2]/ul/li[2]')
  list_item(:second_view_option_date_3, :xpath => '//*[@id="VAAC-request-content"]/ul[2]/li/span[2]/ul/li[3]')

  div(:one_pm_appointment_time_slot, :xpath => '//*[@id="calendar"]/div/div[2]/div/div/div/div/button[21]/div[1]')

  span(:booked_from_alternate_span, :xpath => '//*[@id="request-date-time"]/div[2]/div[3]/label/span/span[1]')
  span(:booked_from_alternate_after_contact_span, :xpath => '//*[@id="request-date-time"]/div[2]/div[2]/label/span/span[1]')

  span(:book_appt_select_purpose, :xpath => '//*[@id="appt-purpose-button"]/span/span[1]')
  link(:book_appt_regular_purpose, :xpath => '//*[@id="appt-purpose-menu"]/li[9]/div/div/a')
  span(:book_appt_select_appt_type, :xpath => '//*[@id="appt-type-button"]/span/span[1]')
  link(:book_appt_other_pat_req_appt_type, :xpath => '//*[@id="appt-type-menu"]/li[4]/div/div/a')

  button(:booking_form_submit_btn, :xpath => '//*[@id="submit-btn"]')
  button(:booking_form_cancel_btn, :xpath => '//*[@id="cancel-btn"]')

  button(:close_book_appt_confirm_screen_btn, :id => 'close-book-appt')
  button(:close_book_appt_confirm_screen_return_vaac_btn, :id => 'go-to-VAAC-btn')

  span(:request_overview_section_status, :xpath => '//*[@id="inner-content"]/section[2]/div/div/div/ul[1]/li/span[2]')
  
  link(:footer_veteran_requests_btn, :id => 'footer-veteran-requests-btn')

  #methods
  def openPage(driver)
    @driver = driver
  end

  def openPatientAppointmentRequest(patient_name, last_activity, status, appt_type)
    puts " -openPatientAppointmentRequest(#{patient_name}; #{last_activity}; #{status}; #{appt_type})"

    last_activity_refmtd = DateTime.parse(last_activity).strftime('%m/%d/%Y %H:%M:%S')

    puts "  -number of appointment requests: #{@driver.find_elements(:xpath, '//*[@id="appointment-requests-table"]/tbody/tr').size}"
    for i in 1..@driver.find_elements(:xpath, '//*[@id="appointment-requests-table"]/tbody/tr').size
      table_row_xpath_string = '//*[@id="appointment-requests-table"]/tbody/tr[' + i.to_s + ']'
      
      pat_ar = @driver.find_element(:xpath, table_row_xpath_string + '/td[2]/a').text
      
      if ((patient_name == pat_ar))
      
        if  (@driver.find_element(:xpath, table_row_xpath_string + '/td[6]').text.strip == last_activity_refmtd)

          if (@driver.find_element(:xpath, table_row_xpath_string + '/td[4]').text.strip == appt_type) 

            if  (@driver.find_element(:xpath, table_row_xpath_string + '/td[5]').text.strip == status) 
            
              puts "  -found patient #{patient_name} and last activity #{last_activity}"
              @driver.find_element(:xpath, table_row_xpath_string + '/td[2]/a').click
              break
            end
          end
        end
      end
    end
  end

  def isFirstPhoneRequestedIndicatorDisplayed()
    puts " -isFirstPhoneRequestedIndicatorDisplayed()"
    puts "  -first phone call requested indicator: #{veteran_phone_call_rqst1}"
    puts "  -first phone call requested indicator is visible: #{veteran_phone_call_rqst1_element.visible?}"
    return veteran_phone_call_rqst1_element.visible?
  end

  def isSecondPhoneRequestedIndicatorDisplayed()
    puts " -isSecondPhoneRequestedIndicatorDisplayed()"
    puts "  -second phone call requested indicator: #{veteran_phone_call_rqst2}"
    puts "  -second phone call requested indicator is visible: #{veteran_phone_call_rqst2_element.visible?}"
    return veteran_phone_call_rqst2_element.visible?
  end

  # def openGenMedSpecialtyClinicCalendar(specialty_name, clinic_name)
  def openGenMedSpecialtyClinicCalendar()
    # puts " -openGenMedSpecialtyClinicCalendar(#{specialty_name}, #{clinic_name})"
    puts " -openGenMedSpecialtyClinicCalendar()"
    self.search_service_specialty_input_field_element.clear
    self.search_service_specialty_input_field = 'GENERAL MEDICINE'
    self.search_service_specialty_input_field_element.send_keys :return
    sleep 3

    #TODO: to make this more general, add logic to find the right specialty in the UI
    gen_medicine_specialty_element.click
    sleep 3
    #click on the clinic dropdown
    clinics_span_element.click
    sleep 3

    #from the popup window, pick the clinic TODO: to make this more general, add logic to pick the clinic passed in
    gen_med_clinic_span_element.click
    sleep 3
    check_gen_med_clinic_chkbx
    view_clinic_availability_span_element.click
    sleep 11
  end

  def selectAppointmentRequestOption1Date(option_date, option_time)
    puts "selectAppointmentRequestOption1Date(#{option_date}, #{option_time})"

    puts "date slice: #{option_date.slice(0..9)}"
    date_xpath = '//*[@id="calendar"]/div/div/table/tbody/tr/td[@data-date="' + option_date.slice(0..9) + '"]'
    puts "date_xpath: #{date_xpath}"
    # desired_date = @driver.find_element(:xpath, '//*[@id="calendar"]/div/div/table/tbody/tr/td[@data-date="2014-05-18"]')
    desired_date = @driver.find_element(:xpath, date_xpath)
    puts "desired date: #{desired_date.text}"
    desired_date.click

    #loop through the times displayed and pick the first available time given the AM/PM preference
    # @driver.find_element(:xpath, "//div[@id='calendar']/div/div[2]/div/div/div/div/button[5]").click
    for i in 1..@driver.find_elements(:xpath, "//div[@id='calendar']/div/div[2]/div/div/div/div/button").size
      time_slot = @driver.find_element(:xpath, "//div[@id='calendar']/div/div[2]/div/div/div/div/button[#{i}]").text
      puts "time slot[#{i}]: #{time_slot}"
      # if (time_slot.to_i < 12) && (option_time == 'AM') #TODO: add this logic in later
      puts "number of appts available: #{@driver.find_element(:xpath, "//div[@id='calendar']/div/div[2]/div/div/div/div/button[#{i}]").attribute('data-status')}"
        if (@driver.find_element(:xpath, "//div[@id='calendar']/div/div[2]/div/div/div/div/button[#{i}]").attribute('data-status').to_i > 0)
          @driver.find_element(:xpath, "//div[@id='calendar']/div/div[2]/div/div/div/div/button[#{i}]").click
          break
        end
      # end
    end

  end

  def openBookAppointmentDialogForGenMed(option_date, option_time)
    self.openGenMedSpecialtyClinicCalendar()
    self.selectAppointmentRequestOption1Date(option_date, option_time)
  end

  def populateBookingForm()
    # @driver.find_element(:css, "#appt-purpose-button > span.ui-btn-inner").click
    book_appt_select_purpose_element.click
    # @driver.find_element(:link, "REGULAR").click
    book_appt_regular_purpose_element.click
    # @driver.find_element(:css, "#appt-type-button > span.ui-btn-inner > span.ui-btn-text").click
    book_appt_select_appt_type_element.click
    # @driver.find_element(:link, "Other (Patient Req.)").click
    book_appt_other_pat_req_appt_type
  end


  def submitBookingForm()
    self.booking_form_submit_btn_element.click
  end

  def cancelBookingForm()
    self.booking_form_cancel_btn.click
  end

  def closeBookingConfirmationAndSave()
    puts " -closeBookingConfirmationAndSave()"
    close_book_appt_confirm_screen_return_vaac_btn_element.click
    sleep 7
  end

  def getOverviewSectionStatus()
    sleep 3
    puts "  -request_overview_section_status element: #{request_overview_section_status}"
    return request_overview_section_status_element.text
  end
end
