module Project
  class ProjectDetailsPage < BasePage
    include ProductSidebar

    page_url "/project/<%=params[:project_id]%>"

    h1(:project_header, class: "ProjectHeader-name")
    button(:save_project, text: "SAVE PROJECT")

    # fields
    text_field(:project_name) { div_element(id: "project-name-input").text_field_element }
    text_field(:project_code) { div_element(id: "project-code-input").text_field_element }
    div(:project_artist, class: "ArtistDropdown-container")
    span(:project_artist_value, class: "Select-multi-value-wrapper")
    div(:subaccount_form, id: "subaccount-select")
    text_field(:subaccount) { subaccount_form_element.text_field_element }
    textarea(:project_description) { div_element(id: "project-description-input").text_area_element }

    button(:delete_project, id: "delete-project")
    article(:delete_confirmation_modal, class: "DeleteConfirmationModal-container")
    button(:confirm_deletion) do
      delete_confirmation_modal_element.button_element(class: "LoadingButton")
    end
    div(:project_success_message, class: ["frc-alert-variant-success"])

    # errors
    span(:error_code_occupied, class: "FormField-errorMessage")
    div(:project_error, id: "project-error")

    # highlights
    textarea(:marketing_highlights, class: "ProjectHighlights-textarea")
    textarea(:marketing_territory_highlights, class: "ProjectHighlights-territory-textarea")
    button(:add_territory_highlight, class: "ProjectHighlights-add-territory")
    button(:add, id: "confirm-add")
    button(:save_highlights, class: "ProjectHighlights-save")
    divs(:territories, class: "AddHighlightProjectionModal-territories-list-item")

    div(:loading_dots, class: "LoadingIndicator-dots")

    # Save or discard changes modal
    button(:save_changes, class: "ConfirmationModal-save-button")

    def project_id_from_url
      url_match = current_url.match(%r{/project/(\d+)})
      fail "Couldn't find project_id from URL!" unless url_match
      url_match[1]
    end

    def project_values
      {
        name: project_name,
        code: project_code,
        artist: project_artist_element.inner_text,
        subaccount: subaccount_value,
        description: project_description_value
      }
    end

    def populate_project_fields_with(data)
      self.project_name = data[:name]
      create_new_project_artist data[:artist]
      # d3 only
      self.subaccount = data[:subaccount] if data[:subaccount]
      # optional
      return unless data[:description]
      project_description.clear
      self.project_description = data[:description]
    end

    # there's a delay between when the page loads and when the fields load
    def wait_for_fields_to_populate
      project_name_element.when_present
      wait_until { project_name != "" }
      wait_until { project_code != "" }
      wait_until { subaccount != "" } if subaccount_form_element.present?
      # wait_until { project_artist_value != "Select Artist" }
    end

    def delete_empty_project
      wait_until { project_success_message.present? }
      retry_delete = proc do |exception, try|
        puts "#{exception.class}: '#{exception.message}' - Retries: #{try}"
        sleep 1
      end
      exceptions = [Selenium::WebDriver::Error::ElementClickInterceptedError,
                    Watir::Exception::UnknownObjectException]
      Retriable.retriable(on: exceptions, tries: 3, on_retry: retry_delete) do
        delete_project
        confirm_deletion
      end
    end

    private

    def project_description_value
      project_description.empty? ? nil : project_description
    end

    # subaccount dropdown only appears for d3s, so return nil if it doesnt exist
    def subaccount_value
      subaccount if subaccount?
    end
  end
end
