require 'spec_helper'

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

  subject { page }

  before(:all) do
    Capybara.default_max_wait_time = 15
  end

  let(:fan) { FactoryGirl.create(:fan) }
  let(:user) { FactoryGirl.create(:user) }
  let(:finder) { FactoryGirl.create(:user) }
  let(:band_musician) { FactoryGirl.create(:band_musician) }

  before(:each) do
    UserMailer.deliveries.clear
  end

  def navigate_band_setup login=user
    sign_in_poltergeist(login) if current_url == 'about:blank'
    find('div.homecard.profile').trigger(:click)
    find('#bands-link').trigger(:click)
    find('#band-setup-link').trigger(:click)
    expect(page).to have_selector('#band-setup-title')
  end

  def fill_out_band_setup_form(band, biography, country="United States", region="Texas", city="Austin", params={})
    navigate_band_setup unless URI.parse(current_url).fragment == '/band/setup/new'
    params['band-name'] ||= band || "Default band name"
    params['band-biography'] ||= biography || "Default band biography"

    within('#band-setup-form') do
      params.each do |field, value|
        fill_in field, with: value
      end
      # Move to experience pane:
      #first('#band-genres input[type="checkbox"]').trigger(:click)
    end

    jk_select(country, '#band-country')
    jk_select(region, '#band-region')
    jk_select(city, '#band-city')

    sleep 1 # work around race condition

    find('#btn-band-setup-next').trigger(:click)
    find('h2', text: 'set up band: musical experience')
  end

  def complete_band_setup_form(band, biography, country="United States", region="Texas", city="Austin", params={})
    navigate_to_friend_page(band, biography, country, region, city, params)

    # Save
    find('#btn-band-setup-next').trigger(:click)
    sleep(1)
  end

  def navigate_to_friend_page(band, biography, country="United States", region="Texas", city="Austin", params={})
    fill_out_band_setup_form(band, biography, country, region, city, params)

    find(:css, "#african").set(true)
    find('#btn-band-setup-next').trigger(:click)
    find('h2', text: 'set up band: current interests')

    find('#btn-band-setup-next').trigger(:click)
    find('h2', text: 'set up band: online presence & performance samples')

    find('#btn-band-setup-next').trigger(:click)
    find('h2', text: 'set up band: invite members')
  end

  context "band profile - new band setup" do
    it "displays 'Set up your band' link to user" do
      sign_in_poltergeist user
      view_profile_of user
      find('#bands-link').trigger(:click)
      expect(page).to have_selector('#band-setup-link')
    end

    it "does not display band setup link when viewed by other user" do
      in_client(fan) do
        sign_in_poltergeist fan
        view_profile_of user
        find('#bands-link').trigger(:click)

        expect(page).to_not have_selector('#band-setup-link')
      end
    end

    it "indicates required fields and user may eventually complete" do
      navigate_band_setup
      find('#btn-band-setup-next').trigger(:click)
      expect(page).to have_selector('#band-setup .band-name .error-text li', text: "can't be blank")
      expect(page).to have_selector('#band-setup .band-biography .error-text li', text: "can't be blank")

      complete_band_setup_form("Test Band name", "Test Band biography")


      expect(page).to have_selector('#band-profile-name', text: "Test Band name")
      expect(page).to have_selector('#band-profile-biography', text: "Test Band biography")
    end

    it "limits genres to 3" do
      pending "Move this to experience pane"
      #expect(page).to have_selector('#band-setup .band-genres .error-text li', text: "At least 1 genre is required.")

      genres = Genre.limit(4)
      navigate_band_setup
      within('#band-setup-form') do
        fill_in 'band-name',    with: "whatever"
        fill_in 'band-biography', with: "a good story"
        genres.each do |genre|
          find("#band-genres input[value='#{genre.id}']").trigger(:click)
        end
      end
      find('#btn-band-setup-next').trigger(:click)
      expect(page).to have_selector('#band-setup .band-genres .error-text li', text: "No more than 3 genres are allowed.")
    end

    it "handles max-length field input" do
      # pending "update this after VRFS-1610 is resolved"
      max = {
          name: 1024,
          bio: 4000,
          website: 4000
      }
      navigate_band_setup
      band_name = 'a'*(max[:name] + 1)
      band_bio = 'b'*(max[:bio] + 1)
      band_website = 'c'*(max[:website] + 1)
      within('#band-setup-form') do
        fill_in 'band-name',    with: band_name
        fill_in 'band-biography', with: band_bio
        #all('#band-genres input[type="checkbox"]').first.trigger(:click)
      end
      sleep 1
      find('#btn-band-setup-next').trigger(:click)
      #expect(page).to have_selector('#band-biography .error-text li', text: "is too long (maximum is 4000 characters)")
    end

    it "handles special characters in text fields" do
      navigate_band_setup
      band_name = garbage(3) + ' ' + garbage(50)
      band_bio = garbage(500)
      band_website = garbage(2000)
      complete_band_setup_form(band_name, band_bio)#, 'band-website' => band_website)

      expect(page).to have_selector('#band-profile-name', text: Sanitize.fragment(band_name))
      expect(page).to have_selector('#band-profile-biography', text: Sanitize.fragment(band_bio))
    end

    it "another user receives invite notification during Band Setup"
  end


  context "about view" do
    it "displays the band's information to another user" do
      #missing photo, genres chosen, number of followers, recordings, sessions, actions: follow button
      band_attributes = {
          name: 'Radical D00dz',
          biography: 'Just a bunch of EXTREME dudes who ROCK professionally!!',
          country: 'US',
          state: 'TX',
          city: 'Lubbock'
      }

      some_band = FactoryGirl.create(:band, band_attributes)
      sign_in_poltergeist fan
      view_band_profile_of some_band


      expect(page).to have_content 'Radical D00dz'
      expect(page).to have_content 'Just a bunch of EXTREME dudes who ROCK professionally!!'
      expect(page).to have_content 'Lubbock, TX'
    end

    it "allows a user to follow the band"
  end

  context "members view" do
    it "photo and name links to the musician's profile page" do
      sign_in_poltergeist fan
      visit "/client#/bandProfile/#{band_musician.bands.first.id}"
      find('#band-profile-members-link').trigger(:click)
      expect(page).to have_selector('.result-name', text: band_musician.name)
    end

    it "displays photo, name, location for member" do
      sign_in_poltergeist fan
      visit "/client#/bandProfile/#{band_musician.bands.first.id}"
      find('#band-profile-members-link').trigger(:click)
      within "div.band-profile-members" do
        find(".avatar-small img")
        find(".result-name", text: band_musician.name)
        find(".result-location", text: "#{band_musician.city}, #{band_musician.state}")
      end
    end

    it "displays any pending band invitations when viewed by current band member" do
      friend = user
      friendship = FactoryGirl.create(:friendship, :user_id=>band_musician.id, :friend_id=>friend.id)

      sign_in_poltergeist(band_musician)
      #visit "/client#/band/setup/#{band_musician.bands.first.id}"
      band_name = "Just The Two Of Us"
      band_bio = "Good, good friends"
      band_website = "http://www.sounds.com/thetwoguysfrom2009.html"

      navigate_to_friend_page(band_name, band_bio)#, 'band-website' => band_website)

      #invite somebody using the picker
      find('#btn-choose-friends-band').trigger(:click)
      find("tr[user-id='#{friend.id}']").trigger(:click)
      expect(page).to have_selector("tr.selected[user-id='#{friend.id}']")
      find('#btn-save-friends').trigger(:click)
      find('#btn-band-setup-next').trigger(:click)
      sleep 1 # ensure the transaction commits..

      find('#band-profile-members-link').trigger(:click)
      within('#band-profile-members') do
        expect(page).to have_selector('h2', text: 'Pending Band Invitations')
        expect(page).to have_css("h2 ~ div[user-id='#{friend.id}']")
      end
    end
  end

  context "history view" do
    it "shows public info"
    it "does not show private info to non-band user"
    it "shows private info to band user"
  end

  context "social view" do
    it "displays musicians and fans who follow band" do
      band_attributes = {
          name: 'Popular Club',
          biography: 'We love the fans',
          city: 'Denton',
          state: 'TX'
      }

      some_band = FactoryGirl.create(:band_with_follower, band_attributes)

      in_client(finder) do
        sign_in_poltergeist finder
        view_band_profile_of some_band
        expect(page).to have_selector('#band-profile-follower-stats', text: "1 Follower")
        find('#band-profile-social-link').trigger(:click)
        expect(page).to have_selector('div.band-profile-block-name')
      end

    end
  end

  context "band profile - editing" do
    it "about page shows the current band's info when 'Edit Profile' is clicked" do
      sign_in_poltergeist band_musician
      band = band_musician.bands.first
      view_band_profile_of band
      expect(page).to have_selector('#btn-edit-band-profile')

      find('#btn-edit-band-profile').trigger(:click)
      find('h2', text: 'set up band: basics')

      expect(page).to have_content band.name
      expect(page).to have_content band.biography
    end

    it "non-member cannot Edit Profile" do
      band = band_musician.bands.first
      in_client(fan) do
        sign_in_poltergeist fan
        view_band_profile_of band
        expect(page).to_not have_selector('#btn-edit-band-profile')
      end
    end

    it "members page shows 'Edit Members' button and user can remove member"
    it "non-member cannot Edit Members"
  end

  it "band shows up in sidebar search result" do
    pending "search Javascript is not working for me"
    band_attributes = {
        name: 'Needle In The Hay',
        biography: 'Good luck!!',
        city: 'San Diego',
        state: 'CA'
    }

    some_band = FactoryGirl.create(:band, band_attributes)
    sign_in_poltergeist fan
    sidebar_search_for band_attributes[:name], "Band"
    expect(page).to contain(band_attributes[:name])

    # band_attributes.each_value { |v| expect(page).to have_content v }
  end
end


