# rubocop:disable Metrics/ModuleLength
module Project
  module DigitalProductTrackDetailsForm
    include PageObject
    include PageHelpers::ReactHelpers

    # track details
    div(:track_edit_form, class: "TrackEditForm")
    h4(:track_header, class: "TrackEditForm-header")
    file_field(:file_replace, id: "replace-audio")
    text_field(:track_name, id: "trackName")
    text_field(:track_version, id: "version")
    text_field(:displayed_as, id: "displayed-as")
    react_select(:meta_language, id: "metaLanguageCode")

    # isrc section elements
    text_field(:isrc_field) { div_element(id: "isrc").text_field_element }
    span(:isrc_checkbox, class: "IsrcField-checkbox-label")

    # explicit section radiobuttons
    radio_button_group(:explicit, name: "explicit")

    # Artists & Performers locators
    text_field(:artist_name, id: "trackArtist")
    text_field(:featuring, id: "trackFeaturing")
    text_field(:remixer, id: "trackRemixer")
    text_field(:songwriters, id: "trackSongwriters")

    # Performers
    button(:add_performers, id: "open-participants-dialog")
    div(:primary_performers, id: "itemPrimary")
    div(:studio_personnel_section, id: "TrackParticipants-list-type-subsection-studioPersonnel")
    h6(:studio_personnel_name) do
      studio_personnel_section_element
        .div_element(class: "TrackParticipants-list-participant-item").h6_element
    end
    span(:studio_personnel_role) do
      studio_personnel_section_element
        .div_element(class: "TrackParticipants-list-participant-item").span_element
    end
    div(:label_personnel_section, id: "TrackParticipants-list-type-subsection-labelPersonnel")
    h6(:label_personnel_name) do
      label_personnel_section_element
        .div_element(class: "TrackParticipants-list-participant-item").h6_element
    end
    span(:label_personnel_role) do
      label_personnel_section_element
        .div_element(class: "TrackParticipants-list-participant-item").span_element
    end
    div(:compositional_section, id: "TrackParticipants-list-type-subsection-compositional")
    h6(:compositional_name) do
      compositional_section_element
        .div_element(class: "TrackParticipants-list-participant-item").h6_element
    end
    span(:compositional_role) do
      compositional_section_element
        .div_element(class: "TrackParticipants-list-participant-item").span_element
    end

    # Localization section elements
    divs(:localization, class: "DigitalProductLocalizationsList-item")
    svg(:loc_trash, class: "Icons-trash-can")
    svg(:loc_edit, class: "Icons-edit")
    span(:loc_added, class: "DigitalProductLocalizationsList-language-name")

    # Localization form elements
    div(:loc_form, class: "DigitalProductLocalizationsForm")
    text_field(:loc_track_name) { loc_form_element.text_field_element(id: "track-name-localization") }
    text_field(:loc_track_version) { loc_form_element.text_field_element(id: "track-version-localization") }
    text_field(:loc_track_artists) { loc_form_element.text_field_element(id: "localizedTrackArtist") }
    text_field(:loc_track_featuring) { loc_form_element.text_field_element(id: "localizedTrackFeaturing") }
    text_field(:loc_track_remixer) { loc_form_element.text_field_element(id: "localizedTrackRemixer") }
    button(:loc_add) { loc_form_element.button_element(text: "Add Localization") }
    button(:loc_cancel) { loc_form_element.button_element(text: "Cancel") }

    # [classic] genre specific locators
    text_field(:work, id: "work")
    text_field(:movement, id: "movement")
    # [Additional Info for Classical Releases] section
    text_field(:classic_artist, id: "trackArtist")
    text_field(:composer, id: "trackComposer")
    text_field(:orchestra, id: "trackOrchestra")
    text_field(:ensemble, id: "trackEnsemble")
    text_field(:conductor, id: "trackConductor")

    # Other
    text_field(:p_info, id: "pInfo")
    text_field(:preview_start_time, id: "previewStartTime")
    div(:ownership) { div_element(class: "RightsInfoFields").div_element(class: "Select") }
    spans(:dropdown_clear, class: "Select-clear")
    links(:tag_clear, class: "TagsInput-remove")

    react_select(:country_of_recording) do
      div_element(id: "recording-country").div_element(class: "Select")
    end
    react_select(:producer_nationality) do
      div_element(id: "rightsholder-country").div_element(class: "Select")
    end
    react_select(:localized_language) do
      div_element(class: "LocalizationEditor").div_element(class: "Select")
    end

    # Seek tracks
    span(:previous_track, id: "previous-track")
    button(:next_track, id: "next-track")

    def clear_track_metadata
      dropdown_clear_elements.each { |x| x.click }
      text_field_elements.each { |x| x.set " " if x.present? }
      isrc_checkbox_element.click unless isrc_field_element.disabled?
    end

    def wait_for_localization_to_update(track_edit_data, timeout = 10)
      i = 0
      while i != timeout
        loc_edit_element.click
        return true if loc_track_name_element.value == track_edit_data[:track_name] &&
                       loc_track_version_element.value == track_edit_data[:version]
        loc_cancel_element.click
        sleep(1)
        i += 1
      end
      false
    end

    def populate_dropdown(element, text)
      element.send_keys :backspace
      throttled_input(element, text, 3, false)
      dropdown_element = div_element(class: "AsyncDropdown__option", text: text)
      dropdown_element.when_present.click
    end

    # rubocop:disable Metrics/AbcSize
    def fill_neccessary_track_data(track_data)
      track_name_element.clear_text_and_type track_data[:track_name]
      self.track_version = track_data[:version]
      select_meta_language(track_data[:meta_language])
      isrc_checkbox_element.click
      self.p_info = track_data[:p_info]
      select_explicit track_data[:explicit]
      artist_name_element.send_keys :backspace
      populate_dropdown(artist_name_element, track_data[:artist_name])
      featuring_element.send_keys :backspace
      populate_dropdown(featuring_element, track_data[:featuring])
      self.remixer = track_data[:remixer]
      self.songwriters = track_data[:songwriter]
      select_ownership track_data[:ownership]
      select_country_of_recording(track_data[:country_of_recording])
      select_producer_nationality(track_data[:producer_nationality])
    end

    # Track builder has many fields - causing this function to fail the
    # "A" principle of ABC - too much assignment in one function
    def track_values
      { track_name: track_name, meta_language: meta_language, p_info: p_info, explicit: explicit_selected?,
        artist_name: artist_name_element.parent.span.text, featuring: featuring_element.parent.span.text,
        remixer: remixer_element.parent.span.text, songwriter: songwriters_element.parent.span.text,
        ownership: ownership, country_of_recording: country_of_recording,
        producer_nationality: producer_nationality, version: track_version }
    end
    # rubocop:enable Metrics/AbcSize

    private

    def select_ownership(type)
      ownership_element.click
      wait_until { div_element(class: "Select-menu-outer").present? }
      div_elements(class: "Select-option").find { |owner_type| owner_type.text == type }.element.click
    end
  end
end
