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')
      badge = find("#{NOTIFICATION_PANEL} .badge", text:options[:count])
      badge['class'].include?('highlighted').should == options[:highlighted]

      if options[:action] == :click
        badge.trigger(:click)
        badge = find("#{NOTIFICATION_PANEL} .badge", text:0)
        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: false, count:0, 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: false, count:0
      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
    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)

      in_client(user) do
        sign_in_poltergeist(user)
      end

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

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

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

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