require "rails_helper"

feature "User managing material 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_material_release_path(project.account, project, contract_template)
      expect(country_field_value).to eq "US"
    end

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

      visit new_account_project_contract_template_material_release_path(project.account, project, contract_template)

      by "filling out the form" do
        fill_in material_name_field, with: "Pepsi Logo"
        fill_in person_first_name_field, with: "Jane"
        fill_in person_last_name_field, with: "Doe"
        draw_signature file_fixture("signature.png"), "material_release_signature_base64"
      end

      click_button submit_release_button

      expect(page).to have_content("Your release was successfully submitted. Thank you.")
    end

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

      visit new_account_project_contract_template_material_release_path(project.account, project, contract_template)

      fill_in material_name_field, with: "Pepsi Logo"
      fill_in person_first_name_field, with: "Jane"
      fill_in person_last_name_field, with: "Doe"
      draw_signature file_fixture("signature.png"), "material_release_signature_base64"

      drop_file Rails.root.join(file_fixture("material_photo.png")), type: :dropzone
      click_button submit_release_button

      expect(page).to have_content("Your release was successfully submitted. Thank you.")
      expect(MaterialRelease.last.photos.attached?).to eq true
    end
  end

  context "when signed in" do
    before do
      sign_in current_user
    end

    scenario "creating a release", js: true do
      visit new_project_material_release_path(project)

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

        expect(page).to have_invalid_field(material_name_field)
      end

      by "attaching photos" do
        drop_file Rails.root.join(file_fixture("material_photo.png")), type: :dropzone
        click_button create_release_button

        expect(page).to have_invalid_field(material_name_field)
      end

      by "filling out the remaining information" do
        fill_in_release_fields name: "Apple Laptop"
        click_button create_release_button

        expect(page).to have_content(create_release_notice)
        expect(page).to have_photo("material_photo.png")

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

    scenario "updating an existing release", js: true do
      material_release = create(:material_release, :non_native, project: project)

      visit project_material_releases_path(project)
      click_on "Manage"
      click_link *update_material_release_link(material_release)

      fill_in_release_fields name: "New release name"
      select "Other", from: "Applicable Media"
      fill_in "Describe other applicable media", with: "New Test"
      click_button update_release_button

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

    scenario "deleting an existing release", js: true do
      material_release = create(:material_release, project: project)

      visit project_material_releases_path(project)
      click_on "Manage"

      accept_alert do
        click_link *destroy_material_release_link(material_release)
      end

      expect(page).to have_content(destroy_release_alert)
      expect(page).not_to have_content(material_release.name)
    end

    scenario "searching for a release", js: true do
      create(:material_release, name: "Apple MacBook Air", project: project)
      create(:material_release, name: "Microsoft Surface Pro", project: project)

      visit project_material_releases_path(project)

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

      expect(page).to have_content("Apple MacBook Air")
      expect(page).not_to have_content("Microsoft Surface Pro")
      expect(page).to have_field("Search", with: "Apple")
    end

    scenario "adding photos to an existing release", js: true do
      create(:material_release, name: "Apple MacBook Air", project: project)

      visit project_material_releases_path(project)

      expect(page).to have_content("Needs Photo")

      click_on "Manage"
      click_on "Photos"

      expect(page).to have_content("Add Photos")
      expect(page).to have_content("Apple MacBook Air")

      drop_file Rails.root.join(file_fixture("material_photo.png")), type: :dropzone
      click_on "Save Changes"

      expect(page).to have_content("The release has been updated")
      expect(page).to have_photo("material_photo.png")
    end

    scenario "viewing the contract PDF" do
      material_release = create(:material_release_with_contract_template_and_photo,
                                :native,
                                project: project,
                                name: "Test Materials",
                                tag_list: "Soda Can",
                                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),),
                                ])

      sign_in(current_user)
      visit project_material_releases_path(project)
      click_link *view_release_pdf_link_for(material_release)

      expect(content_type).to eq("application/pdf")
      expect(content_disposition).to include("inline")
      expect(pdf_filename).to include("test-materials")
      expect(pdf_body).to have_content("Test Materials")
      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("Soda Can")
      expect(pdf_body).to have_content photos_heading.upcase
      expect(pdf_body).to have_content("material_photo.png")
    end
  end

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

    scenario "should not show download" do
      create(:material_release_with_contract_template, name: "Apple MacBook Air", project: project)

      sign_in current_user
      visit project_material_releases_path(project)

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

  private

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

  def photos_heading(photos_count = 1)
    t 'contracts.photos.heading', count: photos_count
  end

  def material_name_field
    "material_release[name]"
  end

  def person_first_name_field
    "material_release[person_first_name]"
  end

  def person_last_name_field
    "material_release[person_last_name]"
  end

  def have_photo(filename)
    have_selector("img[src*='#{filename}']")
  end

  def import_material_release_link(project)
    ["Import Release", href: new_project_material_release_path(project)]
  end

  def update_material_release_link(material_release)
    ["Edit", href: edit_material_release_path(material_release)]
  end

  def destroy_material_release_link(material_release)
    ["Delete", href: material_release_path(material_release)]
  end

  def view_release_pdf_link_for(material_release)
    ["Download", href: material_release_contracts_path(material_release, format: "pdf")]
  end

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

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

  def submit_release_button
    t 'shared.submit_release_long'
  end

  def create_release_notice
    t "material_releases.create.notice"
  end

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

  def update_release_notice
    t "material_releases.update.notice"
  end

  def destroy_release_alert
    t "material_releases.destroy.alert"
  end

  def fill_in_exploitable_rights
    select "Other", from: "Applicable Media"
    fill_in "Describe other applicable media", with: "Test"
    select "Other", from: "Territory"
    fill_in "Describe other territory", with: "Test"
    select "Other", from: "Term"
    fill_in "Describe other term", with: "Test"
    select "Other", from: "Restriction"
    fill_in "Describe other restrictions", with: "Test"
  end
end
