# rubocop:disable Metrics/ModuleLength
module Video
  module VideoMetadataForm
    include PageHelpers::ReactHelpers
    include PageObject

    # video metadata fields
    react_select(:type_of_video) { div_element(id: "metarow-type-of-video").div_element(class: "Select") }
    text_field(:video_title) { div_element(id: "metarow-video-title").text_field_element }
    react_select(:language_of_video_title) do
      div_element(id: "metarow-language-of-video-title").div_element(class: "Select")
    end
    span(:primary_artist, id: "react-select-12--value-item")
    div(:artist_row, id: "metarow-primary-artist")
    button(:new_artist) { artist_row_element.button_element }
    text_field(:artist_name) { artist_row_element.text_field_element(class: "Basics-imprint-name-label") }
    button(:add_artist) { artist_row_element.button_element(class: "Basics-imprint-add") }
    text_field(:version) { div_element(id: "metarow-version").text_field_element }
    text_area(:description) { div_element(id: "metarow-description").text_area_element }
    div(:imprint_row, id: "metarow-imprint")
    react_select(:imprint) { imprint_row_element.div_element(class: "Select") }
    button(:new_imprint) { imprint_row_element.button_element }
    text_field(:imprint_name) { imprint_row_element.text_field_element(class: "Basics-imprint-name-label") }
    button(:add_imprint) { div_element(id: "metarow-imprint").button_element(class: "Basics-imprint-add") }
    div(:isrc_toggle) { div_element(id: "metarow-isrc").div_element(class: "ToggleInput-toggle-bg") }
    text_field(:isrc) { div_element(id: "metarow-isrc").text_field_element }
    div(:upc_toggle) { div_element(id: "metarow-upc").div_element(class: "ToggleInput-toggle-bg") }
    text_field(:upc) { div_element(id: "metarow-upc").text_field_element }
    text_field(:product_code) { div_element(id: "metarow-product-code").text_field_element }
    react_select(:language_of_video_content) do
      div_element(id: "metarow-language-of-video-content").div_element(class: "Select")
    end
    react_select(:genre) { div_element(id: "metarow-genre").div_element(class: "Select") }
    react_select(:subgenre) { div_element(id: "metarow-subgenre").div_element(class: "Select") }
    radio_button_group(:parental_advisory, name: "parentalAdvisory")
    text_field(:keywords_tags) { div_element(id: "metarow-keywords").text_field_element }
    text_area(:lyrics) { div_element(id: "metarow-lyrics").text_area_element }
    text_field(:c_line_year) { div_element(id: "metarow-c-line").text_field_elements[0] }
    text_field(:c_line_info) { div_element(id: "metarow-c-line").text_field_elements[1] }
    text_field(:p_line_year) { div_element(id: "metarow-p-line").text_field_elements[0] }
    text_field(:p_line_info) { div_element(id: "metarow-p-line").text_field_elements[1] }
    div(:contributor_row, id: "metarow-contributors")
    react_select(:contributor_role) { contributor_row_element.div_element(class: "Select") }
    text_field(:contributor_name, class: "Contributors-name")
    react_select(:contributor_role_2) { contributor_row_element.div_elements(class: "Select")[-1] }
    text_field(:contributor_name_2) { text_field_elements(class: "Contributors-name")[-1] }
    button(:add_contributor) { contributor_row_element.button_element(text: /Add/) }
    text_field(:release_date, class: "DayPickerInput-input")
    div(:new_release_toggle) do
      div_element(id: "metarow-new-release").div_element(class: "ToggleInput-toggle-bg")
    end
    text_area(:special_instructions) { div_element(id: "metarow-special-instructions").text_area_element }
    div(:deliver_to_all_toggle) do
      div_element(id: "metarow-deliver-to-all").div_element(class: "ToggleInput-toggle-bg")
    end
    react_select(:youtube_channel) do
      div_element(id: "metarow-channel-selection").div_element(class: "Select")
    end

    def populate_video_basics(data)
      retry_select_and_assert("type_of_video", data[:type_of_video])
      self.video_title = data[:release_name]
      retry_select_and_assert("language_of_video_title", data[:language_of_video_title])
      new_artist
      artist_name_element.when_present.set data[:primary_artist]
      add_artist
      self.version = data[:version]
      self.description = data[:description]
      new_imprint
      imprint_name_element.when_present.set data[:imprint]
      add_imprint
      self.product_code = data[:product_code]
    end

    def populate_video_content(data)
      retry_select_and_assert("genre", data[:genre])
      retry_select_and_assert("language_of_video_content", data[:language_of_video_title])
      wait_until { !subgenre_element.attribute_value("class").include? "is-disabled" }
      retry_select_and_assert("subgenre", data[:subgenre])
      select_parental_advisory data[:parental_advisory]
      self.keywords_tags = data[:keywords_tags]
      keywords_tags_element.send_keys :tab
      self.lyrics = data[:lyrics]
    end

    def populate_video_rights_and_distribution(data)
      self.c_line_year = data[:c_line_year]
      self.c_line_info = data[:c_line_info]
      self.p_line_year = data[:p_line_year]
      self.p_line_info = data[:p_line_info]
      retry_select_and_assert("contributor_role", data[:contributor_role])
      self.contributor_name = data[:contributor_name]
      add_second_contributor(data)
      scroll_to :bottom
      apply_release_date(data[:release_date].to_s)
      self.special_instructions = data[:special_instructions]
      special_instructions_element.send_keys :tab
      retry_select_and_assert("youtube_channel", data[:youtube_channel])
    end

    def video_page_values
      {
        type_of_video: type_of_video,
        release_name: video_title,
        language_of_video_title: language_of_video_title,
        primary_artist: primary_artist,
        version: version,
        description: description,
        imprint: imprint,
        product_code: product_code.to_s
      }.merge(video_content_values).merge(video_rights_and_distribution_values)
    end

    def apply_release_date(date)
      year, month, day = parse_date(date.to_s)
      release_date_element.fire_event :click
      div_element(class: "DayPickerInput-Overlay").wait_until(&:present?)
      select_list_element(class: "DayPicker-Year-Picker").select! year
      select_list_element(class: "DayPicker-Year-Picker").fire_event "change"
      select_list_element(class: "DayPicker-Month-Picker").select! month
      select_list_element(class: "DayPicker-Month-Picker").fire_event "change"
      div_element(class: "DayPicker-Day", "aria-disabled": "false", text: day).fire_event :click
    end

    def retry_select_and_assert(react_list, select_data)
      Retriable.retriable(on: RSpec::Expectations::ExpectationNotMetError, tries: 3) do
        send("select_#{react_list}", select_data + "\n")
        expect(send(react_list)).to eq select_data
      end
    end

    private

    def video_content_values
      {
        language_of_video_content: language_of_video_content,
        genre: genre,
        subgenre: subgenre,
        parental_advisory: parental_advisory_selected?,
        keywords_tags: keywords_tags_element.parent.text,
        lyrics: lyrics
      }
    end

    def video_rights_and_distribution_values
      {
        c_line_year: c_line_year.to_i,
        c_line_info: c_line_info,
        p_line_year: p_line_year.to_i,
        p_line_info: p_line_info,
        contributor_role: contributor_role,
        contributor_name: contributor_name,
        contributor_role_2: contributor_role_2,
        contributor_name_2: contributor_name_2,
        release_date: Date.parse(release_date),
        special_instructions: special_instructions,
        youtube_channel: youtube_channel
      }
    end

    def parse_date(date)
      year = Date.strptime(date).strftime("%Y")
      month = Date.strptime(date).strftime("%B")
      day = Date.strptime(date).strftime("%-d")

      [year, month, day]
    end

    def add_second_contributor(data)
      add_contributor
      wait_until { text_field_elements(class: "Contributors-name").size == 2 }
      retry_select_and_assert("contributor_role_2", data[:contributor_role_2])
      self.contributor_name_2 = data[:contributor_name_2]
    end
  end
end
# rubocop:enable Metrics/ModuleLength
