require 'spec_helper'

describe RecordingHelper, type: :helper do

  before(:each) do
    @user = FactoryGirl.create(:user)
    @connection = FactoryGirl.create(:connection, :user => @user)
    @instrument = FactoryGirl.create(:instrument, :description => 'a great instrument')
    @track = FactoryGirl.create(:track, :connection => @connection, :instrument => @instrument)
    @music_session = FactoryGirl.create(:active_music_session, :creator => @user, :musician_access => true)
    # @music_session.connections << @connection
    @music_session.save
    @connection.join_the_session(@music_session, true, nil, @user, 10)
    @recording = Recording.start(@music_session, @user)
    @recording.stop
    @recording.reload
    @genre = FactoryGirl.create(:genre)
    @recording.claim(@user, "name", "description", @genre, true)
    @recording.reload
    @claimed_recording = @recording.claimed_recordings.first
  end

  describe "facebook_image_for_claimed_recording" do
    it "with band with no photo url" do
      @recording.band = FactoryGirl.create(:band)
      @recording.save!(:validate => false)
      @claimed_recording.reload
      result = helper.facebook_image_for_claimed_recording(@claimed_recording)
      result.include?('/assets/web/logo-256.png').should be true
    end

    it "with band with photo url" do
      @recording.band = FactoryGirl.create(:band, large_photo_url: 'abc.png')
      @recording.save!(:validate => false)
      @claimed_recording.reload
      result = helper.facebook_image_for_claimed_recording(@claimed_recording)
      result.include?(@claimed_recording.recording.band.large_photo_url).should be true
    end

    it "with user with no photo url" do
      result = helper.facebook_image_for_claimed_recording(@claimed_recording)
      result.include?("/assets/web/logo-256.png").should be true
    end

    it "with user with photo url" do
      @claimed_recording.user.large_photo_url = 'abc.png'
      @claimed_recording.user.save!
      @claimed_recording.save!
      @claimed_recording.reload
      result = helper.facebook_image_for_claimed_recording(@claimed_recording)
      result.include?("/assets/web/logo-256.png").should be true
    end

    it "with sharer with no photo url" do
      sharer = FactoryGirl.create(:user)
      result = helper.facebook_image_for_claimed_recording(@claimed_recording)
      result.include?("/assets/web/logo-256.png").should be true
    end

    it "with sharer with photo url" do
      sharer = FactoryGirl.create(:user, large_photo_url: 'abc.png')
      result = helper.facebook_image_for_claimed_recording(@claimed_recording)
      result.include?("/assets/web/logo-256.png").should be true
    end
  end

  describe "title_for_claimed_recording" do
    it "with band" do
      @recording.band = FactoryGirl.create(:band)
      @recording.save!(:validate => false)
      @claimed_recording.reload
      result = helper.title_for_claimed_recording(@claimed_recording)
      result.start_with?("RECORDING").should be true
      result.end_with?(@claimed_recording.recording.band.name).should be true
    end

    it "with user" do
      result = helper.title_for_claimed_recording(@claimed_recording)
      result.start_with?("RECORDING").should be true
      result.end_with?(@claimed_recording.user.name).should be true
    end

    # regression: https://jamkazam.atlassian.net/browse/VRFS-2468
    it "correct number of others when multiple tracks for 2 users" do
      second_user = FactoryGirl.create(:user)
      track1 = FactoryGirl.create(:recorded_track, user: second_user, recording: @recording)
      track2 = FactoryGirl.create(:recorded_track, user: second_user, recording: @recording)
      result = helper.title_for_claimed_recording(@claimed_recording)
      result.include?("& #{second_user.name}").should be true

    end

    # regression: https://jamkazam.atlassian.net/browse/VRFS-2468
    it "correct number of others when multiple tracks for 3 users" do
      second_user = FactoryGirl.create(:user)
      track1 = FactoryGirl.create(:recorded_track, user: second_user, recording: @recording)
      track2 = FactoryGirl.create(:recorded_track, user: second_user, recording: @recording)
      third_user = FactoryGirl.create(:user)
      track1 = FactoryGirl.create(:recorded_track, user: third_user, recording: @recording)
      track2 = FactoryGirl.create(:recorded_track, user: third_user, recording: @recording)
      result = helper.title_for_claimed_recording(@claimed_recording)
      result.include?("& 2 OTHERS").should be true
    end

  end

  describe "additional_member_count" do
    it "no unique users" do
       helper.additional_member_count([], @user).should == ""
    end

    it "has 2 users" do
      user2 = FactoryGirl.create(:user)
      helper.additional_member_count([@user, user2], @user).should == " & #{user2.name}"
    end

    it "has 3 users" do
      user2 = FactoryGirl.create(:user)
      user3 = FactoryGirl.create(:user)
      helper.additional_member_count([@user, user2, user3], @user).should == " & 2 OTHERS"
    end
  end
end
