require "rails_helper"

RSpec.describe VideoAnalyses::UnreleasedAppearancesController, type: :controller do
  let(:user) { create(:user) }
  let(:project) { create(:project, account: user.primary_account) }
  let!(:video) { create(:video, project: project) }

  before :each do
    sign_in user
  end

  render_views

  describe "#new" do
    let(:edl_event_gateway) { instance_double(EdlEventGateway) }

    before :each do
      allow(EdlEventGateway).to receive(:new).and_return(edl_event_gateway)
      allow(edl_event_gateway).to receive(:edl_events).and_return(build_list(:edl_event, 1))
    end

    it "sets unreleased_appearance, edl_events_data" do
      get :new, params: {
        unreleased_appearance: { time_elapsed: "1", notes: "great work" },
        video_id: video,
      }, xhr: true

      expect(response).to be_successful
      expect(assigns(:unreleased_appearance)).to have_attributes({
                                                                   video_id: video.id,
                                                                   time_elapsed: "1",
                                                                   notes: "great work",
                                                                   channel: "V",
                                                                   timecode_in: "timecode_in",
                                                                   timecode_out: "timecode_out",
                                                                   duration: "duration",
                                                                   source_file_name: "source_file_name",
                                                                   clip_name: "clip_name",
                                                                   description: "description",
                                                                 })
      expect(assigns(:edl_events_data)).to eq({
                                                edl_events: build_list(:edl_event, 1),
                                                edl_attributes: {
                                                  channel: "V",
                                                  timecode_in: "timecode_in",
                                                  timecode_out: "timecode_out",
                                                  duration: "duration",
                                                  source_file_name: "source_file_name",
                                                  clip_name: "clip_name",
                                                  description: "description",
                                                },
                                                info_message: "An EDL event was found. Data is shown below",
                                              })
    end
  end

  describe "#create" do
    it "creates unreleased_appearance" do
      expect {
        post :create, params: {
          unreleased_appearance: {
            time_elapsed: "1",
            notes: "great work",
            channel: "V",
            timecode_in: "timecode_in",
            timecode_out: "timecode_out",
            duration: "duration",
            source_file_name: "source_file_name",
            clip_name: "clip_name",
            description: "description",
            note_category: "other",
          },
          video_id: video,
        }, xhr: true
      }.to change(UnreleasedAppearance, :count).from(0).to(1)
    end

    it "sets unreleased_appearances_data" do
      post :create, params: {
        unreleased_appearance: {
          time_elapsed: "1",
          notes: "great work",
          channel: "V",
          timecode_in: "timecode_in",
          timecode_out: "timecode_out",
          duration: "duration",
          source_file_name: "source_file_name",
          clip_name: "clip_name",
          description: "description",
          note_category: "other",
        },
        video_id: video,
      }, xhr: true

      expect(assigns(:unreleased_appearances_data).unreleased_appearances.first).to have_attributes(
        notes: "great work",
        time_elapsed: "1",
        timecode_in: "timecode_in",
        timecode_out: "timecode_out",
        duration: "duration",
        source_file_name: "source_file_name",
        clip_name: "clip_name",
        description: "description",
        note_category: "other",
      )
    end
  end

  describe "#edit" do
    let(:edl_gateway) { double(:edl_gateway) }

    before :each do
      allow(EdlEventGateway).to receive(:new).and_return(edl_gateway)
      allow(edl_gateway).to receive(:edl_events).and_return(build_list(:edl_event, 1))
    end

    it "sets unreleased_appearance, edl_events_data" do
      unreleased_appearance = create(:unreleased_appearance, video: video)

      get :edit, params: { id: unreleased_appearance }, xhr: true

      expect(EdlEventGateway).to have_received(:new).with(
        instance_of(FilesForRequest),
        "00:00:01:13",
        "00:00:01:13",
      )

      expect(response).to be_successful
      expect(assigns(:unreleased_appearance)).to eq unreleased_appearance
      expect(assigns(:edl_events_data)).to eq({
                                                edl_events: build_list(:edl_event, 1),
                                                edl_attributes: {
                                                  channel: "V",
                                                  timecode_in: "timecode_in",
                                                  timecode_out: "timecode_out",
                                                  duration: "duration",
                                                  source_file_name: "source_file_name",
                                                  clip_name: "clip_name",
                                                  description: "description",
                                                },
                                                info_message: "An EDL event was found. Data is shown below",
                                              })
    end
  end

  describe "#update" do
    let(:edl_gateway) { double(:edl_gateway) }

    before :each do
      allow(EdlEventGateway).to receive(:new).and_return(edl_gateway)
      allow(edl_gateway).to receive(:edl_events).and_return(build_list(:edl_event, 1))
    end

    it "sets unreleased_appearances_data" do
      unreleased_appearance = create(:unreleased_appearance, video: video)

      put :update, params: {
        id: unreleased_appearance,
        unreleased_appearance: {
          notes: "notes",
          time_elapsed: "time_elapsed",
          channel: "V",
          timecode_in: "timecode_in",
          timecode_out: "timecode_out",
          duration: "duration",
          source_file_name: "source_file_name",
          clip_name: "clip_name",
          description: "description",
          note_category: "other",
        }
      }, xhr: true

      expect(response).to be_successful
      expect(assigns(:unreleased_appearances_data).unreleased_appearances.first).to have_attributes(
        notes: "notes",
        time_elapsed: "time_elapsed",
        timecode_in: "timecode_in",
        timecode_out: "timecode_out",
        duration: "duration",
        source_file_name: "source_file_name",
        clip_name: "clip_name",
        description: "description",
        note_category: "other",
      )
    end
  end

  describe "#destroy" do
    let!(:unreleased_appearance) { create(:unreleased_appearance) }

    it "sets unreleased_appearances_data" do
      delete :destroy, params: { id: unreleased_appearance.id }, xhr: true

      expect(assigns(:unreleased_appearances_data).unreleased_appearances).to eq([])
    end

    it "destroys given unreleased_appearance" do
      expect {
        delete :destroy, params: { id: unreleased_appearance.id }, xhr: true
      }.to change(UnreleasedAppearance, :count).from(1).to(0)
    end
  end
end
