require 'spec_helper'

describe "Chat Message", :js => true, :type => :feature, :capybara_feature => true do
  before(:all) do
    Capybara.default_max_wait_time = 15
  end

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

  before(:each) do
    UserMailer.deliveries.clear
    ActiveMusicSession.delete_all
    ChatMessage.delete_all

    FactoryGirl.create(:friendship, :user => user1, :friend => user2)
  end

  # what are all the ways to be in a session?
  describe "join session" do

    it "on try to send chat before joining session" do
      description = "Try to send chat message before joining session!"
      create_session(creator: user1, description: description)

      in_client(user2) do
        sign_in_poltergeist(user1)

        find("[layout-id=\"panelChat\"] .panel-header").trigger(:click)
        find(".chat-status", text: 'Chat is available when in session.')
      end
    end

    it "on join a session" do
      description = "Find chat panel expanded when join session"
      create_session(creator: user1, description: description)

      wait_for_ajax

      join_session(user2, description: description)

      find(".chatcontents").should be_visible
      find("[layout-id=\"panelChat\"] .chat-sender").should be_visible
    end
  end

  describe "sidebar session chat behavior" do

    it "send a message" do
      description = "Find chat panel expanded when join session"
      create_session(creator: user1, description: description)

      join_session(user2, description: description)

      in_client(user1) do
        send_chat_message("Hey, I am #{user1.id}")
        find('.chat-message-text', text: "Hey, I am #{user1.id}")
      end

      in_client(user2) do
        find('.chat-message-text', text: "Hey, I am #{user1.id}")
        send_chat_message("Received, I am #{user2.id}")
      end

      in_client(user1) do
        find('.chat-message-text', text: "Received, I am #{user2.id}")
      end
    end

    it "shows error with a notify" do
      description = "Find chat panel expanded when join session"
      create_session(creator: user1, description: description)

      join_session(user2, description: description)

      in_client(user2) do
        chat_max = 256
        chat_msg = 'a' * (chat_max + 1)

        send_chat_message(chat_msg)

        find('#notification').should have_text("Unable to Send Chat Message")
      end
    end

    it "shows badge if not on chat panel" do
      description = "Find chat panel expanded when join session"
      create_session(creator: user1, description: description)

      join_session(user2, description: description)

      in_client(user1) do
        find("[layout-id=\"panelFriends\"] .panel-header").trigger(:click)
      end

      in_client(user2) do
        send_chat_message("Detect badge information")
      end

      in_client(user1) do
        find('#sidebar-chat-count.badge.highlighted', text: "1")
        find("[layout-id=\"panelChat\"] .panel-header").trigger(:click)
        find('.chat-message-text', text: "Detect badge information")
      end
    end
  end
end
