module ExcelReports
  module GraphicReports
    class NatGeoTextGraphicsLog
      def initialize(video)
        @video = video
      end

      def to_xls
        NatGeoTextGraphicsLogs::InternalProgramGfxLogSheet.build(
          workbook, graphics_elements_data, graphics_elements_header_data
        )

        package.to_stream.read
      end

      def filename(format = "xlsx")
        name = [@video.file.filename.to_s, "text-graphics-log"].map(&:parameterize).join("_")
        [name, format].join(".")
      end

      private

      attr_reader :video

      def graphics_elements_data
        video.graphics_elements.order(timecode_in: :asc)
      end

      def graphics_elements_header_data
        video
      end

      def workbook
        @workbook ||= package.workbook
      end

      def package
        @package ||= Axlsx::Package.new
      end
    end
  end
end
