module Rspec::Dunlop::JsHelpers
  def js_set_date(selector, value = '')
    js_set_field selector, value
  end

  def js_set_field(selector, value = '')
    find selector
    page.execute_script("$('#{selector}').val('#{value}').trigger('change')")
  end


  def toggle_optional_input_activator_for(attr, options={})
    js_click "#{options[:selector_prefix]} .optional-input-activator-container.#{attr} .optional-input-activator-toggle"
  end

  def toggle_optional_text_field_for(attr)
    js_click ".optional-text-field-activator-container.#{attr} .optional-input-activator-toggle"
  end

  def set_optional_value(attr, value='', options = {})
    selector = "#{options[:selector_prefix]} .optional-attribute-container.#{attr} input, .optional-attribute-container.#{attr} select, .optional-attribute-container.#{attr} textarea"
    find selector
    js_set_field selector, value
  end

  # Activate the boolean checkbox
  def toggle_optional_boolean_activator_for(attr)
    js_click ".optional-attribute-container.optional-boolean.#{attr} .optional-boolean-activator-toggle"
  end

  # Check the boolean checkbox
  def toggle_optional_boolean_for(attr, options={})
    selector = "#{options[:selector_prefix]} .optional-attribute-container.optional-boolean.#{attr} .optional-boolean-toggle"
    js_click selector
  end

  # Trigger a click event through javascript. Use this to avoid waiting for
  # javascript events issues
  def js_click(selector)
    find(selector) # wait for the page to contain the selector
    page.execute_script("$('#{selector}').click()")

    # wait for triggered ajax requests to be finished
    steps = 0
    while page.evaluate_script('Dunlop.action_in_progress + $.active').to_i > 0 and steps < 25
      sleep 0.1
      steps += 1
    end
  end

  def select_all_workflow_instances
    js_click '.selection-toggle-all'

    steps = 0
    #until all('table.with-selection td.selection .checker').all?{|e| e['class'].include? 'checked'} or steps > 15
    until page.evaluate_script("$('table.with-selection td.selection .checker:not(.checked)').length === 0") or steps > 15
      sleep 0.1
      steps += 1
    end
  end

  def select_workflow_instance(id)
    #page.execute_script %|$("tr[data-record='{\"id\":#{id}}'] .checker").click()|
    page.execute_script %|if( found_element = $('table.with-selection tbody tr').filter(function(i, e){ return $(e).data('record').id == #{id}})[0]){ $(found_element).find('.checker:not(.checked)').click()}|
    sleep 0.1
  end

  def deselect_workflow_instance(id)
    page.execute_script %|if( found_element = $('table.with-selection tbody tr').filter(function(i, e){ return $(e).data('record').id == #{id}})[0]){ $(found_element).find('.checker.checked').click()}|
    sleep 0.1
  end

  def expect_path_bo_be(path)
    steps = 0
    while page.current_path != path and steps < 15
      sleep 0.1
      steps += 1
    end
    expect( page.current_path ).to eq path
  end

  def disable_the_datepicker
    page.driver.execute_script %|$('.datepicker').pickadate('stop');$.fn.pickadate = function(){}| rescue nil
  end
end
