module ExcelReports
  module AudioReports
    class AudioConfirmationData
      def initialize(audio_confirmation)
        @audio_confirmation = audio_confirmation
      end

      def cue_number
        ""
      end

      def timecode_in
        audio_confirmation.timecode_in
      end

      def title_and_source_file_name
        [audio_confirmation.title, audio_confirmation.source_file_name].reject(&:blank?).join(" - ")
      end

      def catalog
        audio_confirmation.catalog
      end

      def use
        "#{audio_confirmation.music_type} #{audio_confirmation.music_category}"
      end

      def interested_parties
        "Composers:\n#{audio_confirmation.composer_info}\nPublishers:\n#{audio_confirmation.publisher_info}"
      end

      def composers
        converted_composers.map do |composer|
          "#{composer.name} (#{composer.affiliation})"
        end.join("\n")
      end

      def composers_split
        converted_composers.map(&:percentage).join("\n")
      end

      def composers_with_split
        converted_composers.map do |composer|
          "#{composer.name} (#{composer.affiliation}) #{composer.percentage}%"
        end.join("\n")
      end

      def composers_cae_numbers
        converted_composers.map(&:cae_number).join("\n")
      end

      def publishers
        converted_publishers.map do |publisher|
          "#{publisher.name} (#{publisher.affiliation})"
        end.join("\n")
      end

      def publishers_split
        converted_publishers.map(&:percentage).join("\n")
      end

      def publishers_with_split
        converted_publishers.map do |publisher|
          "#{publisher.name} (#{publisher.affiliation}) #{publisher.percentage}%"
        end.join("\n")
      end

      def music_type
        audio_confirmation.music_type.to_s.first
      end

      def music_category
        audio_confirmation.music_category.to_s.first
      end

      def timecode_out
        audio_confirmation.timecode_out
      end

      def duration
        audio_confirmation.duration
      end

      def origin
        if audio_confirmation.confirmation_type_library?
          "Production Library (Non-affiliated)"
        else
          "Commissioned"
        end
      end

      def ==(other)
        audio_confirmation == other.audio_confirmation
      end

      protected

      attr_reader :audio_confirmation

      def converted_composers
        composers = audio_confirmation.composer_info.split("|")
        composers.map do |composer_info|
          parts = composer_info.split(",")
          if cae_number_index = parts.index { |part| part.include?("$cae") }
            cae_number = parts.delete_at(cae_number_index)
          end
          Composer.new(
            name: parts[0...-2].join(","),
            affiliation: parts[-2].strip,
            percentage: parts.last,
            cae_number: cae_number.to_s.gsub("$cae:", "").strip
          )
        end
      end

      def converted_publishers
        audio_confirmation.publisher_info.split("|").map do |publisher_info|
          parts = publisher_info.split(",")
          Publisher.new(
            name: parts[0...-2].join(","),
            affiliation: parts[-2].strip,
            percentage: parts.last,
          )
        end
      end
    end
  end
end
