require "rails_helper"

feature "User managing acquired_media releases" do
  let(:current_user) { create(:user, :manager) }
  let(:project) { create(:project, members: current_user, account: current_user.primary_account) }

  context "when signed out" do
    scenario "United States is default country" do
      contract_template = create(:contract_template, project: project)

      visit new_account_project_contract_template_acquired_media_release_path(project.account, project, contract_template)
      expect(country_field_value).to eq "US"
    end

    scenario "creating a release", js: true do
      contract_template = create(:contract_template, project: project)

      visit new_account_project_contract_template_acquired_media_release_path(project.account, project, contract_template)

      by "filling out the form" do
        fill_in acquired_media_name_field, with: "Jane Doe"
        acquired_media_category_fields

        draw_signature file_fixture("signature.png"), "acquired_media_release_signature_base64"
      end

      click_button "I have read and agree to the above"

      expect(AcquiredMediaRelease.last.categories).to include("Video Footage")
      expect(AcquiredMediaRelease.last.categories).to include("Still Photograph")
      expect(page).to have_content("Your release was successfully submitted. Thank you.")
    end
  end

  context "when signed in" do
    scenario "creating, updating, destroying a release", js: true do
      release_data = {
        name: "Test Acquired Media Release",
        applicable_media: ApplicableMedium.last.label,
        territory: Territory.last.label,
        term: Term.last.label,
        restriction: Restriction.first.label,
        restriction_text: "Not available in China",
      }

      sign_in current_user
      visit new_project_acquired_media_release_path(project)

      by "attaching only a contract" do
        attach_file "acquired_media_release[contract]", Rails.root.join(file_fixture("contract.pdf")), visible: false
        click_button create_release_button

        expect(page).to have_invalid_field(acquired_media_name_field)
      end

      by "attaching files" do
        drop_file Rails.root.join(file_fixture("video_file.mp4")), type: "file-info-dropzone"
        click_button create_release_button

        expect(page).to have_invalid_field(acquired_media_name_field)
      end

      by "filling out the remaining information" do
        fill_in_release_fields release_data
        click_button create_release_button

        expect(page).to have_content(create_release_notice)
        expect(page).to have_content("1")

        click_on "Manage"
        expect(page).to have_link("Download")
      end

      it_also "updates an existing release" do
        click_link "Edit"

        within ".dropzone" do
          expect(page).to have_filename("video_file.mp4")
        end

        expect(page).to have_filled_in_data(release_data)

        fill_in_release_fields name: "New name"
        drop_file Rails.root.join(file_fixture("person_photo.png")), type: "file-info-dropzone"
        click_button update_release_button

        expect(page).to have_content(update_release_notice)
        expect(page).to have_content("New name")
        expect(page).to have_content("2")
      end

      it_also "deletes an existing release" do
        click_button "Manage"
        accept_alert do
          click_link "Delete"
        end

        expect(page).not_to have_content("New name")
      end
    end

      scenario "viewing the contract PDF" do
        acquired_media_release = create(:acquired_media_release_with_contract_template, 
                                  :native,
                                  project: project,
                                  person_name: "Jane Doe",
                                  tag_list: "Woman, Brunette",
                                  notes: [
                                    build(:note, 
                                          content: "Note 1", 
                                          user: build(:user, email: "jane.doe@test.com"),
                                          email: "jane.doe@test.com",
                                          created_at: DateTime.new(2020, 2, 21, 12, 0, 0),
                                         ), 
                                    build(:note, 
                                          content: "Note 2", 
                                          user: build(:user, email: "john.doe@test.com"),
                                          email: "john.doe@test.com",
                                          created_at: DateTime.new(2020, 2, 20, 11, 0, 0),
                                         ),
                                  ], 
                                  file_infos: 
                                  [
                                    build(:file_info, 
                                          filename: "aaa.jpg", 
                                          content_type: "image/jpeg"),
                                    build(:file_info, 
                                          filename: "bbb.mp4", 
                                          content_type: "video/mp4"),
                                    build(:file_info, 
                                          filename: "unknown.doc", 
                                          content_type: "unknown/file")
                                  ]

                                 )

      sign_in(current_user)
      visit project_acquired_media_releases_path(project)
      click_link *view_release_pdf_link_for(acquired_media_release)

      expect(content_type).to eq("application/pdf")
      expect(pdf_body).to have_content("NOTES")
      expect(pdf_body).to have_content("Note 1")
      expect(pdf_body).to have_content("jane.doe@test.com")
      expect(pdf_body).to have_content("2/21/20 12:00 PM")
      expect(pdf_body).to have_content("Note 2")
      expect(pdf_body).to have_content("john.doe@test.com")
      expect(pdf_body).to have_content("2/20/20 11:00 AM")
      expect(pdf_body).to have_content("TAGS")
      expect(pdf_body).to have_content("Woman")
      expect(pdf_body).to have_content("Brunette")
      expect(pdf_body).to have_content("FILES")
      expect(pdf_body).to have_content("Photos")
      expect(pdf_body).to have_content("Videos")
      expect(pdf_body).to have_content("aaa.jpg")
      expect(pdf_body).to have_content("bbb.mp4")
      expect(pdf_body).to have_content("unknown.doc")
      expect(pdf_body).to have_content("Other files")
    end

    scenario "searching for a release", js: true do
      collection1 = create(:acquired_media_release, name: "EDM Music", project: project)
      collection2 = create(:acquired_media_release, name: "Classical Music", project: project)

      sign_in current_user
      visit project_acquired_media_releases_path(project)

      within "form#search" do
        fill_in "Search", with: "EDM"
        click_on "button"
      end

      expect(page).to have_content("EDM Music")
      expect(page).not_to have_content("Classical Music")
      expect(page).to have_field("Search", with: "EDM")
    end

    scenario "edit is visible for non-native release" do
      create(:acquired_media_release_with_contract_template, name: "EDM Music", project: project)

      sign_in current_user
      visit project_acquired_media_releases_path(project)

      click_on "Manage"
      expect(page).to have_link("Edit", exact: true)
    end

    scenario "edit is not visible for native release" do
      create(:acquired_media_release_with_contract_template, :native, name: "EDM Music", project: project)

      sign_in current_user
      visit project_acquired_media_releases_path(project)

      click_on "Manage"
      expect(page).not_to have_link("Edit", exact: true)
    end

    context "when the user is associate" do
      let(:current_user) { create(:user, :associate) }

      scenario "should not show download" do
        collection1 = create(:acquired_media_release_with_contract_template, name: "EDM Music", project: project)

        sign_in current_user
        visit project_acquired_media_releases_path(project)

        click_on "Manage"
        expect(page).not_to have_link("Download", exact: true)
      end
    end
  end

  private

  def country_field_value
    find_field("acquired_media_release[person_address_country]").value
  end

  def acquired_media_name_field
    "acquired_media_release[name]"
  end

  def acquired_media_category_fields
    find(:css, "#acquired_media_release_categories_video_footage").set(true)
    find(:css, "#acquired_media_release_categories_still_photograph").set(true)
  end

  def have_filename(filename)
    have_selector("span[data-dz-name]", text: filename)
  end

  def import_acquired_media_release_link(project)
    ["Import Release", href: new_project_acquired_media_release_path(project)]
  end

  def update_acquired_media_release_link(acquired_media_release)
    ["Edit", href: edit_acquired_media_release_path(acquired_media_release)]
  end

  def destroy_acquired_media_release_link(acquired_media_release)
    ["Delete", href: acquired_media_release_path(acquired_media_release)]
  end

  def fill_in_release_fields(data)
    fill_in "acquired_media_release[name]", with: data[:name]

    select_exploitable_right "applicable_medium", data[:applicable_medium]
    select_exploitable_right "territory", data[:territory]
    select_exploitable_right "term", data[:term]
    select_exploitable_right "restriction", data[:restriction]

    fill_in_exploitable_right_field("applicable_medium", data[:applicable_medium_text])
    fill_in_exploitable_right_field("territory", data[:territory_text])
    fill_in_exploitable_right_field("term", data[:term_text])
    fill_in_exploitable_right_field("restriction", data[:restriction_text])
  end

  def select_exploitable_right(name, value)
    if value.present?
      select value, from: "acquired_media_release[#{name}_id]"
    end
  end

  def fill_in_exploitable_right_field(name, text)
    if text.present?
      fill_in "acquired_media_release[#{name}_text]", with: text
    end
  end

  def view_release_pdf_link_for(acquired_media_release)
    ["Download", href: acquired_media_release_contracts_path(acquired_media_release, format: "pdf")]
  end

  def have_filled_in_data(data)
    have_field "acquired_media_release[name]", with: data[:name]
    have_field "acquired_media_release[acquired_medium_id]", with: data[:acquired_medium]
    have_field "acquired_media_release[acquired_medium_text]", with: data[:acquired_medium_text]
    have_field "acquired_media_release[territory_id]", with: data[:territory]
    have_field "acquired_media_release[territory_text]", with: data[:territory_text]
    have_field "acquired_media_release[term_id]", with: data[:term]
    have_field "acquired_media_release[term_text]", with: data[:term_text]
    have_field "acquired_media_release[restriction_id]", with: data[:restriction]
    have_field "acquired_media_release[restriction_text]", with: data[:restriction_text]
  end

  def create_release_button
    t "helpers.submit.acquired_media_release.create"
  end

  def create_release_notice
    t "acquired_media_releases.create.notice"
  end

  def update_release_button
    t "helpers.submit.acquired_media_release.update"
  end

  def update_release_notice
    t "acquired_media_releases.update.notice"
  end

  def destroy_release_alert
    t "acquired_media_releases.destroy.alert"
  end
end
