require "rails_helper"

RSpec.describe EdlEventsPresenter do
  let(:edl_gateway) { instance_double(EdlEventGateway) }

  describe "#present" do
    it "returns hash of edl_events, edl_attributes, info_message" do
      allow(edl_gateway).to receive(:edl_events).and_return(build_list(:edl_event, 1))

      expect(described_class.new(edl_gateway).present).to eq({
        edl_events: build_list(:edl_event, 1),
        edl_attributes: {
          timecode_in: "timecode_in",
          timecode_out: "timecode_out",
          duration: "duration",
          source_file_name: "source_file_name",
          clip_name: "clip_name",
          description: "description",
          channel: "V",
        },
        info_message: "An EDL event was found. Data is shown below",
      })
    end

    describe "edl_attributes" do
      context "when 1 edl event" do
        before :each do
          allow(edl_gateway).to receive(:edl_events).and_return(build_list(:edl_event, 1))
        end

        it "returns edl attributes from that event" do
          expect(described_class.new(edl_gateway).present[:edl_attributes]).to eq({
            timecode_in: "timecode_in",
            timecode_out: "timecode_out",
            duration: "duration",
            source_file_name: "source_file_name",
            clip_name: "clip_name",
            description: "description",
            channel: "V",
          })
        end
      end

      context "when multiple edl events" do
        before :each do
          allow(edl_gateway).to receive(:edl_events).and_return(
            [
              build(:edl_event),
              build(:edl_event, timecode_in: "NOT__timecode_in")
            ]
          )
        end

        it "returns edl attributes from first event" do
          expect(described_class.new(edl_gateway).present[:edl_attributes]).to eq({
            timecode_in: "timecode_in",
            timecode_out: "timecode_out",
            duration: "duration",
            source_file_name: "source_file_name",
            clip_name: "clip_name",
            description: "description",
            channel: "V",
          })
        end
      end

      context "when no edl events" do
        before :each do
          allow(edl_gateway).to receive(:edl_events).and_return([])
        end

        it "returns nil attributes from edl event" do
          expect(described_class.new(edl_gateway).present[:edl_attributes]).to eq({
            timecode_in: nil,
            timecode_out: nil,
            duration: nil,
            source_file_name: nil,
            clip_name: nil,
            description: nil,
            channel: nil,
          })
        end
      end
    end

    describe "info_message" do
      context "when 1 edl event" do
        before :each do
          allow(edl_gateway).to receive(:edl_events).and_return(build_list(:edl_event, 1))
        end

        it "returns 'An EDL event was found. Data is shown below'" do
          expect(described_class.new(edl_gateway).present[:info_message]).to eq "An EDL event was found. Data is shown below"
        end
      end

      context "when multiple edl events" do
        before :each do
          allow(edl_gateway).to receive(:edl_events).and_return(build_list(:edl_event, 2))
        end

        it "returns 'Multiple EDL events were found. Data for the first is shown below'" do
          expect(described_class.new(edl_gateway).present[:info_message]).to eq "Multiple EDL events were found. Data for the first is shown below"
        end
      end

      context "when no edl events" do
        before :each do
          allow(edl_gateway).to receive(:edl_events).and_return([])
        end

        it "returns 'No EDL events were found. Please enter manually'" do
          expect(described_class.new(edl_gateway).present[:info_message]).to eq "No EDL events were found. Please enter manually"
        end
      end
    end
  end
end
