Then(/^the page should( not)? include Statement Attachments column$/) do |flag|
  on(Accounting::StatementsPage) do |page|
    eventually(timeout: 30) do
      table_header = page.statements_table_element.ths.map(&:text)
      error_message = "Page should#{flag} include STATEMENT ATTACHMENTS column"
      expect(table_header.include?("STATEMENT ATTACHMENTS")).to be(!flag), error_message
    end
  end
end

When(/^I click 'View' button for the last period$/) do
  on(Accounting::StatementsPage) do |page|
    page.wait_until { page.view_button_elements.any? }
    page.view_button_elements.first.click
  end
end

When(/^I click 'View' button of a period without Statement Attachments$/) do
  on(Accounting::StatementsPage) do |page|
    page.wait_until { page.view_button_elements.any? }
    page.view_button_elements.last.click
  end
end

Then(/^Statement Attachments page should be displayed with all appropriate fields$/) do
  on(Accounting::StatementAttachmentsPage) do |page|
    expected_page_header = "Statement Attachments for #{page.url.scan(/\w+_\d+/).first.tr('_', ' ')}"
    eventually(timeout: 30) do
      expect(page.page_header).to eq(expected_page_header)
      expect(page.statements_link_element.text).to eq("Statements")
      expect(page.url).to include("statement_attachment")
      expect(page.text).to include(
        "Available below are all attachments currently queued or ready for download for this period."
      )
    end
  end
end

When(/^I upload a (.*) file to S3 bucket for the selected period$/) do |file_type|
  @accounting_user ||= @users.accounting_user
  @selected_period_id = @browser.url.scan(/\d{3}/).last
  @uploaded_file_name = "auto_test_#{file_type}"
  Support::Helpers::ScenarioVariables.add_scenario_variable("Accounting user ", @accounting_user)
  Support::Helpers::ScenarioVariables.add_scenario_variable("Selected Period id ", @selected_period_id)

  uploaded_file_type = file_type.downcase

  upload_params = "L#{@accounting_user.info[:id]}_#{@selected_period_id}"
  file_to_upload = "#{upload_params}_#{@uploaded_file_name}.#{uploaded_file_type}"
  Support::Helpers::ScenarioVariables.add_scenario_variable("Uploaded file name ", file_to_upload)

  templates_dir = "#{Support::Paths.assets}/accounting/statement_attachements"
  FileUtils.cp("#{templates_dir}/template_#{uploaded_file_type}.#{uploaded_file_type}",
               "temp/#{file_to_upload}")

  s3 = Aws::S3::Resource.new(region: "us-east-1")
  obj = s3.bucket(PROFILE.bucket[:qa_bucket_name]).object("qa-statement-attachments/#{file_to_upload}")
  @uploaded_file_path = "temp/#{file_to_upload}"
  obj.upload_file(@uploaded_file_path)
end

Then(/^a new record should be created in statement_attachments_success table for (.*) file$/) do |file_type|
  params = {
    file_name: "#{@uploaded_file_name}.#{file_type.downcase}",
    label_id: @accounting_user.info[:id],
    period_id: @selected_period_id
  }
  statement_attachments = DataHelpers::AccountingStatementAttachments.new(params)
  eventually(timeout: 10, interval: 2) do
    expect(statement_attachments.upload_successful?).to eq(true),
                                                        "#{@uploaded_file_name} was not uploaded to S3."
  end
end

Then(/^a new record should be created in statement_attachments_error table for (.*) file$/) do |file|
  file_name = file.include?(".") ? file : "#{@uploaded_file_name}.#{file.downcase}"
  params = {
    file_name: file_name
  }
  eventually(timeout: 10, interval: 2) do
    expect(DataHelpers::AccountingStatementAttachments.new(params).upload_error?).to eq(true)
  end
end

Then(/^the file should( not)? be displayed on the page$/) do |flag|
  on(Accounting::StatementAttachmentsPage) do |page|
    eventually do
      @browser.refresh
      page.wait_until { page.file_row_elements.any? }
      error_message = "#{@uploaded_file_name} should#{flag} be displayed on the page"
      expect(!page.file_details(@uploaded_file_name).nil?).to eq(!flag), error_message
      @uploaded_file_details = page.file_details(@uploaded_file_name)
    end
  end
end

Then(/^I should see the upload date "([^"]*)"$/) do |upload_date_format|
  expect(@uploaded_file_details[:upload_date]).to eq(Time.now.strftime(upload_date_format))
end

Then(/^I should see the file name "([^"]*)"$/) do |file_name|
  expect(@uploaded_file_details[:file_name]).to eq(file_name)
end

Then(/^I should see the file type "([^"]*)"$/) do |file_type|
  expect(@uploaded_file_details[:file_type]).to eq(file_type.downcase)
end

Then(/^I should see the status "([^"]*)"$/) do |status|
  expect(@uploaded_file_details[:status]).to eq(status)
end

When(/^I click '(.*)' for the last file$/) do |link_name|
  on(Accounting::StatementAttachmentsPage) do |page|
    page.attachments_table_element.link(text: link_name).click
  end
end

Then(/^the link text should be changed to '(.*)'$/) do |link_name|
  on(Accounting::StatementAttachmentsPage) do |page|
    page.attachments_table_element.link(text: link_name).wait_until(&:present?)
    link = page.attachments_table_element.links.first
    expect(link.text).to eq(link_name)
    @statement_attachment_url = link.href
    expect(@statement_attachment_url).to include(PROFILE.bucket[:qa_bucket_name])
  end
end

When(/^I download the Statement Attachment file$/) do
  @downloaded_file_path = FileSystemHelpers::Downloader.download(@statement_attachment_url,
                                                                 with_timestamp: false).first
end

Then(/^the downloaded file should exactly the same as the just uploaded one$/) do
  expect(FileUtils.compare_file(@uploaded_file_path, @downloaded_file_path)).to be_truthy
end

When(/^I upload (.*) file to to qa\-statement\-attachments S3 bucket directory$/) do |file_name|
  templates_dir = "#{Support::Paths.assets}/accounting/statement_attachements"
  s3 = Aws::S3::Resource.new(region: "us-east-1")
  obj = s3.bucket(PROFILE.bucket[:qa_bucket_name]).object("qa-statement-attachments/#{file_name}")
  obj.upload_file("#{templates_dir}/#{file_name}")
end

Then(/^I should see the file name with the selected period id$/) do
  on(Accounting::StatementAttachmentsPage) do |page|
    page.attachments_table_element.wait_until(&:present?)
    expected_file_name = "#{@uploaded_file_name}(#{@selected_period_id})"
    expect(page.file_details(expected_file_name)).to be_truthy,
                                                     "'#{expected_file_name}' was not found on the page"
  end
end

Then(/^I remove the uploaded (.*) file from the qa bucket$/) do |file_type|
  label = "L#{@accounting_user.info[:id]}"
  file_to_remove = "#{label}_#{@selected_period_id}_#{@uploaded_file_name}"
  file_path = "qa-per-label-statement-attachments/#{label}/#{file_to_remove}.#{file_type.downcase}"
  step "I remove the \"#{file_path}\" file from the qa bucket"
end

Then(/^I remove the "(.*)" file from the (.*) bucket$/) do |file_name, bucket|
  bucket_name = PROFILE.bucket["#{bucket}_bucket_name".to_sym]
  s3_bucket = Aws::S3::Resource.new(region: "us-east-1").bucket(bucket_name)
  file = s3_bucket.object(file_name)
  file.delete
end
