module ExcelReports
  module AudioReports
    module NatGeoOriginalMusicLogs
      class MainSheet < Worksheet
        def title
          "Nat Geo Original Music Log"
        end

        def fill_content(sheet)
          sheet.add_row ["National Geographic", "", "", "",]
          sheet.add_row ["National Geographic Music", "", "", "",]
          sheet.add_row ["Original Music Log", "", "", "",]
          sheet.add_row ["[PROGRAM]", "", "", ""]
          sheet.add_row ["[SEASON #]", "", "", ""]
          sheet.add_row [BigMediaTime.time_zone_now.to_date.strftime("%D"), "", "", ""]
          sheet.add_row table_headers

          data.each_with_index do |datum, index|
            sheet.add_row [
              datum.title_and_source_file_name,
              datum.composers_with_split,
              datum.composers_cae_numbers,
              datum.publishers_with_split,
            ]
          end
        end

        def format(sheet)
          sheet.column_widths *column_widths
          cells_to_merge.each { |cell| sheet.merge_cells cell }
        end

        def style(sheet)
          sheet.add_style "A1:D1", Styles::BOLD, { sz: 18 }
          sheet.add_style "A2:D2", Styles::BOLD, Styles::FULLY_CENTERED, { sz: 16 }
          sheet.add_style "A3:D3", Styles::FULLY_CENTERED, { sz: 14 }
          sheet.add_style "A#{data_start_index-1}:D#{data_start_index-1}", Styles::BOLD, Styles::THIN_BLACK_BORDER, Styles::WRAP_TEXT, Styles::BG_GRAY

          if data.any?
            sheet.add_style "A#{data_start_index}:D#{data_end_index}", Styles::THIN_BLACK_BORDER
            sheet.add_style "A#{data_start_index}:D#{data_end_index}", Styles::WRAP_TEXT
          end
        end

        private

        def table_headers
          [
            "CUE TITLE",
            "COMPOSER(S), % SPLIT & PRO",
            "COMPOSER CAE/IPI#",
            "PUBLISHER(S), % SPLIT & PRO",
          ]
        end

        def column_widths
          [25, 45, 25, 80]
        end

        def cells_to_merge
          %w(A1:D1 A2:D2 A3:D3)
        end

        def data_start_index
          8
        end

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