module ReactWorkstation
  class ReactPhysicalReportingPage < UniversalPage
    include PageHelpers::ReactHelpers
    include PhysicalReporting::PhysicalReportingFilters
    page_url "/analytics/reporting/physical/report/product-view"

    button(:generate_report_button, text: "GENERATE REPORT")
    div(:frame_window, class: "WhitelabelEmbed")
    div(:results_table, class: "fixedDataTableLayout_main")
    divs(:row, class: "fixedDataTableRowLayout_rowWrapper")
    span(:close_open_select) { div_element(class: "is-open").span_element(class: "Select-arrow") }
    div(:frontend_reporting_app_div, id: "frontend_reporting_app")
    div(:spinner, class: "LoadingIndicator-dots")
    elements(:column_name, class: "SortedDataCell-column-name")
    checkboxes(:column_checkbox, class: "checkbox-input")
    checkboxes(:modal_column_checkbox) do
      div_element(class: "ReactModal-content").checkbox_elements(class: "checkbox-input")
    end
    div(:metadata_selection) do
      div_element(class: "FieldSelectionSection-metadata").div_element(class: "FieldSelectionSection-edit")
    end
    button(:column_selection_done, class: "FieldSelection-done-button")
    div(:product_name_value) { product_element.div_element(class: "Select-custom-value") }
    div(:artist_name_value) { artist_element.div_element(class: "Select-custom-value") }
    # Report presets
    div(:preset_dropdown, id: "report-preset-dropdown")
    button(:save_as_preset, class: "FormFooter-save-preset")
    links(:preset) { div_element(class: "ReportPresetDropdown").link_elements }
    # Save preset modal
    text_field(:present_name, name: "preset-name")
    button(:save_preset, class: "SavePresetModal-save-button")
    # Preset modal
    button(:preset_done, class: "ManagePresetModal-done-button")
    divs(:modal_preset, class: "ManagePresetModal-preset-name")
    elements(:delete_preset, class: "Icons-trash-can")
    div(:delete_confirm, class: "ManagePresetModal-delete-confirm")
    label(:include_coop) { div_element(class: "ReportSelection-coop-data").label_element }

    def select_type_of_report(report_type)
      select_report_type report_type
      wait_for_spinner_to_disappear
    end

    def wait_for_spinner_to_disappear
      spinner_element.wait_while(&:present?)
      results_table_element.wait_until(&:present?)
    end

    def remove_filter(value)
      tag = div_elements(class: "Select-custom-value").find { |x| x.text.include? value }
      tag.span_element(class: "Select-remove").click
    end

    def cell_text(table_row, index)
      dom_element = table_row.div_elements(class: "public_fixedDataTableCell_cellContent")[index]
      dom_element.scroll.to
      dom_element.text
    end

    def column_index(name)
      column_index = column_name_elements.find_index do |header|
        header.parent.attribute_value("data-tip").strip == name.strip
      end
      fail "Could not find index position for column " + name if column_index.nil?
      column_index
    end

    def click_column_checkbox(column_name, in_modal = true)
      elements = if in_modal
                   modal_column_checkbox_elements
                 else
                   column_checkbox_elements
                 end
      checkbox = elements.find do |box|
        box.parent.label_element.text.strip.tr("\"", "") == column_name
      end
      checkbox.parent.label_element.click
    end

    def set_week_date(date, cal)
      selected_date = { month_year: "#{date.split[0]} #{date.split[2]}", day: date.split[1] }
      date_input_container_elements.send(cal).click
      find_month_year(selected_date[:month_year], cal)
      # Click the desired date on the selected month
      month_cal_elements.find_all { |x| x.attribute_value("data-visible") == "true" }.send(cal)
                        .div_element(class: "CalendarDay-day-container", text: selected_date[:day]).click
    end

    private

    def find_month_year(month_year, cal)
      wait_until { calendar_grid? }
      # Given all the calendars, find all the visible ones (should be 2 at a time)
      # Then, select either the start or end calendar and see if it's the desired month/year combo
      # If not, keep clicking either previous to go back a month or next to go to the next one
      until month_cal_elements.find_all { |x| x.attribute_value("data-visible") == "true" }
                              .send(cal).text.include? month_year
        cal == "first" ? prev_month : next_month
        # Kinda lame but it's needed to wait for the current calendar to finish transitioning
        sleep 1
      end
    end
  end
end
