require "rails_helper"

module ExcelReports
  module VideoReports
    module DiscoveryProductionElementsLogs
      RSpec.describe AcquiredFootageAndStillsSheet do
        let(:workbook) { instance_double(Axlsx::Workbook, :workbook) }

        it_behaves_like "a worksheet" do
          subject { described_class.new(workbook) }
        end

        describe "#title" do
          it "returns 'Acquired Footage & Stills'" do
            expect(described_class.new(workbook).title).to eq "Acquired Footage & Stills"
          end
        end

        describe "#fill_content" do
          it "adds content from acquired media release" do
            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
            user = create(:user)
            project = create(:project, account:user.primary_account)
            video = create(:video,
                           project: project,
                           number: "45",
                           name: "Amazing Race",
                          )
            acquired_media_release = create(:acquired_media_release,
                                            project: project,
                                            applicable_medium: ApplicableMedium.last,
                                            territory: Territory.last,
                                            term: Term.last,
                                            restriction: Restriction.last,
                                            name: "Licensor",
                                            person_phone: "1-800-978-2343",
                                           )
            confirmation = create(:video_release_confirmation,
                                  video: video,
                                  releasable: acquired_media_release,
                                  source_file_name: "source_file_name",
                                  clip_name: "clippy",
                                  timecode_in: "timecode_in",
                                  timecode_out: "timecode_out",
                                  duration: "duration",
                                  description: "description",
                                 )
            wrapped_confirmation = ReleasableDataAdapter.new(confirmation)

            sheet = instance_double(Axlsx::Worksheet, :sheet)
            expect(sheet).to receive(:add_row).with(["ACQUIRED FOOTAGE/STILLS/ PUBLIC DOMAIN LOG (for all episodes/programs)"])
            expect(sheet).to receive(:add_row).with([instructions])
            expect(sheet).to receive(:add_row).with(no_args)
            expect(sheet).to receive(:add_row).with(["EPISODE NUMBER", "EPISODE TITLE", "CLIP #", "PROGRAM MASTER TC", "", "TOTAL TIME", "BRIEF VIDEO DESCRIPTION", "LICENSOR\n(incl phone number)", "EXPLOITABLE RIGHTS", "", "", "DCL Rights Waiver Uploaded?"])
            expect(sheet).to receive(:add_row).with(["", "", "", "IN", "OUT", "", "", "", "MEDIA", "TERRITORY", "TERM", ""])
            expect(sheet).to receive(:add_row).with(["45", "Amazing Race", "source_file_name - clippy", "timecode_in", "timecode_out", "duration", "description", "Licensor\n1-800-978-2343", "All", "Worldwide", "In perpetuity", "None"])

            described_class.new(workbook, [wrapped_confirmation]).fill_content(sheet)
          end
        end

        describe "#format" do
          let(:sheet) { instance_double(Axlsx::Worksheet, :sheet) }

          before :each do
            allow(sheet).to receive(:column_widths)
            allow(sheet).to receive(:merge_cells)
          end

          it "sets sheet column widths to 10, 20, 25, 15, 15, 15, 20, 15, 15, 15, 15, 10" do
            expect(sheet).to receive(:column_widths).with(10, 20, 25, 15, 15, 15, 20, 15, 15, 15, 15, 10)

            described_class.new(workbook).format(sheet)
          end

          it "merges columns A1:M1 A2:M2 A4:A5 B4:B5 C4:C5 D4:E4 F4:F5 G4:G5 H4:H5 I4:K4 L4:L5" do
            %w(A1:M1 A2:M2 A4:A5 B4:B5 C4:C5 D4:E4 F4:F5 G4:G5 H4:H5 I4:K4 L4:L5).each do |cell|
              expect(sheet).to receive(:merge_cells).with(cell)
            end

            described_class.new(workbook).format(sheet)
          end
        end

        describe "#style" do
          it "sets sheet style" do
            sheet = instance_double(Axlsx::Worksheet, :sheet)
            expect(sheet).to receive(:add_style).with("A1", { b: true }, { sz: 14 })
            expect(sheet).to receive(:add_style).with("A2", { sz: 10 })
            expect(sheet).to receive(:add_style).with(
              "A4:L4",
              {
                bg_color: "97CBFC",
                b: true,
                border: { :style => :thick, :color => "000000" },
                alignment: { :horizontal => :center, :vertical => :center, :wrap_text => true },
                sz: 8,
              }
            )
            expect(sheet).to receive(:add_style).with(
              "B5:L5",
              {
                bg_color: "97CBFC",
                b: true,
                border: { :style => :thick, :color => "000000" },
                alignment: { :horizontal => :center, :vertical => :center, :wrap_text => true },
                sz: 8,
              }
            )
            expect(sheet).to receive(:add_style).with(
              "A4",
              {
                alignment: { :wrap_text => true },
              }
            )
            expect(sheet).to receive(:add_style).with(
              "L4",
              {
                alignment: { :wrap_text => true },
              }
            )
            expect(sheet).to receive(:add_style).at_least(1).with("A6:L7",
                                                                  {
              border: { style: :thick, color: "000000" },
              alignment: { wrap_text: true },
              sz: 10
            }
                                                                 )

            described_class.new(workbook, ["data1", "data2"]).style(sheet)
          end
        end
      end
    end
  end
end
