require 'spec_helper'

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

  subject { page }

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

  before do
    MusicSession.delete_all
    ActiveMusicSession.delete_all
    Recording.delete_all
    set_login_cookie user
    stub_const("APP_CONFIG", web_config)
  end

  it "loads empty history" do
    nav_profile_history(user)

    find('.no-feed-msg', text: 'This user has no history.')
  end


  # the same feedHelper instance is used to show any user's feed. so we need to make sure bouncing back and forth
  # between feeds is safe
  it "between users" do
    create_session(creator: user)
    formal_leave_by(user)
    user2 = FactoryGirl.create(:user)

    nav_profile_history(user)
    find('.feed-entry.music-session-history-entry')
    visit Nav.feed  # to get around bug where you can't cause a screen to update if it's the same screen, different params
    nav_profile_history(user2)
    should_not have_selector('.feed-entry.music-session-history-entry')
    find('.no-feed-msg', text: 'This user has no history.')
  end

  describe "sessions" do

    before(:each) do
      create_session(creator: user)
      formal_leave_by(user)
    end

    it "should render stats" do
      nav_profile_history(user)

      # 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
      nav_profile_history(user)
      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=\"electric 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 "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
      nav_profile_history(user)

      # 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').should have_content('1')

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

    it "should render details" do
      nav_profile_history(user)

      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

    it "should allow edit and delete" do
      nav_profile_history(user)

      # edit it's name
      find('.edit-recording-dialog').trigger(:click)
      find('h1', text: 'Edit Recording')
      fill_in 'name', with: 'some crazy name'
      find('.save-btn').trigger(:click)
      should_not have_selector('h1', text: 'Edit Recording')
      find('.name-text', text:'some crazy name')

      # now delete it
      find('.edit-recording-dialog').trigger(:click)
      find('h1', text: 'Edit Recording')
      find('.delete-btn').trigger(:click)
      # confirm...
      find('.yes-btn').trigger(:click)
      should_not have_selector('h1', text: 'Edit Recording')
      should_not have_selector('.feed-entry.recording-entry')

      ClaimedRecording.first.discarded.should == true
    end

  end
end
