/*
 * LapisCommons
 * Copyright (c) 2014, LapisDev <https://github.com/LapisDev>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package blue.lapis.common.command.impl;

import blue.lapis.common.LapisCommonsPlugin;
import blue.lapis.common.command.token.InvalidTokenException;
import blue.lapis.common.command.token.TokenParser;
import com.google.common.collect.Lists;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.entity.Player;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.List;

/**
 * Converts a Player name into an online {@link Player}.
 */
public class PlayerTokenParser implements TokenParser<Player> {

    @Override
    @Nonnull
    public Player parse(@Nonnull final CommandSource source, @Nonnull final String token) {
        Collection<Player> players = LapisCommonsPlugin.getGame().getOnlinePlayers();
        String matchString = token.toLowerCase();

        //TODO: strip off leading p: ?

        //attempt resolving full names
        for (Player p : players) {
            if (p.getName().toLowerCase().equals(matchString)) {
                return p;
            }
        }

        //attempt partial names
        for (Player p : players) {
            if (p.getName().toLowerCase().startsWith(matchString)) {
                return p;
            }
        }

        throw new InvalidTokenException(token, Player.class);
    }

    @Override
    @Nonnull
    public List<String> suggest(@Nonnull final CommandSource source, @Nonnull final String partial) {
        List<String> results = Lists.newArrayList();
        Collection<Player> players = LapisCommonsPlugin.getGame().getOnlinePlayers();
        String matchString = partial.toLowerCase();

        for (Player p : players) {
            if (p.getName().toLowerCase().startsWith(matchString)) {
                results.add(p.getName());
            }
        }

        return results;
    }
}

@@ -1914,58 +1914,8 @@
 s();
-%0A        String matchString = token.toLowerCase();
 %0A%0A  
@@ -1922,16 +1922,17 @@
       //
+ 
 TODO: st
@@ -1955,32 +1955,33 @@
 p: ?%0A%0A        //
+ 
 attempt resolvin
@@ -2060,44 +2060,32 @@
 e().
-toLowerCase().equals(matchString)) %7B
+equalsIgnoreCase(token))
 %0A   
@@ -2103,38 +2103,24 @@
   return p;%0A
-            %7D%0A
         %7D%0A%0A 
@@ -2128,16 +2128,17 @@
       //
+ 
 attempt 
@@ -2206,60 +2206,49 @@
 if (
-p.getName().toLowerCase().startsWith(matchString)) %7B
+startsWithIgnoreCase(p.getName(), token))
 %0A   
@@ -2270,30 +2270,16 @@
 turn p;%0A
-            %7D%0A
         
@@ -2622,164 +2622,103 @@
 ();%0A
-        String matchString = partial.toLowerCase();%0A%0A        for (Player p : players) %7B%0A            if (p.getName().toLowerCase().startsWith(matchString)) %7B
+%0A        for (Player p : players) %7B%0A            if (startsWithIgnoreCase(p.getName(), partial))
 %0A   
@@ -2768,47 +2768,181 @@
     
+%7D%0A%0A        return results;%0A
     %7D%0A
+%0A
     
-    %7D%0A%0A        return results
+private static boolean startsWithIgnoreCase(String s, String start) %7B%0A        return s.regionMatches(true, 0, start, 0, start.length())
 ;%0A  

Improve player token parser performance.