module ExcelReports
  module VideoReports
    module NatGeoLegalBinderLogs
      class LocationReleaseLogSheet < Worksheet
        def title
          "Location Release Log"
        end

        def fill_content(sheet)
          instructions1 = "PLEASE ARRANGE CONTRACTS IN ORDER OF FIRST APPEARANCE IN FINAL PROGRAM."
          instructions2 = "PLEASE INCLUDE ALL CONTRACTS SECURED FOR THE PROGRAM INCLUDING THOSE FOR ALL DIGITAL SOCIAL CONTENT."
          instructions3 = "IF INDIVIDUAL DID NOT APPEAR IN THE FINAL SHOW, PLEASE INDICATE \"NOT IN FINAL PROGRAM\" UNDER TIME CODE COLUMN & LIST ALPHABETICALLY."

          sheet.add_row([instructions1])
          sheet.add_row([instructions2])
          sheet.add_row([instructions3])
          sheet.add_row
          sheet.add_row(["", "", "", "RIGHTS OBTAINED", "", "", "", "", ""])
          sheet.add_row(table_headers)

          data.each_with_index do |confirmation, index|
            sheet.add_row [
              index + 1,
              confirmation.timecode_in,
              [confirmation.name, confirmation.confirmation.releasable.address].join("\n"),
              "",
              confirmation.applicable_media,
              confirmation.territory,
              confirmation.term,
              "",
              confirmation.restrictions,
            ]
          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 "D5", Styles::BOLD, Styles::FULLY_CENTERED, Styles::BG_GRAY, Styles::THIN_BLACK_BORDER
          sheet.add_style "A6:I6", Styles::BOLD, Styles::FULLY_CENTERED, Styles::THIN_BLACK_BORDER, Styles::WRAP_TEXT

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

        private

        def table_headers
          [
            "Contract Number", 
            "Program Master Timecode", 
            "Location's Name and Contact Information", 
            "Permit and/or Location Cost",
            "Media", 
            "Territory", 
            "Term", 
            "Program Specific Y/N", 
            "Restrictions"
          ]
        end

        def data_start_index
          7
        end

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

        def column_widths
          [12, 25, 35, 35, 30, 30, 30, 15, 30]
        end

        def cells_to_merge
          %w(A1:F1 A2:F2 A3:F3 D5:I5)
        end
      end
    end
  end
end
