require 'spec_helper'

describe "Feed", :js => true, :type => :feature, :capybara_feature => true do

  let (:user)  { FactoryGirl.create(:user_two_instruments) }

  before(:each) do
    MusicSession.delete_all
    MusicSessionUserHistory.delete_all
    Recording.delete_all
    IcecastMount.delete_all
  end


  describe "regressions" do

    describe "mount" do

      it "should render when has mount" do
        # regression for VRFS-1987
        ams = FactoryGirl.create(:active_music_session)
        FactoryGirl.create(:icecast_mount, music_session_id: ams.id)

        fast_signin user, "/client#/feed"
        find('#feedScreen')
        find(".feed-entry.music-session-history-entry[data-music-session='#{ams.id}']")
      end
    end
  end
  describe "sessions" do

    before(:each) do
      @ams = FactoryGirl.create(:active_music_session, creator:user)
      #create_session(creator: user)
      #formal_leave_by(user)
    end

    # it "should render avatar" do
    #   it " and link to profile" do
    #   end

    #   it " and render artist hover bubble" do
    #   end
    # end

    # it "should render description" do
    #   it " and link to session landing" do
    #   end

    #   it " and render session hover bubble" do
    #   end
    # end

    # it "should render artist name" do
    #   it " and link to profile" do
    #   end

    #   it " and render artist hover bubble"
    # end


    it "should render stats" do
      fast_signin user, "/client#/feed"

      # initial stats
      find('span.plays').should have_content('0')
      find('span.comments').should have_content('0')
      find('span.likes').should have_content('0')

      # Comments
      find('a.btn-comment').trigger(:click)
      comment = 'this sounds great'
      fill_in "txtComment", with: comment
      find('#btn-add-comment').trigger(:click)
      find('div.comment-text', text: comment)
      find('#dialog-close-button', '[layout-id="comment-dialog"]').trigger(:click)

      find('span.comments').should have_content('1')

      # Likes
      find('a.btn-like').trigger(:click)
      find('span.likes').should have_content('1')
    end

    it "should render details" do
      # for the 'total_instruments' query to work in the rabl, you have to have session_removed_at to be set on both
      @ams.music_session.session_removed_at = Time.now
      @ams.music_session.save!
      MusicSessionUserHistory.count.should eq(1)
      msuh = MusicSessionUserHistory.first
      msuh.session_removed_at = Time.now
      msuh.instruments = 'drums'
      msuh.save!

      fast_signin user, "/client#/feed"

      find('.feed-details a.details').trigger(:click)

      # confirm user avatar exists
      find("a.avatar-tiny[user-id=\"#{user.id}\"][hoveraction=\"musician\"] img")

      # confirm user name exists
      find("a.musician-name[user-id=\"#{user.id}\"][hoveraction=\"musician\"]", text: user.name)

      # confirm instrument icons exist
      find("img[instrument-id=\"drums\"]")

      # confirm hover bubbles show
      find("a.avatar-tiny[user-id=\"#{user.id}\"][hoveraction=\"musician\"]").hover_intent

      # confirm navigate to user profile page
      find(".avatar-tiny[user-id=\"#{user.id}\"][hoveraction=\"musician\"]").trigger(:click)
      find("#user-profile h2[id=username]", text: user.name)
    end

    # it "should render play widget" do
    # end

    it "should allow switch from fan_access=true to fan_access=false" do
      ams = FactoryGirl.create(:active_music_session)
      FactoryGirl.create(:icecast_mount, music_session_id: ams.id)

      ms = ams.music_session
      ms.fan_access = false
      ms.save!

      fast_signin user, "/client#/feed"
      find('#feedScreen')
      find(".feed-entry.music-session-history-entry[data-music-session='#{ams.id}']")
    end

  end

  describe "recordings" do

    before(:each) do
      FactoryGirl.create(:claimed_recording, user: user, name: 'my recording', description: "my recording description")
      MusicSession.delete_all
    end

    # it "should render avatar" do
    #   it " and link to profile" do
    #   end

    #   it " and render artist hover bubble" do
    #   end
    # end

    # it "should render description" do
    #   it " and link to recording landing" do
    #   end

    #   it " and render recording hover bubble" do
    #   end
    # end

    # it "should render artist name" do
    #   it " and link to profile" do
    #   end

    #   it " and render artist hover bubble"
    # end

    it "should render stats" do
      fast_signin user, "/client#/feed"

      find('h1', text: 'feed')

      # initial stats
      find('span.plays').should have_content('0')
      find('span.comments').should have_content('0')
      find('span.likes').should have_content('0')

      # ensure Share icon exists
      find('a.btn-share')

      # Comments
      find('a.btn-comment').trigger(:click)
      comment = 'this sounds great'
      fill_in "txtComment", with: comment
      find('#btn-add-comment').trigger(:click)
      find('div.comment-text', text: comment)
      find('#dialog-close-button', '[layout-id="comment-dialog"]').trigger(:click)

      find('span.comments', text: '1')

      # Likes
      find('a.btn-like').trigger(:click)
      find('span.likes', text: '1')
    end

    it "should render details" do
      fast_signin user, "/client#/feed"

      find('.feed-details a.details').trigger(:click)

      # confirm user avatar exists
      find("a.avatar-tiny[user-id=\"#{user.id}\"][hoveraction=\"musician\"] img")

      # confirm user name exists
      find("a.musician-name[user-id=\"#{user.id}\"][hoveraction=\"musician\"]", text: user.name)

      # confirm instrument icons exist
      find("img[instrument-id=\"acoustic guitar\"]")


      # confirm hover bubbles show
      find("a.avatar-tiny[user-id=\"#{user.id}\"][hoveraction=\"musician\"]").hover_intent

      # confirm navigate to user profile page
      find(".avatar-tiny[user-id=\"#{user.id}\"][hoveraction=\"musician\"]").trigger(:click)
      find("#user-profile h2[id=username]", text: user.name)
    end

  end

  describe "session participants behavior (VRFS-2193)" do
    let(:creator)   { FactoryGirl.create(:user) }
    let(:finder_1)  { FactoryGirl.create(:user) }
    let(:finder_2)  { FactoryGirl.create(:user) }

    specify "after session ends all participants are shown in Feed" do
      creator, description = create_join_session creator, finder_1
      # feed shows user, finder_1
      formal_leave_by(finder_1)
      # feed shows user
      join_session(finder_2, description: description)
      # feed shows user, finder_2
      formal_leave_by(finder_2)
      formal_leave_by(creator)

      in_client(creator) { verify_feed_shows_users creator, finder_1, finder_2 }
    end

    specify "during session only current participants are shown in Feed" do
      creator, description = create_join_session creator, finder_1
      formal_leave_by(finder_1)
      join_session(finder_2, description: description)

      in_client(finder_1) { verify_feed_shows_users finder_2, creator }
    end
  end
end
