module OA
  class EditYoutubeChannelPage < OA::BasePage
    page_url("/youtubechannel/editchannel/id/<%=params[:channel_id]%>")

    button(:submit_button, id: "btnSubmitChannel")
    div(:artist_name, id: "artistName")
    div(:channel_verified_alert, id: "channelVerified")
    div(:channel_error_message, id: "channel_error_message")
    div(:channel_success_message, id: "channel_success_message")
    radio(:selected_auto_claim_radio) do
      tr_element(text: /Automatically Claim Videos/).radio_element(checked: "checked").value
    end
    radio_button(:auto_claim_no, value: "no")
    radio_button(:auto_claim_yes, value: "yes")
    select_list(:account_manager_dropdown, id: "accountManager")
    select_list(:channel_country, id: "countryId")
    select_list(:genre_category, id: "youtubeChannelCategoryDropdown")
    select_list(:primary_language, id: "languageCode")
    select_list(:service_tier_dropdown, id: "youtubeChannelServiceTier")
    text_area(:notes, id: "notes")
    text_field(:youtube_channel_address, id: "youtubeChannelName")
    text_field(:youtube_channel_current_upc, id: "youtubeChannelCurrentUpc")
    text_field(:youtube_channel_artist_id, id: "artistId")
    link(:date_initiated_calendar_button) { text_field_element(id: "dateInitiated").element.parent.link }
    link(:date_connected_calendar_button) { text_field_element(id: "dateConnected").element.parent.link }
    link(:current_day_on_calendar) { span_element(class: "currentDay").link_element }

    def select_random_account_manager
      manager_options = account_manager_dropdown_options
      manager_to_select = manager_options.sample
      self.account_manager_dropdown = manager_to_select
    end

    def select_initiated_date
      # page object click method fails with upgrade to 2.1.1
      date_initiated_calendar_button_element.element.click
      browser.windows.last.use
      click_with_javascript(current_day_on_calendar_element)
      browser.windows.last.use
    end

    def select_connected_date
      # page object click method fails with upgrade to 2.1.1
      date_connected_calendar_button_element.element.click
      browser.windows.last.use
      click_with_javascript(current_day_on_calendar_element)
      browser.windows.last.use
    end

    def validate_youtube_channel
      youtube_channel_address_element.element.fire_event("blur")
      wait_for_ajax_scripts_to_finish
      wait_until do
        channel_error_message_element.element.present? || channel_verified_alert_element.element.present?
      end
      if channel_verified_alert_element.element.present?
        "The channel is verified"
      elsif channel_error_message == "YouTube reports that channel does not exist."
        "The channel doesn't exist"
      elsif channel_error_message == "Youtube service is temporarily unavailable. Please try back later."
        "The YouTube API is bugging again"
      end
    end

    def wait_for_artist_validation
      youtube_channel_artist_id_element.element.fire_event("blur")
      artist_name_element.element.wait_until(&:present?)
    end

    def mock_invalid_channel_response
      inject_mockjax_library
      execute_script <<-JS
        $.mockjax({
          url: '/youtubechannel/getyoutubechannelid',
          status: 400,
          contentType: 'text/json',
          responseText: {
            current_topNav: 'relationships',
            err_msg: 'YouTube reports that channel does not exist.'
          }
        });
      JS
    end

    def mock_valid_channel_response
      inject_mockjax_library
      execute_script <<-JS
        $.mockjax({
          url: '/youtubechannel/getyoutubechannelid',
          status: 200,
          contentType: 'text/json',
          responseText: {
            response: {
              youtubeChannelId: 'HolyCrapGianRules'
            }
          }
        });
      JS
    end
  end
end
