require "rails_helper"

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

      subject { described_class.new(workbook) }

      it_behaves_like "a worksheet"

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

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

          project = create(:project, client_name: "client_name", producer_name: "producer_name")
          video = create(:video, project: project, number: 42, name: "video_name")
          header_data = DiscoveryMusicCueHeaderData.new(video)
          data = [
            AudioConfirmationData.new(
              build(:audio_confirmation,
                timecode_in: "timecode_in_1",
                title: "title_1",
                catalog: "catalog_1",
                duration: "duration_1",
                music_type: "music_type_1",
                music_category: "music_category_1",
                composer_info: "composer_info_1",
                publisher_info: "publisher_info_1",
                source_file_name: "source_file_name_1",
              )
            ),
            AudioConfirmationData.new(
              build(:audio_confirmation,
                timecode_in: "timecode_in_2",
                title: "title_2",
                catalog: "catalog_2",
                duration: "duration_2",
                music_type: "music_type_2",
                music_category: "music_category_2",
                composer_info: "composer_info_2",
                publisher_info: "publisher_info_2",
                source_file_name: "source_file_name_2",
              )
            )
          ]

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

          expect(sheet).to have_received(:add_row).with(["Original Production Title", "video_name", "Original Series Title", "", "Broadcaster", ""])
          expect(sheet).to have_received(:add_row).with(["Alternative Production Title", "", "Alternative Series Title", "", "Production Company", "producer_name"])
          expect(sheet).to have_received(:add_row).with(["Local Production Title", "", "Local Series Title", "", "", ""])
          expect(sheet).to have_received(:add_row).with(["Working Production Title", "", "Working Series Title", "", "Production Number", ""])
          expect(sheet).to have_received(:add_row).with(["Version Production Title", "", "Version Series Title", "", "Director", ""])
          expect(sheet).to have_received(:add_row).with(["Episode No.", "42", "Season No.", "", "Production Parent Identifier", ""])
          expect(sheet).to have_received(:add_row).with(["Year", "", "Country", "", "Soundmouse Legacy Identifier", ""])
          expect(sheet).to have_received(:add_row).with(["Duration", "", "Type", "", "Production ID", ""])
          expect(sheet).to have_received(:add_row).with(["First Transmission", "", "Source", ""])
          expect(sheet).to have_received(:add_row).with(["Product Name", ""])
          expect(sheet).to have_received(:add_row).with(["Client Name", "client_name"])
          expect(sheet).to have_received(:add_row).with(["Narrative", ""])
          expect(sheet).to have_received(:add_row).with(["End Line", ""])
          expect(sheet).to have_received(:add_row).with(["Clock Number", ""])
          expect(sheet).to have_received(:add_row).with(["ISAN", ""])
          expect(sheet).to have_received(:add_row).with(no_args)
          expect(sheet).to have_received(:add_row).with([
            "No.",
            "Timecode",
            "Title",
            "Music Origin",
            "Use (Theme/Description)",
            "Interested Parties",
            "Identifiers",
            "Duration",
          ])
          expect(sheet).to have_received(:add_row).with([
            1,
            "timecode_in_1",
            "title_1 - source_file_name_1",
            "catalog_1",
            "music_type_1 music_category_1",
            "Composers:\ncomposer_info_1\nPublishers:\npublisher_info_1",
            "",
            "duration_1",
          ])
          expect(sheet).to have_received(:add_row).with([
            2,
            "timecode_in_2",
            "title_2 - source_file_name_2",
            "catalog_2",
            "music_type_2 music_category_2",
            "Composers:\ncomposer_info_2\nPublishers:\npublisher_info_2",
            "",
            "duration_2",
          ])
        end
      end

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

          subject.format(sheet)

          expect(sheet).to have_received(:column_widths).with(20, 20, 40, 20, 20, 20, 20, 20)
        end
      end

      describe "#style" do
        it "sets sheet style" do
          sheet = instance_double(Axlsx::Worksheet, :sheet)
          expect(sheet).to receive(:add_style).with("A1:A15",
            {
              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("C1:C9",
            {
              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("E1:E8",
            {
              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:B15",
            {
              border: { :style => :thick, :color => "000000" },
              alignment: { :wrap_text => true, :horizontal => :left },
              sz: 10,
            }
          )
          expect(sheet).to receive(:add_style).with(
            "D1:D9",
            {
              border: { :style => :thick, :color => "000000" },
              alignment: { :wrap_text => true, :horizontal => :left },
              sz: 10,
            }
          )
          expect(sheet).to receive(:add_style).with(
            "F1:F8",
            {
              border: { :style => :thick, :color => "000000" },
              alignment: { :wrap_text => true, :horizontal => :left },
              sz: 10,
            }
          )
          expect(sheet).to receive(:add_style).with(
            "A17:H17",
            {
              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(
            "A18:H19",
            {
              border: { style: :thick, color: "000000" },
              alignment: { wrap_text: true },
              sz: 10
            }
          )

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