require "rails_helper"

module ExcelReports
  module VideoReports
    module DiscoveryProductionElementsLogs
      RSpec.describe MusicSheet 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 'Music'" do
            expect(described_class.new(workbook).title).to eq "Music"
          end
        end

        describe "#fill_content" do
          it "adds content from music release" do
            user = create(:user)
            project = create(:project, account:user.primary_account)
            video = create(:video,
                           project: project,
                           number: "45",
                           name: "Amazing Race",
                          )
            confirmation = create(:audio_confirmation,
                                  video: video,
                                  source_file_name: "source_file_name",
                                  composer_info: "composer1, affiliation, 50%|composer2, affiliation, 50%",
                                  publisher_info: "publisher1, affiliation, 50%|publisher2, affiliation, 50%",
                                  confirmation_type: "original_music"
                                 )
            wrapped_confirmation = ReleasableDataAdapter.new(confirmation)

            sheet = instance_double(Axlsx::Worksheet, :sheet)
            expect(sheet).to receive(:add_row).with(["MUSIC LOG (for all episodes/programs) for commissioned work-for-hire music only"])
            expect(sheet).to receive(:add_row).with(["Not applicable if program contains 100% library"])
            expect(sheet).to receive(:add_row).with(["Episode Title(s) or list \"All\"", "TRACK TITLE", "COMPOSER", "PUBLISHER"])
            expect(sheet).to receive(:add_row).with(["Amazing Race", "source_file_name", "composer1, affiliation, 50%\ncomposer2, affiliation, 50%", "publisher1, affiliation, 50%\npublisher2, affiliation, 50%"])

            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 20, 20, 30, 60" do
            expect(sheet).to receive(:column_widths).with(20, 20, 30, 60)

            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: 14 })
            expect(sheet).to receive(:add_style).with(
              "A3:D3",
              {
                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(
              "A3",
              {
                alignment: { :wrap_text => true },
              }
            )
            expect(sheet).to receive(:add_style).at_least(1).with(
              "A4:D5",
              {
                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
