module ExcelReports
  module VideoReports
    module DiscoveryProductionElementsLogs
      class AcquiredFootageAndStillsSheet < ::ExcelReports::Worksheet
        def title
          "Acquired Footage & Stills"
        end

        def fill_content(sheet)
          sheet.add_row ["ACQUIRED FOOTAGE/STILLS/ PUBLIC DOMAIN LOG (for all episodes/programs)"]
          sheet.add_row [instructions]
          sheet.add_row
          sheet.add_row table_headers
          sheet.add_row table_subheaders

          data.each do |confirmation_data|
            sheet.add_row [
              confirmation_data.episode_number,
              confirmation_data.episode_title,
              confirmation_data.source_file_and_clip_names,
              confirmation_data.timecode_in,
              confirmation_data.timecode_out,
              confirmation_data.duration,
              confirmation_data.description,
              licensor_with_phone_number(confirmation_data),
              confirmation_data.applicable_media,
              confirmation_data.territory,
              confirmation_data.term,
              confirmation_data.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 "A1", Styles::BOLD, { sz: 14 }
          sheet.add_style "A2", { sz: 10 }
          sheet.add_style "A4:L4", Styles::TABLE_HEADER
          sheet.add_style "B5:L5", Styles::TABLE_HEADER
          sheet.add_style "A4", Styles::WRAP_TEXT
          sheet.add_style "L4", Styles::WRAP_TEXT

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

        private

        def licensor_with_phone_number(confirmation)
          [
            confirmation.name,
            confirmation.confirmation.releasable.person_phone
          ].reject(&:blank?)
            .join("\n")
        end

        def data_start_index
          6
        end

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

        def instructions
          <<~INSTRUCTIONS
          All licenses must conform to the DCI contractual requirements and must be fully executed.  

          Releases, licenses and agreements should only be logged once, based on the order of their appearance in the program.

          If an image does not appear in the final Program, log that release after those in the final Program; indicate on the Log sheet “NOT IN FINAL PROGRAM” and list the camera tape only.  

          Refer to the source of all third party footage in the exact form as it appears on the release.  

          Please note that if it is not possible to deliver an English language agreement, an English language translation must accompany any agreement delivered in a foreign language (if applicable).
          INSTRUCTIONS
        end

        def table_headers
          ["EPISODE NUMBER", "EPISODE TITLE", "CLIP #", "PROGRAM MASTER TC", "", "TOTAL TIME", "BRIEF VIDEO DESCRIPTION", "LICENSOR\n(incl phone number)", "EXPLOITABLE RIGHTS", "", "", "DCL Rights Waiver Uploaded?"]
        end

        def table_subheaders
          ["", "", "", "IN", "OUT", "", "", "", "MEDIA", "TERRITORY", "TERM", ""]
        end

        def column_widths
          [10, 20, 25, 15, 15, 15, 20, 15, 15, 15, 15, 10]
        end

        def cells_to_merge
          %w(
          A1:M1 A2:M2 A4:A5 B4:B5 C4:C5 D4:E4 F4:F5 G4:G5 H4:H5 I4:K4 L4:L5
          )
        end
    end
  end
end
end
