# frozen_string_literal: true

require 'spec_helper'
require 'rails_helper'

describe 'Enumeration Management', js: true do
  before(:all) do
    @repository = create(:repo, repo_code: "enum_test_#{Time.now.to_i}")
    run_indexer
  end

  before(:each) do
    login_admin
    select_repository(@repository)
    @now = Time.now.to_i
  end

  def visit_enumerations
    click_on 'System'
    click_on 'Manage Controlled Value Lists'
    wait_for_ajax
  end

  def select_enum(dropdown_text)
    visit_enumerations
    element = find('.alert.alert-info.with-hide-alert')
    expect(element.text).to eq 'Please select a Controlled Value List'
    select dropdown_text, from: 'enum_selector'
    wait_for_ajax
  end

  def create_enum_value(value)
    find('a', text: 'Create Value').click
    within '#form_enumeration' do
      fill_in 'enumeration_value_', with: value
      click_on 'Create Value'
    end
  end

  describe 'accessibility' do
    it 'has acceptable color contrast in disabled buttons' do
      visit "/enumerations?id=14"
      expect(page).to be_axe_clean.checking_only :'color-contrast'
    end
  end

  it 'lets you add a new value to an enumeration' do
    select_enum 'Accession Acquisition Type (accession_acquisition_type)'

    create_enum_value("enumaration_value_#{@now}")

    element = find('.alert.alert-success.with-hide-alert')
    expect(element.text).to eq 'Value Created'
    expect(page).to have_css '.enumeration-list tr', text: "enumaration_value_#{@now}"
  end

  it 'lets you delete a value from an enumeration' do
    select_enum 'Accession Acquisition Type (accession_acquisition_type)'

    create_enum_value("enumaration_value_#{@now}")

    element = find('.alert.alert-success.with-hide-alert')
    expect(element.text).to eq 'Value Created'

    element = find('.enumeration-list tr', text: "enumaration_value_#{@now}")
    within element do
      click_on 'Delete'
    end

    within '#form_enumeration' do
      click_on 'Delete Value'
    end

    expect(page).not_to have_text "enumaration_value_#{@now}"
  end

  it 'lets you merge one value into another in an enumeration' do
    enumeration_a = "Enumeration_a_#{@now}"
    enumeration_b = "Enumeration_b_#{@now}"

    select_enum 'Accession Acquisition Type (accession_acquisition_type)'

    create_enum_value(enumeration_a)
    element = find('.alert.alert-success.with-hide-alert')
    expect(element.text).to eq 'Value Created'
    expect(page).to have_css '.enumeration-list tr', text: enumeration_a

    create_enum_value(enumeration_b)
    element = find('.alert.alert-success.with-hide-alert')
    expect(element.text).to eq 'Value Created'

    element = find('.enumeration-list tr', text: enumeration_b)
    within element do
      click_on 'Merge'
    end

    wait_for_ajax

    within '#form_enumeration' do
      select enumeration_a, from: 'merge_into'
      click_on 'Merge Value'
    end

    wait_for_ajax

    expect(page).to have_text 'Value Merged'
    expect(page).to_not have_css '.enumeration-list tr', text: enumeration_b
  end

  it 'lets you set a default enumeration (date_type)' do
    select_enum 'Date Type (date_type)'

    elements = all('tr', text: 'Set as Default')
    expect(elements.length > 0).to eq true

    default_value = elements[0].all('td').first.text

    within elements[0] do
      click_on 'Set as Default'
    end

    element = find('.alert.alert-success.with-hide-alert')
    expect(element.text).to eq 'Default Value Set'

    click_on 'Create'
    click_on 'Accession'
    click_on 'Add Date'

    element = find('#accession_dates__0__date_type_')
    expect(element.value).to eq default_value
  end

  it 'lets you add a new value to an enumeration, reorder it and then you can use it' do
    select_enum 'Collection Management Processing Priority (collection_management_processing_priority)'

    create_enum_value("enumaration_value_#{@now}")

    element = find('.alert.alert-success.with-hide-alert')
    expect(element.text).to eq 'Value Created'
    expect(page).to have_css '.enumeration-list tr', text: "enumaration_value_#{@now}"

    click_on 'Create'
    click_on 'Accession'
    fill_in 'accession_title_', with: "Accession Title #{@now}"
    fill_in 'accession_id_0_', with: "1 #{@now}"
    fill_in 'accession_id_1_', with: "2 #{@now}"
    fill_in 'accession_id_2_', with: "3 #{@now}"
    fill_in 'accession_id_3_', with: "4 #{@now}"
    fill_in 'accession_accession_date_', with: '2012-01-01'
    fill_in 'accession_content_description_', with: "Accession Description #{@now}"
    fill_in 'accession_condition_description_', with: "Accession Condition Description #{@now}"
    click_on 'Add Collection Management Fields'
    select "enumaration_value_#{@now}", from: 'accession_collection_management__processing_priority_'

    find('button', text: 'Save Accession', match: :first).click
    element = find('.alert.alert-success.with-hide-alert')
    expect(element.text).to eq "Accession Accession Title #{@now} created"

    find('a', text: "Accession Title #{@now}").click

    element = find('#accession_collection_management__accordion')
    expect(element).to have_text "enumaration_value_#{@now}"
  end

  it 'lets you see how many times the term has been used and search for it' do
    select_enum 'Collection Management Processing Priority (collection_management_processing_priority)'

    create_enum_value("enumaration_value_#{@now}")

    element = find('.alert.alert-success.with-hide-alert')
    expect(element.text).to eq 'Value Created'
    expect(page).to have_css '.enumeration-list tr', text: "enumaration_value_#{@now}"

    click_on 'Create'
    click_on 'Accession'
    fill_in 'accession_title_', with: "Accession Title #{@now}"
    fill_in 'accession_id_0_', with: "1 #{@now}"
    fill_in 'accession_id_1_', with: "2 #{@now}"
    fill_in 'accession_id_2_', with: "3 #{@now}"
    fill_in 'accession_id_3_', with: "4 #{@now}"
    fill_in 'accession_accession_date_', with: '2012-01-01'
    fill_in 'accession_content_description_', with: "Accession Description #{@now}"
    fill_in 'accession_condition_description_', with: "Accession Condition Description #{@now}"
    click_on 'Add Collection Management Fields'
    select "enumaration_value_#{@now}", from: 'accession_collection_management__processing_priority_'

    find('button', text: 'Save Accession', match: :first).click
    element = find('.alert.alert-success.with-hide-alert')
    expect(element.text).to eq "Accession Accession Title #{@now} created"

    run_index_round

    visit '/'
    click_on 'System'
    click_on 'Manage Controlled Value Lists'
    select 'Collection Management Processing Priority (collection_management_processing_priority)', from: 'enum_selector'

    element = find('.enumeration-list tr', text: "enumaration_value_#{@now}")
    expect(element).to have_text '1 related item'
  end

  it 'lets you suppress an enumeration value' do
    select_enum 'Collection Management Processing Priority (collection_management_processing_priority)'

    create_enum_value("enumaration_value_#{@now}")

    element = find('.alert.alert-success.with-hide-alert')
    expect(element.text).to eq 'Value Created'

    element = find('.enumeration-list tr', text: "enumaration_value_#{@now}")
    within element do
      click_on 'Suppress'
    end

    element = find('.enumeration-list tr', text: "enumaration_value_#{@now}")
    expect(element).to have_css 'a', text: 'Unsuppress'

    click_on 'Create'
    click_on 'Accession'
    fill_in 'accession_title_', with: "Accession Title #{@now}"
    fill_in 'accession_id_0_', with: "1 #{@now}"
    fill_in 'accession_id_1_', with: "2 #{@now}"
    fill_in 'accession_id_2_', with: "3 #{@now}"
    fill_in 'accession_id_3_', with: "4 #{@now}"
    fill_in 'accession_accession_date_', with: '2012-01-01'
    fill_in 'accession_content_description_', with: "Accession Description #{@now}"
    fill_in 'accession_condition_description_', with: "Accession Condition Description #{@now}"
    click_on 'Add Collection Management Fields'

    elements = all('#accession_collection_management__processing_priority_ option')
    options = elements.map { |option| option.value }
    expect(options.include?("enumaration_value_#{@now}")).to eq false

    find('button', text: 'Save Accession', match: :first).click
    element = find('.alert.alert-success.with-hide-alert')
    expect(element.text).to eq "Accession Accession Title #{@now} created"
  end

  it 'lets you delete a suppressed enumeration value' do
    select_enum 'Collection Management Processing Priority (collection_management_processing_priority)'

    create_enum_value("enumaration_value_#{@now}")

    element = find('.alert.alert-success.with-hide-alert')
    expect(element.text).to eq 'Value Created'

    element = find('.enumeration-list tr', text: "enumaration_value_#{@now}")
    within element do
      click_on 'Suppress'
    end

    element = find('.alert.alert-success.with-hide-alert')
    expect(element.text).to eq 'Value Updated'

    element = find('.enumeration-list tr', text: "enumaration_value_#{@now}")
    within element do
      click_on 'Delete'
    end

    within '#form_enumeration' do
      click_on 'Delete Value'
    end

    wait_for_ajax

    expect(page).to have_text 'Value Deleted'
    expect(page).to_not have_css '.enumeration-list tr', text: "enumaration_value_#{@now}"
  end

  it 'lets you set a default value with another value suppressed' do
    select_enum 'Record Control Level of Detail (level_of_detail)'

    element = find('.enumeration-list tr', text: 'natc')
    within element do
      click_on 'Suppress'
    end

    element = find('.enumeration-list tr', text: 'not_applicable')
    within element do
      click_on 'Set as Default'
    end

    element = find('.enumeration-list tr', text: 'not_applicable')
    within element do
      click_on 'Unset Default'
    end
  end
end
