module DataHelpers
  class AccountingReportData
    def read_report_file(file_path)
      content = if file_path.end_with?(".xls")
                  encode_file(File.read(file_path))
                else
                  File.read(file_path)
                end
      content.split("\n")
    end

    def extract_zip(file_path)
      dest = "#{file_path}_unzipped"
      FileUtils.mkdir_p(dest)
      files = Zip::File.open(file_path) do |zip_file|
        zip_file.each do |f|
          fpath = File.join(dest, f.name)
          zip_file.extract(f, fpath) unless File.exist?(fpath)
        end
      end
      files.keys.map { |f| "#{dest}/#{f}" }
    end

    def expected_headers(report_type, user_type = nil)
      if user_type
        yml_file["#{user_type}_#{report_type}_report_headers".to_sym]
      else
        yml_file["#{report_type}_report_headers".to_sym]
      end
    end

    def encode_file(file_content)
      enc = Encoding::Converter.new("UTF-16LE", "UTF-8")
      enc.convert(file_content)
    end

    private

    def yml_file
      YAML.load_file(
        File.join(Support::Paths.cucumber_root, "features", "yml", "accounting_report_data.yml")
      )
    end
  end
end
