class ReportCollectionService

  def initialize(videos)
    @videos = videos
  end

  def build
    Dir.mktmpdir { |dir|
      videos.each do |video|
        build_reports_for(video, dir)
      end

      files = Dir.entries("#{dir}/").select { |f| !File.directory? f }
      raise StandardError.new "Reports not found" unless files.any?
      yield(dir, files)
    }
  end

  private

  def build_reports_for(video, dir)
    case video.project.client_name
    when "Nat Geo"
      nat_geo_reports_for(video, dir)
    else
      discovery_reports_for(video, dir)
    end

    default_reports_for(video, dir)
  end

  def save(report, dir)
    File.open("#{dir}/#{report.filename}", "wb") { |f| f.write(report.to_xls) }
  end

  def nat_geo_reports_for(video, dir)
    [ExcelReports::VideoReports::NatGeoLegalBinderLog.new(video),
     ExcelReports::GraphicReports::NatGeoTextGraphicsLog.new(video),
     ExcelReports::AudioReports::NatGeoMusicCueSheet.new(video),
     ExcelReports::AudioReports::NatGeoOriginalMusicLog.new(video)].each do |report|
      save(report, dir)
    end
  end

  def default_reports_for(video, dir)
    [ExcelReports::AudioReports::BrayInnovationGroupMusicCueReport.new(video),
     ExcelReports::IssuesAndConcernsReports::IssuesAndConcernsReport.new(video)].each do |report|
        save(report, dir)
    end
  end

  def discovery_reports_for(video, dir)
    [ExcelReports::VideoReports::DiscoveryProductionElementsLog.new(video),
     ExcelReports::GraphicReports::DiscoveryGfxCueList.new(video),
     ExcelReports::AudioReports::DiscoveryMusicCueReport.new(video)].each do |report|
       save(report, dir)
    end
  end

  attr_reader :videos
end