require "rails_helper"

module ExcelReports
  module AudioReports
    RSpec.describe BrayInnovationGroupMusicCueSheet do
      let(:workbook) { instance_double(Axlsx::Workbook, :workbook) }

      subject { described_class.new(workbook) }

      it_behaves_like "a worksheet"

      describe "#title" do
        it "returns 'BiG Music Cue Sheet'" do
          expect(subject.title).to eq "BiG Music Cue Sheet"
        end
      end

      describe "#fill_content" do
        it "adds headers and data to given sheet" do
          sheet = instance_double(Axlsx::Worksheet)
          allow(sheet).to receive(:add_row)

          video = create(:video,
            name: "video name",
            project: build(:project,
              producer_name: "company name",
              producer_address: "company address",
            ),
          )
          original_confirmation = create(:audio_confirmation, :original,
            video: video,
            title: "title_1",
            source_file_name: "filename_1",
            timecode_in: "01:00:00:00",
            timecode_out: "01:05:00:00",
            duration: "00:05:00",
            music_type: "Vocal",
            music_category: "Background",
            composer_info: "composer1, affiliation, 50%|composer2, affiliation, 50%",
            publisher_info: "publisher1, affiliation, 50%|publisher2, affiliation, 50%",
          )
          library_confirmation = create(:audio_confirmation, :library,
            video: video,
            title: "title_2",
            source_file_name: "filename_2",
            timecode_in: "01:00:00:00",
            timecode_out: "01:05:00:00",
            duration: "00:05:00",
            music_type: "Vocal",
            music_category: "Background",
            composer_info: "composer1, affiliation, 50%|composer2, affiliation, 50%",
            publisher_info: "publisher1, affiliation, 50%|publisher2, affiliation, 50%",
          )
          header_data = BrayInnovationGroupMusicCueHeaderData.new(video)
          data = [AudioConfirmationData.new(original_confirmation), AudioConfirmationData.new(library_confirmation)]

          described_class.new(workbook, data, header_data).fill_content(sheet)

          expect(sheet).to have_received(:add_row).with(["TITLE", "video name"])
          expect(sheet).to have_received(:add_row).with(["COMPANY NAME", "company name"])
          expect(sheet).to have_received(:add_row).with(["ADDRESS", "company address"])
          expect(sheet).to have_received(:add_row).with(["LENGTH", ""])
          expect(sheet).to have_received(:add_row).with(["TYPE", ""])
          expect(sheet).to have_received(:add_row).with(no_args)
          expect(sheet).to have_received(:add_row).with([
            "Cue #",
            "Cue Title",
            "Composer(s)/Affiliation",
            "%",
            "Publisher(s)/Affiliation",
            "%",
            "Use",
            "",
            "In Time",
            "Out Time",
            "Duration",
          ])
          expect(sheet).to have_received(:add_row).with([
            "",
            "",
            "",
            "",
            "",
            "",
            "I = Instr.\nV = Vocal",
            "B = Bckgrnd\nF = Feature\nT = Theme",
            "",
            "",
            "",
          ])
          expect(sheet).to have_received(:add_row).with([
            "",
            "title_1 - filename_1",
            "composer1 (affiliation)\ncomposer2 (affiliation)",
            "50.0\n50.0",
            "publisher1 (affiliation)\npublisher2 (affiliation)",
            "50.0\n50.0",
            "V",
            "B",
            "01:00:00:00",
            "01:05:00:00",
            "00:05:00",
          ])
          expect(sheet).to have_received(:add_row).with([
            "",
            "title_2 - filename_2",
            "composer1 (affiliation)\ncomposer2 (affiliation)",
            "50.0\n50.0",
            "publisher1 (affiliation)\npublisher2 (affiliation)",
            "50.0\n50.0",
            "V",
            "B",
            "01:00:00:00",
            "01:05:00:00",
            "00:05:00",
          ])
        end
      end

      describe "#format" do
        it "sets column widths" do
          sheet = instance_double(Axlsx::Worksheet)
          allow(sheet).to receive(:column_widths)
          allow(sheet).to receive(:merge_cells)

          subject.format(sheet)

          expect(sheet).to have_received(:column_widths).with(15, 40, 20, 10, 20, 10, 20, 20, 15, 15, 15)
          expect(sheet).to have_received(:merge_cells).with("G7:H7")
        end
      end

      describe "#style" do
        it "sets sheet style" do
          sheet = instance_double(Axlsx::Worksheet, :sheet)
          expect(sheet).to receive(:add_style).with("A1:A5",
            {
              bg_color: "97CBFC",
              b: true,
              border: { :style => :thick, :color => "000000" },
              alignment: { :horizontal => :center, :wrap_text => true },
              sz: 12,
            }
          )
          expect(sheet).to receive(:add_style).with(
            "B1:B5",
            {
              border: { :style => :thick, :color => "000000" },
              alignment: { :wrap_text => true, :horizontal => :left },
              sz: 10,
            }
          )
          expect(sheet).to receive(:add_style).with(
            "A7:K7",
            {
              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).at_least(1).with("A9:K10",
            {
              border: { style: :thick, color: "000000" },
              alignment: { wrap_text: true },
              sz: 10
            }
          )

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