module ConflictManager
  class ResponseHistoryPage < BasePage
    page_url "/conflicts/response-history"

    table(:response_history, class: "ActionedConflictsTable")
    div(:sort_by_date_responded) { response_history_element.first["RESPONDED"].div_element }

    def match_bulk_responses(response_index, conflict)
      your_response = response_history_element[response_index].cell_element(
        class: "ActionedConflictRow-response-cell"
      ).text
      expect(your_response).to include conflict["action"] + conflict["conflicts"]
    end

    def match_conflict_responses(response_index, released, asserted)
      your_response = response_history_element[response_index].cell_element(
        class: "ActionedConflictRow-response-cell"
      ).text
      expect(your_response).to include "Release #{released}" unless released.nil? || released.zero?
      expect(your_response).to include "Assert #{asserted}" unless asserted.nil? || asserted.zero?
    end

    def sort_by_date_responded(newest_oldest)
      sort_by_date_responded_element.click
      wait_until { response_history? }
      wait_until do
        sort_by_date_responded_element.div_elements.any? do |div|
          div.attribute_value("class").include? "sorted"
        end
      end
      return unless newest_oldest == "newest_first"
      return if sort_by_date_responded_element.div_element(class: "SortIndicator-down-sorted").present?
      sort_by_date_responded_element.click
      wait_until { response_history? }
    end
  end
end
