<commit_before>package org.sagebionetworks.web.client.widget.search;

import java.util.ArrayList;

import org.sagebionetworks.web.client.DisplayUtils;
import org.sagebionetworks.web.client.GWTTimer;
import org.sagebionetworks.web.client.SynapseJavascriptClient;

import com.google.gwt.core.client.ScriptInjector;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.SuggestOracle;
import com.google.inject.Inject;

public class GooglePlacesSuggestOracle extends SuggestOracle {
	public static final int DELAY = 1500;	// milliseconds
	public SuggestOracle.Request request;
	public SuggestOracle.Callback callback;
	public int offset;
	public boolean isLoading = false;
	private GWTTimer timer;
	private SynapseJavascriptClient jsClient;
	private static String key;
	public static final String S3_PREFIX = "https://s3.amazonaws.com/suggest-places.sagebase.org/";
	public static final String GOOGLE_PEOPLE_SUGGESTIONS_URL = S3_PREFIX + "google-place-suggestions.txt";
	String searchTerm;
	ArrayList<PlaceSuggestion> suggestions;
	@Inject
	public GooglePlacesSuggestOracle(
			GWTTimer timer,
			SynapseJavascriptClient jsClient) {
		this.jsClient = jsClient;
		this.timer = timer;
		init();
		timer.configure(() -> {
			getSuggestions(offset);
		});
	}

	private void init() {
		if (key == null) {
			boolean forceAnonymous = true;
			jsClient.doGetString(GOOGLE_PEOPLE_SUGGESTIONS_URL, forceAnonymous, new AsyncCallback<String>() {
				@Override
				public void onFailure(Throwable ex) {
				}
				@Override
				public void onSuccess(String result) {
					key = result;
					ScriptInjector.fromUrl("https://maps.googleapis.com/maps/api/js?key=" + key + "&libraries=places").inject();
				}
			});
		}
	}

	public SuggestOracle.Request getRequest()	{	return request;		}
	public SuggestOracle.Callback getCallback()	{	return callback;	}

	public void getSuggestions(final int offset) {
		if (!isLoading && !request.getQuery().equals(searchTerm)) {
			isLoading = true;
			searchTerm = request.getQuery();
			suggestions = new ArrayList<>();
			DisplayUtils.scrollToTop();
			_getPredictions(this, searchTerm);
		}
	}

	public final static native void _getPredictions(GooglePlacesSuggestOracle oracle, String searchTerm) /*-{
		 var displaySuggestions = function(predictions, status) {<commit_msg>clean up<commit_after>package org.sagebionetworks.web.client.widget.search;

import java.util.ArrayList;

import org.sagebionetworks.web.client.DisplayUtils;
import org.sagebionetworks.web.client.GWTTimer;
import org.sagebionetworks.web.client.SynapseJavascriptClient;

import com.google.gwt.core.client.ScriptInjector;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.SuggestOracle;
import com.google.inject.Inject;

public class GooglePlacesSuggestOracle extends SuggestOracle {
	public static final int DELAY = 1500;	// milliseconds
	public SuggestOracle.Request request;
	public SuggestOracle.Callback callback;
	public int offset;
	public boolean isLoading = false;
	private GWTTimer timer;
	private SynapseJavascriptClient jsClient;
	private static String key;
	public static final String S3_PREFIX = "https://s3.amazonaws.com/suggest-places.sagebase.org/";
	public static final String GOOGLE_PEOPLE_SUGGESTIONS_URL = S3_PREFIX + "google-place-suggestions.txt";
	String searchTerm;
	ArrayList<PlaceSuggestion> suggestions;
	@Inject
	public GooglePlacesSuggestOracle(
			GWTTimer timer,
			SynapseJavascriptClient jsClient) {
		this.jsClient = jsClient;
		this.timer = timer;
		init();
		timer.configure(() -> {
			getSuggestions(offset);
		});
	}

	private void init() {
		if (key == null) {
			boolean forceAnonymous = true;
			jsClient.doGetString(GOOGLE_PEOPLE_SUGGESTIONS_URL, forceAnonymous, new AsyncCallback<String>() {
				@Override
				public void onFailure(Throwable ex) {
				}
				@Override
				public void onSuccess(String result) {
					key = result;
					ScriptInjector.fromUrl("https://maps.googleapis.com/maps/api/js?key=" + key + "&libraries=places").inject();
				}
			});
		}
	}

	public SuggestOracle.Request getRequest()	{	return request;		}
	public SuggestOracle.Callback getCallback()	{	return callback;	}

	public void getSuggestions(final int offset) {
		if (!isLoading && !request.getQuery().equals(searchTerm)) {
			isLoading = true;
			searchTerm = request.getQuery();
			suggestions = new ArrayList<>();
			_getPredictions(this, searchTerm);
		}
	}

	public final static native void _getPredictions(GooglePlacesSuggestOracle oracle, String searchTerm) /*-{
		 var displaySuggestions = function(predictions, status) {
{'fraction_non_alphanumeric': 0.10471657291215924, 'fraction_numerical': 0.003028991778450887, 'mean_word_length': 4.919334186939821, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5532226', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1440, 'n_words': 306}