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 - guardian fields are required when minor checkbox is checked", js: true do
      contract_template = create(:contract_template, project: project)

      visit new_account_project_contract_template_talent_release_path(project.account, project, contract_template)

      all('input[data-required-tag="guardian"]').each do |field|
        expect(field['required']).to eq 'false'
        expect(field).not_to be_visible
      end

      page.check person_is_minor_checkbox

      all('input[data-required-tag="guardian"]').each do |field|
        expect(field['required']).to eq 'true'
        expect(field).to be_visible
      end
    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 guardian_email_field, with: "valid@email.com"
        fill_in_guardian_address_fields
        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

    scenario "creating a release for a minor with two guardians", 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_heading.upcase
      expect(page).not_to have_content guardian_2_information_heading.upcase
      expect(page).not_to have_content guardian_photo_heading.upcase
      expect(page).not_to have_content guardian_2_photo_heading.upcase

      page.check person_is_minor_checkbox
      expect(page).to have_content guardian_information_heading.upcase
      expect(page).to have_content guardian_2_information_heading.upcase
      expect(page).to have_content guardian_photo_heading.upcase
      expect(page).to have_content guardian_2_photo_heading.upcase

      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"

      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 guardian_email_field, with: "valid@email.com"

      fill_in_guardian_address_fields

      fill_in guardian_2_first_name_field, with: "Second"
      fill_in guardian_2_last_name_field, with: "Guardian"

      drop_file Rails.root.join(file_fixture("person_photo.png")), type: :dropzone
      attach_file guardian_photo_field, file_fixture("hemsworth.jpeg"), visible: :all
      attach_file guardian_2_photo_field, file_fixture("person_photo.png"), visible: :all
      draw_signature file_fixture("signature.png"), "talent_release_signature_base64"

      click_button submit_button

      expect(page).to have_content success_submit_message
      expect(TalentRelease.last.guardian_2_photo.attached?).to eq true
      expect(TalentRelease.last.guardian_2_name).to eq "Second Guardian"
    end

    scenario "creating a release, if contract template contains signature legal language, it is shown" do
      contract_template = create(:contract_template, project: project, signature_legal_text: dummy_signature_legal_text)
      visit new_account_project_contract_template_talent_release_path(project.account, project, contract_template)

      expect(page).to have_content dummy_signature_legal_text
    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 "creating a release for minor with two guardians", js: true do
      visit new_project_talent_release_path(project)

      expect(page).not_to have_content guardian_photo_heading
      expect(page).not_to have_content guardian_2_photo_heading

      page.check person_is_minor_checkbox

      expect(page).to have_content guardian_photo_heading
      expect(page).to have_content guardian_2_photo_heading

      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 guardian_2_first_name_field, with: "Second"
      fill_in guardian_2_last_name_field, with: "Guardian"

      fill_in_exploitable_rights

      attach_file contract_field, 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
      attach_file guardian_2_photo_field, Rails.root.join(file_fixture("pratt.jpg")), visible: false

      click_button create_release_button

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

      expect(TalentRelease.last.guardian_2_photo.attached?).to eq true
      expect(TalentRelease.last.guardian_2_name).to eq "Second Guardian"
    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 account manager" do
    let(:current_user) { create(:user, :account_manager) }

    before do
      sign_in current_user
    end

    scenario "Review action in Manage menu is visible" do
      create(:talent_release_with_contract_template, :native, project: project)

      visit project_talent_releases_path(project)

      expect(page).to have_link(review_action, exact: true)
    end

    scenario "Reviewing release" do
      create(:talent_release_with_contract_template, :native, project: project)

      visit project_talent_releases_path(project)

      click_link review_action

      expect(page).to have_content review_page_heading
      expect(page).to have_content approve_button
    end

    scenario "Approved releases have checkmark and non-approved releases don't have checkmarks" do
      create(:talent_release_with_contract_template, :native, project: project)

      visit project_talent_releases_path(project)
      expect(page).to have_css('i.fa.fa-check-circle.fa-2x', count: 0)

      create(:talent_release_with_contract_template, :native, project: project, approved_by_user_email: "some@email.com", approved_at: DateTime.now)
      visit project_talent_releases_path(project)

      expect(page).to have_css('i.fa.fa-check-circle.fa-2x', count: 1)
    end

    scenario 'When viewing the contract PDF of approved release there is page for office use only' do
      talent_release = create(:talent_release_with_contract_template,
                               :native,
                               project: project,
                               person_first_name: 'Jane',
                               person_last_name: 'Doe',
                               approved_by_user_name: "Big Joe",
                               approved_by_user_email: "some@email.com",
                               approved_at: DateTime.now)

      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_body).to have_content for_office_use_only.upcase
      expect(pdf_body).to have_content producer_label
      expect(pdf_body).to have_content production_label
      expect(pdf_body).to have_content issued_to_label
      expect(pdf_body).to have_content issued_by_label
      expect(pdf_body).to have_content date_issued
      expect(pdf_body).to have_content 'Big Joe'
    end

    scenario 'When viewing the contract PDF of not approved release there is no page for office use only' do
      talent_release = create(:talent_release_with_contract_template,
                               :native,
                               project: project,
                               person_first_name: 'Jane',
                               person_last_name: 'Doe')

      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_body).not_to have_content for_office_use_only.upcase
      expect(pdf_body).not_to have_content producer_label
      expect(pdf_body).not_to have_content production_label
      expect(pdf_body).not_to have_content issued_to_label
      expect(pdf_body).not_to have_content issued_by_label
      expect(pdf_body).not_to have_content date_issued
      expect(pdf_body).not_to have_content 'Big Joe'
    end
  end

  context "when the user is project manager" do
    before do
      sign_in current_user
    end

    scenario "Review action in Manage menu is not visible" do
      create(:talent_release_with_contract_template, :native, project: project)

      visit project_talent_releases_path(project)
      click_on "Manage"

      expect(page).not_to have_link(review_action, exact: true)
    end
  end

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

    before do
      sign_in current_user
    end

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

      visit project_talent_releases_path(project)

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

    scenario "Review action in Manage menu is not visible" do
      create(:talent_release_with_contract_template, :native, project: project)

      visit project_talent_releases_path(project)
      click_on "Manage"

      expect(page).not_to have_link(review_action, 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_2_first_name_field
    "talent_release[guardian_2_first_name]"
  end

  def guardian_2_last_name_field
    "talent_release[guardian_2_last_name]"
  end

  def guardian_address_street1_field
    "talent_release[guardian_address_street1]"
  end

  def guardian_address_city_field
    "talent_release[guardian_address_city]"
  end

  def guardian_address_state_field
    "talent_release[guardian_address_state]"
  end

  def guardian_address_zip_field
    "talent_release[guardian_address_zip]"
  end

  def guardian_phone_field
    "Guardian phone"
  end

  def guardian_email_field
    "talent_release[guardian_email]"
  end

  def guardian_photo_field
    "talent_release[guardian_photo]"
  end

  def guardian_2_photo_field
    "talent_release[guardian_2_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

  def guardian_information_heading
    t 'public.talent_releases.new.guardian_info.heading'
  end

  def guardian_2_information_heading
    t 'public.talent_releases.new.guardian_2_info.heading'
  end

  def guardian_photo_heading
    t 'public.talent_releases.new.guardian_photo.heading'
  end

  def guardian_2_photo_heading
    t 'public.talent_releases.new.guardian_2_photo.heading'
  end

  def submit_button
    t 'shared.submit_release_long'
  end

  def success_submit_message
    "Your release was successfully submitted. Thank you."
  end

  def contract_field
    "talent_release[contract]"
  end

  def fill_in_guardian_address_fields
    fill_in guardian_address_street1_field, with: "124 Test Lane"
    fill_in guardian_address_city_field, with: "New York"
    fill_in guardian_address_state_field, with: "NY"
    fill_in guardian_address_zip_field, with: '1000'
  end

  def dummy_signature_legal_text
    'Some signature legal language'
  end

  def review_action
    t 'talent_releases.talent_release.actions.review'
  end

  def review_page_heading
    t 'approvals.new.heading', release_type: "Talent Release"
  end

  def approve_button
    t 'approvals.new.actions.approve'
  end

  def for_office_use_only
    t 'contracts.for_office_use_only.heading'
  end

  def producer_label
    t 'contracts.for_office_use_only.description_labels.producer'
  end

  def production_label
    t 'contracts.for_office_use_only.description_labels.production'
  end

  def issued_to_label
    t 'contracts.for_office_use_only.description_labels.issued_to'
  end

  def issued_by_label
    t 'contracts.for_office_use_only.description_labels.issued_by'
  end

  def date_issued
    t 'contracts.for_office_use_only.description_labels.date_issued'
  end
end
