module ExcelReports
  module AudioReports
    class DiscoveryMusicCueSheet < ::ExcelReports::Worksheet
      def title
        "Discovery Music Cue Sheet"
      end

      def fill_content(sheet)
        sheet.add_row ["Original Production Title", header_data.title, "Original Series Title", "", "Broadcaster", ""]
        sheet.add_row ["Alternative Production Title", "", "Alternative Series Title", "", "Production Company", header_data.company_name]
        sheet.add_row ["Local Production Title", "", "Local Series Title", "", "", ""]
        sheet.add_row ["Working Production Title", "", "Working Series Title", "", "Production Number", ""]
        sheet.add_row ["Version Production Title", "", "Version Series Title", "", "Director", ""]
        sheet.add_row ["Episode No.", header_data.episode_number, "Season No.", "", "Production Parent Identifier", ""]
        sheet.add_row ["Year", "", "Country", "", "Soundmouse Legacy Identifier", ""]
        sheet.add_row ["Duration", "", "Type", "", "Production ID", ""]
        sheet.add_row ["First Transmission", "", "Source", ""]
        sheet.add_row ["Product Name", ""]
        sheet.add_row ["Client Name", header_data.client_name]
        sheet.add_row ["Narrative", ""]
        sheet.add_row ["End Line", ""]
        sheet.add_row ["Clock Number", ""]
        sheet.add_row ["ISAN", ""]

        sheet.add_row
        sheet.add_row table_headers

        data.each_with_index do |datum, index|
          sheet.add_row [
            index + 1,
            datum.timecode_in,
            datum.title_and_source_file_name,
            datum.catalog,
            datum.use,
            datum.interested_parties,
            "",
            datum.duration,
          ]
        end
      end

      def format(sheet)
        sheet.column_widths *column_widths
      end

      def style(sheet)
        sheet.add_style "A1:A15", Styles::PROGRAM_HEADER
        sheet.add_style "C1:C9", Styles::PROGRAM_HEADER
        sheet.add_style "E1:E8", Styles::PROGRAM_HEADER

        sheet.add_style "B1:B15", Styles::HEADER_DATA
        sheet.add_style "D1:D9", Styles::HEADER_DATA
        sheet.add_style "F1:F8", Styles::HEADER_DATA

        sheet.add_style "A17:H17", Styles::TABLE_HEADER

        if data.any?
          sheet.add_style "A#{data_start_index}:H#{data_end_index}", Styles::TABLE_DATA
        end
      end

      private

      def table_headers
        [
          "No.",
          "Timecode",
          "Title",
          "Music Origin",
          "Use (Theme/Description)",
          "Interested Parties",
          "Identifiers",
          "Duration",
        ]
      end

      def column_widths
        [20, 20, 40, 20, 20, 20, 20, 20]
      end

      def data_start_index
        18
      end

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