module ClientHelper

  # is this the native client (or browser emulating native client with CTRL+SHIFT+0), or browser?
  def is_native_client?
    # is this the native client or browser?
    user_agent = request.env["HTTP_USER_AGENT"]
    is_native_client = !user_agent.blank? && user_agent.downcase.include?("jamkazam")

    # allow override of the client type if configured to so, and if we find the override cookie in place
    if Rails.application.config.allow_force_native_client
      unless cookies[:act_as_native_client].nil?
        is_native_client = (cookies[:act_as_native_client] == "true") ? true : false
      end
    end

    is_native_client
  end

  def gon_setup

    gon.root_url = root_url
    gon.client_path = client_path
    # use gon to pass variables into javascript
    if Rails.env == "development"
      # if in development mode, we assume you are running websocket-gateway
      # on the same host as you hit your server.
      if request.headers['X-Forwarded-Proto'] == 'https'
        gon.websocket_gateway_uri = "wss://" + request.host + ":6443/websocket";
      else
        gon.websocket_gateway_uri = "ws://" + request.host + ":6767/websocket";
      end

    else
      # but in any other mode, just use config
      if request.headers['X-Forwarded-Proto'] == 'https'
        gon.websocket_gateway_uri = Rails.application.config.websocket_gateway_uri_ssl
      else
        gon.websocket_gateway_uri = Rails.application.config.websocket_gateway_uri
      end


    end

    if Rails.env == "development"
      # if in development mode, we assume you are running websocket-gateway
      # on the same host as you hit your server.
      if request.headers['X-Forwarded-Proto'] == 'https'
        gon.websocket_gateway_trusted_uri = "wss://" + request.host + ":6444/websocket";
      else
        gon.websocket_gateway_trusted_uri = "ws://" + request.host + ":6768/websocket";
      end
    else
      # but in any other mode, just use config
      if request.headers['X-Forwarded-Proto'] == 'https'
        gon.websocket_gateway_trusted_uri = Rails.application.config.websocket_gateway_trusted_uri_ssl
      else
        gon.websocket_gateway_trusted_uri = Rails.application.config.websocket_gateway_trusted_uri
      end
    end

    gon.check_for_client_updates = Rails.application.config.check_for_client_updates
    gon.fp_apikey = Rails.application.config.filepicker_rails.api_key
    gon.fp_upload_dir = Rails.application.config.filepicker_upload_dir
    gon.allow_force_native_client = Rails.application.config.allow_force_native_client
    gon.ftue_io_wait_time = Rails.application.config.ftue_io_wait_time
    gon.ftue_packet_rate_treshold = Rails.application.config.ftue_packet_rate_treshold
    gon.ftue_network_test_duration = Rails.application.config.ftue_network_test_duration
    gon.ftue_network_test_max_clients = Rails.application.config.ftue_network_test_max_clients
    gon.ftue_maximum_gear_latency = Rails.application.config.ftue_maximum_gear_latency
    gon.musician_search_meta = MusicianSearch.search_filter_meta
    gon.band_search_meta = BandSearch.search_filter_meta
    gon.session_stat_thresholds = Rails.application.config.session_stat_thresholds
    gon.midi_enabled = Rails.application.config.midi_enabled
    gon.chat_blast = Rails.application.config.chat_blast
    gon.jamclass_enabled = Rails.application.config.jamclass_enabled

    # is this the native client or browser?
    @nativeClient = is_native_client?

    # let javascript have access to the server's opinion if this is a native client
    gon.isNativeClient = @nativeClient

    gon.use_cached_session_scores = Rails.application.config.use_cached_session_scores
    gon.allow_both_find_algos = Rails.application.config.allow_both_find_algos
    gon.stripe_publishable_key = Rails.application.config.stripe[:publishable_key]
  end
end
