class ContentDownloadContext < CmsContext

  include ContentDataSetup
  include FormTester

  @@content_download_data_create = {
    valid: {
      name: "This is a new Content Download",
      content_score: "2"
    },

    invalid: {
      name: "",
      content_score: "A"
    },

    errors: [
      "Name can't be blank",
      "Content score is not a number"
    ]
  }

  @@content_download_data_update = {
    inactive: {
      valid: {
        active: "No",
        name: "Updated name",
        landing_page_slug: "updated-name",
        content_funnel_stage_id: "Funnel Stage 2"
      },
      invalid: {
        active: "No",
        name: "",
        content_score: "a"
      },
      errors: [
        "Name can't be blank",
        "Content score is not a number"
      ]
    },

    active: {
      valid: {
        active: "Yes",
        title_text: "Title text",
        description: "Description",
        landing_page_slug: "valid-slug",
        landing_page_header: "Landing Page Header",
        landing_page_button_text: "Button Text",
        landing_page_bullet_header: "Bullet Header",
        landing_page_bullet_points: "-1 -2 -3",
        landing_page_event_name: "landing-page-test-event-name",

        UPLOAD_FIELDS: [
          :upload,
          :landing_page_large_image
        ]
      },

      invalid: {
        active: "Yes",
        content_type: "File",
        landing_page_slug: "",
        landing_page_header: "",
        landing_page_button_text: "",
        landing_page_bullet_header: "",
        landing_page_bullet_points: "",
        landing_page_event_name: "",
        title_text: "",
        description: "",
      },

      errors: [
        "Redemption link can't be blank",
        "Landing page slug is invalid",
        "Landing page header can't be blank",
        "Landing page button text can't be blank",
        "Landing page bullet header can't be blank",
        "Landing page bullet points can't be blank",
        "Landing page event name can't be blank",
        "Title text can't be blank",
        "Description can't be blank"
      ]
    }
  }

  # INDEX -----

  def expect_content_downloads_table
    ContentDownload.all.each do |content_download|
      expect_content_on_page(content_download.name)
    end
  end

  # CREATE -----

  def enter_content_download_create_data(validity)
    data = @@content_download_data_create[validity]
    fill_in_form(data, -> (camelcase_name) { ".admin-modal ##{camelcase_name}" })
  end

  def expect_invalid_content_download_create_errors
    errors = @@content_download_data_create[:errors]
    expect_form_errors(errors)
  end

  def expect_latest_edit_page
    expect(current_path).to eq("/admin/content_downloads/4")
    expect_content_on_page("Content Download details")
  end

  # UPDATE -----

  def visit_content_download_edit_page(ordinal)
    id = ContentDownload.send(ordinal).id
    visit("admin/content_downloads/#{id}#{get_params}")
  end

  def enter_content_download_update_data(activity, validity)
    data = @@content_download_data_update[activity][validity]
    fill_in_form(data)
    if validity == :invalid
      find("#upload .remove-upload").click
      find("#landingPageBackgroundImage .remove-upload").click
      find("#landingPageLargeImage .remove-upload").click
    end
  end

  def expect_invalid_content_download_update_errors(activity)
    errors = @@content_download_data_update[activity][:errors]
    expect_form_errors(errors)
  end

  def expect_updated_content_download(activity)
    valid_page_data = @@content_download_data_update[activity][:valid].except(:UPLOAD_FIELDS)
    has_valid_page_data = page.has_content?("Saved")
    id = current_url.split("/")[-2].to_i
    db_entry = ContentDownload.find(id)
    has_valid_db_data = valid_page_data.except(:content_funnel_stage_id, :active).all? { |property, value| db_entry.send(property) == value }
    if activity == :active
      has_valid_db_data &&= db_entry.active == true
    elsif activity == :inactive
      has_valid_db_data &&= db_entry.content_funnel_stage.name == valid_page_data[:content_funnel_stage_id]
      has_valid_db_data &&= db_entry.active == false
    end

    expect(has_valid_page_data && has_valid_db_data).to be(true)
  end

  def expect_content_download_landing_page
    data = @@content_download_data_update[:active][:valid]

    visit("get/#{data[:landing_page_slug]}/#{get_params}")
    expect_content_on_page(data[:landing_page_header])
    expect_content_on_page(data[:landing_page_button_text])
    expect_content_on_page(data[:landing_page_bullet_header])
    expect_content_on_page(data[:landing_page_bullet_points])
  end

  # DELETE -----

  def expect_content_download_index_page
    expect(current_path).to eq("/admin/content_downloads")
  end

  def expect_deleted_content_download(i)
    expected_cd = instance_variable_get("@content_download_#{i}")

    not_in_db = !ContentDownload.all.map(&:id).include?(expected_cd.id)
    not_on_page = expect_content_not_on_page(expected_cd.name)

    expect(not_in_db && not_on_page).to be(true)
  end

  # PREVIEW -----

  def expect_content_download_landing_page_preview
    switch_to_recently_opened_tab
    expect_content_on_page(@content_download_1.landing_page_header)
  end

  def expect_content_download_hero_form_preview
    switch_to_recently_opened_tab
    expect_content_on_page(@content_download_1.hero_form_header)
  end

  def expect_content_download_preview_card_preview
    switch_to_recently_opened_tab
    expect_content_on_page(@content_download_1.name)
  end

end
