module ExcelReports
  module VideoReports
    module DiscoveryProductionElementsLogs
      class MusicSheet < ::ExcelReports::Worksheet
        def title
          "Music"
        end

        def fill_content(sheet)
          sheet.add_row ["MUSIC LOG (for all episodes/programs) for commissioned work-for-hire music only"]
          sheet.add_row ["Not applicable if program contains 100% library"]
          sheet.add_row ["Episode Title(s) or list \"All\"", "TRACK TITLE", "COMPOSER", "PUBLISHER"]

          data.each do |confirmation_data|
            sheet.add_row [
              confirmation_data.episode_title, 
              confirmation_data.source_file_name, 
              composers(confirmation_data), 
              publishers(confirmation_data),
            ]
          end
        end

        def format(sheet)
          sheet.column_widths *column_widths
        end

        def style(sheet)
          sheet.add_style "A1", Styles::BOLD, { sz: 14 }
          sheet.add_style "A2", { sz: 14 }
          sheet.add_style "A3:D3", Styles::TABLE_HEADER
          sheet.add_style "A3", Styles::WRAP_TEXT

          if has_data?
            sheet.add_style "A#{data_start_index}:D#{data_end_index}", Styles::TABLE_DATA
          end
        end

        private

        def column_widths
          [20, 20, 30, 60]
        end

        def has_data?
          data.any?
        end

        def data_start_index
          4
        end

        def data_end_index
          (data_start_index + data.size) - 1
        end

        def composers(confirmation_data)
          confirmation_data.confirmation.composer_info.split("|").join("\n")
        end

        def publishers(confirmation_data)
          confirmation_data.confirmation.publisher_info.split("|").join("\n")
        end
      end
    end
  end
end
