require "rails_helper"

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

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

      visit new_account_project_contract_template_talent_release_path(project.account, project, contract_template)

      by "filling out the form" do
        fill_in person_first_name_field, with: "Jane"
        fill_in person_last_name_field, with: "Doe"
        fill_in person_address_field, with: "123 Test Lane, New York, NY 10000"
        fill_in person_phone_field, with: "555-555-5555"
        fill_in person_email_field, with: "jane.doe@test.com"
        drop_file Rails.root.join(file_fixture("person_photo.png")), type: :dropzone
        draw_signature file_fixture("signature.png"), "talent_release_signature_base64"
      end

      click_button "I have read and agree to the above"

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

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

      visit new_account_project_contract_template_talent_release_path(project.account, project, contract_template)

      expect(page).not_to have_content("GUARDIAN INFORMATION")
      expect(page).not_to have_content("GUARDIAN PHOTO")

      page.check person_is_minor_checkbox
      expect(page).to have_content("GUARDIAN INFORMATION")
      expect(page).to have_content("GUARDIAN PHOTO")

      by "filling out the form" do
        fill_in guardian_first_name_field, with: "Guardian"
        fill_in guardian_last_name_field, with: "Name"
        fill_in guardian_phone_field, with: "001101"
        fill_in person_first_name_field, with: "Jane"
        fill_in person_last_name_field, with: "Doe"
        fill_in person_address_field, with: "123 Test Lane, New York, NY 10000"
        fill_in person_phone_field, with: "555-555-5555"
        fill_in person_email_field, with: "jane.doe@test.com"
        drop_file Rails.root.join(file_fixture("person_photo.png")), type: :dropzone
        attach_file guardian_photo_field, file_fixture("hemsworth.jpeg"), visible: :all
        draw_signature file_fixture("signature.png"), "talent_release_signature_base64"
      end

      click_button "I have read and agree to the above"

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

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

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

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

        expect(page).to have_invalid_field("Person first name")
      end

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

        expect(page).to have_invalid_field("Person first name")
      end

      by "filling out the remaining information" do
        fill_in person_first_name_field, with: "John"
        fill_in person_last_name_field, with: "Doe"
        
        fill_in_exploitable_rights
        click_button create_release_button

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

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

    scenario "creating a release for minor", js: true do
      visit new_project_talent_release_path(project)

      expect(page).not_to have_content("Guardian Photo")
      page.check person_is_minor_checkbox
      expect(page).to have_content("Guardian Photo")
      fill_in person_first_name_field, with: "John"
      fill_in person_last_name_field, with: "Doe"
      fill_in guardian_first_name_field, with: "Guardian"
      fill_in guardian_last_name_field, with: "Name"
      fill_in guardian_phone_field, with: "01010"

      fill_in_exploitable_rights

      attach_file "talent_release[contract]", Rails.root.join(file_fixture("contract.pdf")), visible: false
      drop_file Rails.root.join(file_fixture("person_photo.png")), type: :dropzone
      attach_file guardian_photo_field, Rails.root.join(file_fixture("hemsworth.jpeg")), visible: false

      click_button create_release_button

      expect(page).to have_content(create_release_notice)
      expect(page).to have_photo("person_photo.png")
    end

    scenario "updating an existing release" do
      talent_release = create(:talent_release, project: project)

      visit project_talent_releases_path(project)
      click_link *update_talent_release_link(talent_release)

      fill_in "talent_release[person_first_name]", with: "New"
      fill_in "talent_release[person_last_name]", with: "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" do
      talent_release = create(:talent_release, project: project)

      visit project_talent_releases_path(project)
      click_link *destroy_talent_release_link(talent_release)

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

    scenario "searching for a release", js: true do
      create(:talent_release, person_first_name: "Robert Downey Jr.", person_last_name: "Downey Jr.", project: project)
      create(:talent_release, person_first_name: "Chris", person_last_name: "Evans", project: project)

      visit project_talent_releases_path(project)

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

      expect(page).to have_content("Robert Downey Jr.")
      expect(page).not_to have_content("Chris Evans")
      expect(page).to have_field("Search", with: "Robert")
    end

    scenario "adding photos to an existing release", js: true do
      create(:talent_release, person_first_name: "Robert", person_last_name: "Downey Jr.", project: project)

      visit project_talent_releases_path(project)

      click_on "Manage"
      click_on "Photos"

      expect(page).to have_content("Add Photos")
      expect(page).to have_content("Robert Downey Jr.")

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

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

    scenario "viewing the contract PDF" do
      talent_release = create(:talent_release_with_contract_template_and_photos,
                              :native,
                              project: project,
                              person_first_name: "Jane",
                              person_last_name: "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),),
                              ])

      sign_in(current_user)
      visit project_talent_releases_path(project)

      click_link *view_release_pdf_link_for(talent_release)

      expect(content_type).to eq("application/pdf")
      expect(content_disposition).to include("inline")
      expect(pdf_filename).to include("doe-jane")
      expect(pdf_body).to have_content("Jane Doe")
      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 photos_heading.upcase
      expect(pdf_body).to have_content("person_photo.png")
    end

    scenario "viewing the contract PDF for a minor without guardian photo" do
      talent_release = create(:talent_release_with_contract_template, :native, :minor, project: project)

      visit project_talent_releases_path(project)
      click_link *view_release_pdf_link_for(talent_release)

      expect(content_type).to eq("application/pdf")
      expect(content_disposition).to include("inline")
      expect(pdf_filename).to include(talent_release.filename_suffix.parameterize)
      expect(pdf_body).to have_content(talent_release.name)
      expect(pdf_body).to have_content(talent_release.guardian_name)
      expect(pdf_body).to have_content photos_heading.upcase
      talent_release.photos.each do |photo|
        expect(pdf_body).to have_content(photo.filename.to_s)
      end
    end

    scenario "viewing the contract PDF for a minor with guardian photo" do
      talent_release = create(:talent_release_with_contract_template, :native, :minor_with_guardian_photo, project: project)

      visit project_talent_releases_path(project)
      click_link *view_release_pdf_link_for(talent_release)

      expect(content_type).to eq("application/pdf")
      expect(content_disposition).to include("inline")
      expect(pdf_filename).to include(talent_release.filename_suffix.parameterize)
      expect(pdf_body).to have_content(talent_release.name)
      expect(pdf_body).to have_content(talent_release.guardian_name)
      expect(pdf_body).to have_content photos_heading(2).upcase
      talent_release.photos.each do |photo|
        expect(pdf_body).to have_content(photo.filename.to_s)
      end
      expect(pdf_body).to have_content(talent_release.guardian_photo.filename.to_s)
    end
  end

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

    scenario "should not show download" do
      create(:talent_release_with_contract_template, person_first_name: "Robert", person_last_name: "Downey Jr.", project: project)

      sign_in current_user
      visit project_talent_releases_path(project)

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

  private

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

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

  def person_is_minor_checkbox
    "talent_release_minor"
  end

  def guardian_first_name_field
    "Guardian first name"
  end

  def guardian_last_name_field
    "Guardian last name"
  end

  def guardian_phone_field
    "Guardian phone"
  end

  def guardian_photo_field
    "talent_release[guardian_photo]"
  end

  def have_photo_button
    have_selector(".take-photo-button")
  end

  def person_first_name_field
    "talent_release[person_first_name]"
  end

  def person_last_name_field
    "talent_release[person_last_name]"
  end

  def person_address_field
    "talent_release[person_address_street1]"
  end

  def person_email_field
    "talent_release[person_email]"
  end

  def person_phone_field
    "talent_release[person_phone]"
  end

  def import_talent_release_link(project)
    ["Import Release", href: new_project_talent_release_path(project)]
  end

  def update_talent_release_link(talent_release)
    ["Edit", href: edit_talent_release_path(talent_release)]
  end

  def destroy_talent_release_link(talent_release)
    ["Delete", href: talent_release_path(talent_release)]
  end

  def view_release_pdf_link_for(talent_release)
    ["Download", href: talent_release_contracts_path(talent_release, format: "pdf")]
  end

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

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

  def create_release_notice
    t "talent_releases.create.notice"
  end

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

  def update_release_notice
    t "talent_releases.update.notice"
  end

  def destroy_release_alert
    t "talent_releases.destroy.alert"
  end

  def successful_submission_message
    "Your release was successfully submitted. Thank you."
  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
