
# methods here all assume you are in /client

NOTIFICATION_PANEL = '[layout-id="panelNotifications"]'

# enters text into the search sidebar
def site_search(text, options = {})
  within('#searchForm') do
    fill_in "search-input",    with: text
  end

  if options[:expand]
    page.driver.execute_script("jQuery('#searchForm').submit()")
    find('h1', text:'search results')
  end
end

# goes to the musician tile, and tries to find a musician
def find_musician(user)
  visit "/client#/musicians"

  timeout = 30

  start = Time.now
  # scroll by 100px until we find a user with the right id
  while page.all('#end-of-musician-list').length == 0
    page.execute_script('jQuery("#musician-filter-results").scrollTo("+=100px", 0, {axis:"y"})')
    found = page.all(".result-list-button-wrapper[data-musician-id='#{user.id}']")
    if found.length == 1
      return found[0]
    elsif found.length > 1
      raise "ambiguous results in musician list"
    end

    if Time.now - start > timeout
      raise "unable to find musician #{user.id} within #{timeout} seconds"
    end
  end

  raise "unable to find musician #{user.id}"
end

def initiate_text_dialog(user)

  # verify that the chat window is grayed out
  site_search(user.first_name, expand: true)

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

# sends a text message in the chat interface.
def send_text_message(msg, options={})
  find('#text-message-dialog') # assert that the dialog is showing already

  within('#text-message-dialog form.text-message-box') do
    fill_in 'new-text-message', with: msg
  end
  find('#text-message-dialog .btn-send-text-message').trigger(:click)
  find('#text-message-dialog .previous-message-text', text: msg) unless options[:should_fail]

  # close the dialog if caller specified close_on_send
  if options[:close_on_send]
    find('#text-message-dialog .btn-close-dialog', text: 'CLOSE').trigger(:click) if options[:close_on_send]
    page.should have_no_selector('#text-message-dialog')
  end

  if options[:should_fail]
    find('#notification').should have_text(options[:should_fail])
  end
end

#  sends a chat message during session
def send_chat_message(msg)
  find("[layout-id=\"panelChat\"] .chat-sender").should be_visible

  within("[layout-id=\"panelChat\"] .chat-sender form.chat-message-form") do
    fill_in 'new-chat-message', with: msg
  end
  find("[layout-id=\"panelChat\"] .chat-sender .btn-send-chat-message").trigger(:click)
end

def open_notifications
  find("#{NOTIFICATION_PANEL} .panel-header").trigger(:click)
end


def hover_intent(element)
  element.hover
  element.hover
end

# forces document.hasFocus() to return false
def document_blur
  page.evaluate_script(%{(function() {
    // save original
    if(!window.documentFocus) { window.documentFocus = window.document.hasFocus; }

    window.document.hasFocus = function() {
      console.log("document.hasFocus() returns false");
      return false;
    }
  })()})
end

def document_focus
  page.evaluate_script(%{(function() {
    // save original
    if(!window.documentFocus) { window.documentFocus = window.document.hasFocus; }

    window.document.hasFocus = function() {
      console.log("document.hasFocus() returns true");
      return true;
    }
  })()})
end

# simulates focus event on window
def window_focus
  page.evaluate_script(%{window.jQuery(window).trigger('focus');})
end

def close_websocket
  page.evaluate_script("window.JK.JamServer.close(true)")
end

