require 'spec_helper'

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

  subject { page }


  let(:user) { FactoryGirl.create(:user, last_jam_locidispid: 1) }
  let(:user2) { FactoryGirl.create(:user, last_jam_locidispid: 2) }


  shared_examples_for :notification_badge do |options|
    it "in correct state" do
      #sign_in_poltergeist(user) unless page.has_selector?('h2', 'musicians')
     sign_in_poltergeist(user) unless page.has_selector?('h2', text: 'musicians')
     badge = find("#{NOTIFICATION_PANEL} .badge", text: options[:count], visible: :all)
     badge['class'].include?('highlighted').should == options[:highlighted]

     if options[:action] == :click
       badge.click
       badge = find("#{NOTIFICATION_PANEL} .badge", text:0, visible: :all)
       badge['class'].include?('highlighted').should == false
     end

    end
  end


  describe "user with no notifications" do

    it_behaves_like :notification_badge, highlighted: false, count:0

    describe "and realtime notifications with sidebar closed" do
      before(:each) do
        sign_in_poltergeist(user)
        document_focus
        user.reload
      end

      
      it_behaves_like :notification_badge, highlighted: false, count:0

      describe "sees notification" do
        before(:each) do
          notification = Notification.send_text_message("text message", user2, user)
          notification.errors.any?.should be false
        end

        it_behaves_like :notification_badge, highlighted: true, count:1, action: :click
      end

      describe "document out of focus" do
        before(:each) do
          document_blur
          notification = Notification.send_text_message("text message", user2, user)
          notification.errors.any?.should be false
        end
        
        it_behaves_like :notification_badge, highlighted: true, count:1, action: :click
      end
      
    end


     describe "and realtime notifications with sidebar open" do
      before(:each) do
        # generate one message so that count = 1 to start
        notification = Notification.send_text_message("text message", user2, user)
        notification.errors.any?.should be false
        sign_in_poltergeist(user)
        document_focus
        open_notifications
        badge = find("#{NOTIFICATION_PANEL} .badge", text:'0') # wait for the opening of the sidebar to bring count to 0
        user.reload
      end

      it_behaves_like :notification_badge, highlighted: false, count:0

      describe "sees notification" do
        before(:each) do
          notification = Notification.send_text_message("text message", user2, user)
          notification.errors.any?.should be false
          find('.notification #btn-reply') # wait for notification to show, so that we know the sidebar had a chance to update
        end

        it_behaves_like :notification_badge, highlighted: true, count:1
      end

      describe "document out of focus" do
        before(:each) do
          document_blur
          notification = Notification.send_text_message("text message 2", user2, user)
          notification.errors.any?.should be false
          find('.notification #btn-reply')
        end

        it_behaves_like :notification_badge, highlighted: true, count:1

        describe "user comes back" do
          before(:each) do
            window_focus

            it_behaves_like :notification_badge, highlighted: false, count:1
          end
        end
      end


      describe "click on notification panel", focus: true do
        before(:each) do
          notification = Notification.send_text_message("text message", user2, user)
          notification.errors.any?.should be false
          find('.notification #btn-reply') # wait for notification to show, so that we know the sidebar had a chance to update
          find('#sidebar-notification-list').click 
        end
        
        it_behaves_like :notification_badge, highlighted: false, count: 0
      end

     end
  end

  describe "user with new notifications" do
    before(:each) do
      # create a notification
      notification = Notification.send_text_message("text message", user2, user)
      notification.errors.any?.should be false
    end

    it_behaves_like :notification_badge, highlighted:true, count:1, action: :click

    describe "user has previously seen notifications" do
      before(:each) do
        user.update_notification_seen_at 'LATEST'
        user.save!
      end

      it_behaves_like :notification_badge, highlighted: false, count:0, action: :click

      describe "user again has unseen notifications" do
        before(:each) do
          # create a notification
          notification = Notification.send_text_message("text message", user2, user)
          notification.errors.any?.should be false
        end

        it_behaves_like :notification_badge, highlighted:true, count:1, action: :click
      end
    end
  end

  describe "delete notification" do

    before(:each) do
      User.delete_all
    end

    it "while notification panel closed" do
      # we should see the count go to 1, but once the notification is accepted, which causes it to delete,
      # we should see the count go back down to 0.

      JamRuby::Score.delete_all
      JamRuby::Score.connection.execute('delete from current_network_scores').check
      JamRuby::Score.createx(1, 'a', 1, 2, 'b', 2, 10)

      sign_in_poltergeist(user)

      in_client(user2) do
        sign_in_poltergeist(user2)
        find_musician(user)
        find(".result-list-button-wrapper[data-musician-id='#{user.id}'] .search-m-friend").click
      end


      badge = find("#{NOTIFICATION_PANEL} .badge", text: '1')
      badge['class'].include?('highlighted').should == true

      find('.notification #btn-accept', text: 'ACCEPT').click

      badge = find("#{NOTIFICATION_PANEL} .badge", text: '0')
      badge['class'].include?('highlighted').should == false
    end
  end

  describe "priority notifications", focus: true do
    let(:user1) { FactoryGirl.create(:user) }
    let(:user2) { FactoryGirl.create(:user) }
    
    before(:each) do
      Notification.destroy_all
      Friendship.destroy_all
      FriendRequest.destroy_all
      JoinRequest.destroy_all
      fast_signin(user2, "/client")
      wait_until_curtain_gone
    end

    describe "friend request" do
      before do
        req1 = FriendRequest.new
        req1.user_id = user1.id
        req1.friend_id = user2.id
        req1.save!
        req1.reload
        Notification.send_friend_request(req1.id, user1.id, user2.id)
        find('#btn-notification-action', text: 'ACCEPT')
        expect(page).to have_selector("#{NOTIFICATION_PANEL} li.notification-entry", count: 1)
        find('.note-text', text: "#{user1.name} has sent you a friend request.")
      end
      it "save_screenshot" do
        save_screenshot("friend_request.png")
      end
      it_behaves_like :notification_badge, highlighted: false, count: 0
    end

    describe "friend request accepted" do
      before do
        Notification.send_friend_request_accepted(user2.id, user1.id)
        expect(page).to have_selector("#{NOTIFICATION_PANEL} li.notification-entry", count: 1)
        find('.note-text', text: "#{user1.name} has accepted your friend request.")
      end
      it "save_screenshot" do
        save_screenshot("friend_request_accepted.png")
      end
      it_behaves_like :notification_badge, highlighted: false, count: 0
    end

    describe "music session join request" do
      before do
        ms = FactoryGirl.create(:music_session, creator: user2)
        join_request = JoinRequest.new
        join_request.music_session = ms
        join_request.user = user1
        text = "#{user1.name} has requested to join your session."
        join_request.text = text
        join_request.save

        Notification.send_join_request(ms, join_request, text)
        expect(page).to have_selector("#{NOTIFICATION_PANEL} li.notification-entry", count: 1)
        find('.note-text', text: text)
      end
      it "save_screenshot" do
        save_screenshot("music_session_join_request.png")
      end
      it_behaves_like :notification_badge, highlighted: false, count: 0
    end

    describe "music session join request approved" do
      before do
        ms = FactoryGirl.create(:music_session, creator: user1)
        join_request = JoinRequest.new
        join_request.music_session = ms
        join_request.user = user2
        text = "#{user1.name} has approved your request to join the session."
        join_request.text = text
        join_request.save

        Notification.send_join_request_approved(ms, join_request)
        expect(page).to have_selector("#{NOTIFICATION_PANEL} li.notification-entry", count: 1)
        find('.note-text', text: text)
      end
      it "save_screenshot" do
        save_screenshot("music_session_join_request_approved.png")
      end
      it_behaves_like :notification_badge, highlighted: false, count: 0
    end

    describe "music session invitation" do
      before do
        user2.online = true
        user2.save!
        ms = FactoryGirl.create(:music_session, creator: user1)
        text = "You have been invited to join a session by #{user1.name}."
        Notification.send_session_invitation(user2, user1, ms.id)
        expect(page).to have_selector("#{NOTIFICATION_PANEL} li.notification-entry", count: 1)
        find('.note-text', text: text)
      end
      it "save_screenshot" do
        save_screenshot("music_session_invitation.png")
      end
      it_behaves_like :notification_badge, highlighted: false, count: 0
    end

  end
end
