package org.mschaeffner.metmoji.server.context;

import java.util.Collection;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import org.mschaeffner.metmoji.server.owm.domain.City;

public class CityProvider {

	private static final int MIN_PREFIX_FOR_COMPLETION = 2;

	private final TreeSet<City> cities;

	public CityProvider(Collection<City> cities) {
		this.cities = new TreeSet<City>(new Comparator<City>() {
			@Override
			public int compare(City city1, City city2) {
				return city1.getName().toLowerCase().compareTo(city2.getName().toLowerCase());
			}
		});

		if (cities != null) {
			this.cities.addAll(cities);
		}
	}

	public List<City> findCompletions(String prefix) {
		if (prefix == null) {
			// TODO throw an exception
			return null;
		}

		final String cleanPrefix = prefix.trim().toLowerCase();
		if (cleanPrefix.length() < MIN_PREFIX_FOR_COMPLETION) {
			// TODO throw an exception
			return null;
		}

		final List<City> result = new LinkedList<>();
		final City prefixCity = new City(0l, cleanPrefix, null, null);
		final SortedSet<City> tailSet = cities.tailSet(prefixCity);
		for (City city : tailSet) {
			if (city.getName().toLowerCase().startsWith(cleanPrefix)) {
				result.add(city);
			} else {
				break;
			}
		}

		return result;
	}

}

@@ -350,16 +350,75 @@
 N = 2;%0A%0A
+%09private static final int MAX_NUMBER_OF_COMPLETIONS = 20;%0A%0A
 %09private
@@ -1357,16 +1357,87 @@
 (city);%0A
+%09%09%09%09if(result.size() %3E= MAX_NUMBER_OF_COMPLETIONS) %7B%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A
 %09%09%09%7D els

set max number of suggested completions for city name to 20