require 'spec_helper'

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

  before(:each) do
    User.delete_all # we delete all users due to the use of find_musician() helper method, which scrolls through all users
    MaxMindManager.create_phony_database
    @user1 = FactoryGirl.create(:user, last_jam_locidispid: 1)
    @user2 = FactoryGirl.create(:user, last_jam_locidispid: 2, first_name: 'bone_crusher')
    sign_in_poltergeist(@user1)

  end

  # what are all the ways to launch the dialog?
  describe "launches" do

    it "on hover bubble" do
      site_search(@user2.first_name, expand: true)

      find("#search-results a[user-id=\"#{@user2.id}\"][hoveraction=\"musician\"]", text: @user2.name).hover_intent
      find('#musician-hover #btnMessage').trigger(:click)
      find('h1', text: 'conversation with ' + @user2.name)
    end

    it "on find musician in musician's tile" do
      JamRuby::Score.delete_all
      JamRuby::Score.connection.execute('delete from current_network_scores').check
      JamRuby::Score.createx(locidispid_from_ip('127.0.0.1'), 'a', ip_address_to_int('127.0.0.1'),
                             locidispid_from_ip('10.0.0.1'), 'a', ip_address_to_int('10.0.0.1'),
                             10)

      musician = find_musician(@user2)
      find(".result-list-button-wrapper[data-musician-id='#{@user2.id}'] .search-m-message").trigger(:click)
      find('h1', text: 'conversation with ' + @user2.name)
    end

    it "on musician profile" do
      visit "/client#/profile/#{@user2.id}"
      find('#btn-message-user').trigger(:click)
      find('h1', text: 'conversation with ' + @user2.name)
    end

    it "on reply of notification in sidebar" do

      @user1.reload
      # create a notification
      notification = Notification.send_text_message("bibbity bobbity boo", @user2, @user1)
      notification.errors.any?.should be false

      open_notifications

      # find the notification and click REPLY
      find("[layout-id='panelNotifications'] [notification-id='#{notification.id}'] .button-orange", text:'REPLY').trigger(:click)
      find('h1', text: 'conversation with ' + @user2.name)
    end

    it "on hit reply in message notification" do

      in_client(@user1) do
        sign_in_poltergeist(@user1)
      end

      in_client(@user2) do
        sign_in_poltergeist(@user2)

        site_search(@user1.name, expand: true)

        find("#search-results a[user-id=\"#{@user1.id}\"][hoveraction=\"musician\"]", text: @user1.name).hover_intent
        find('#musician-hover #btnMessage').trigger(:click)
        find('h1', text: 'conversation with ' + @user1.name)

        send_text_message("Hello to user id #{@user1.id}", close_on_send: true)
      end

      in_client(@user1) do
        find('#notification #btn-reply').trigger(:click)
        find('h1', text: 'conversation with ' + @user2.name)
      end
    end

    it "can load directly into chat session from url" do
      visit "/"
      should_be_at_root
      visit "/client#/home/text-message/d1=#{@user2.id}"
      find('h1', text: 'conversation with ' + @user2.name)
    end
  end

  describe "chat dialog behavior" do

    it "send a message to someone" do
      in_client(@user1) do
        sign_in_poltergeist(@user1)
      end

      in_client(@user2) do
        sign_in_poltergeist(@user2)

        site_search(@user1.name, expand: true)

        find("#search-results a[user-id=\"#{@user1.id}\"][hoveraction=\"musician\"]", text: @user1.name).hover_intent
        find('#musician-hover #btnMessage').trigger(:click)
        find('h1', text: 'conversation with ' + @user1.name)

        send_text_message("Oh hai to user id #{@user1.id}")
      end

      in_client(@user1) do
        find('#notification #btn-reply').trigger(:click)
        find('h1', text: 'conversation with ' + @user2.name)
        find('.previous-message-text', text: "Oh hai to user id #{@user1.id}")
        send_text_message('hey there yourself')
      end

      in_client(@user2) do
        find('.previous-message-text', text: "hey there yourself")
        send_text_message('ok bye', close_on_send: true)
      end

      in_client(@user1) do
        find('.previous-message-text', text: "ok bye")
        send_text_message('bye now', close_on_send: true)
      end
    end

    it "shows error with a notify" do
      visit '/'
      should_be_at_root
      visit "/client#/home/text-message/d1=#{@user2.id}"
      find('h1', text: 'conversation with ' + @user2.name)
      send_text_message('ass', should_fail:'profanity')

    end
  end
end
