package org.aaron1011.whowas.impl.bukkit.command;

import com.google.common.base.Optional;
import org.aaron1011.whowas.core.namehistory.PlayerNameHistory;
import org.aaron1011.whowas.core.namehistory.PlayerNameHistoryFetcher;
import org.aaron1011.whowas.core.uuid.PlayerUUIDFetcher;
import org.aaron1011.whowas.core.namehistory.TimestampedName;
import org.aaron1011.whowas.impl.bukkit.BukkitPlugin;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.UUID;

public class WhoWasCommand implements CommandExecutor {

    private final JavaPlugin plugin;

    public WhoWasCommand(JavaPlugin plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (args.length < 1) {
            return false;
        }

        Player player;
        if (this.plugin.getConfig().getBoolean("exactLookup", true)) {
            player = sender.getServer().getPlayerExact(args[0]);
        } else {
            player = sender.getServer().getPlayer(args[0]);
        }

        UUID uuid;
        if (player == null) {
            try {
                Optional<UUID> uuidOpt = PlayerUUIDFetcher.getUUID(args[0]);
                if (uuidOpt.isPresent()) {
                    uuid = uuidOpt.get();
                } else {
                    sender.sendMessage(String.format("Unable to fetch uuid for username %s. Do they exist?", args[0]));
                    return true;
                }
            } catch (Exception e) {
                sender.sendMessage(String.format("Error occurred when fetching UUID for %s: " , args[0]) + e.getMessage());
                return true;
            }
        } else {
            uuid = player.getUniqueId();
        }

        PlayerNameHistory history;
        try {
            Optional<PlayerNameHistory> historyOpt = PlayerNameHistoryFetcher.getPlayerNameHistory(uuid);
            if (historyOpt.isPresent()) {
                history = historyOpt.get();
            } else {
                sender.sendMessage(String.format("Error fetcher player name history for UUID %s. Does it exist?", uuid));
                return true;
            }
        } catch (Exception e) {
            sender.sendMessage("Error fetching player name history: " + e.getMessage());
            return true;
        }


        sender.sendMessage(ChatColor.BLUE + "Name history:");
        for (TimestampedName name: history.getNames()) {
            StringBuilder builder = new StringBuilder(name.getName() + ": ");

            if (name.getChangedToAt().isPresent()) {
                builder.append(ChatColor.GREEN + "Changed to at " + name.getChangedToAt().get().toString());
            } else {
                builder.append(ChatColor.GOLD + "In use");
            }

            sender.sendMessage(builder.toString());
        }
        return true;
    }
}

@@ -1833,17 +1833,16 @@
 or %25s: %22
- 
 , args%5B0
@@ -2559,17 +2559,16 @@
      %7D%0A%0A
-%0A
         
@@ -2617,24 +2617,236 @@
 history:%22);%0A
+        if (history.getNames().size() == 1) %7B%0A            TimestampedName name = history.getNames().get(0);%0A            sender.sendMessage(name.getName() + %22: %22 + ChatColor.GOLD + %22In use%22);%0A        %7D else %7B%0A    
         for 
@@ -2866,16 +2866,17 @@
 ame name
+ 
 : histor
@@ -2887,24 +2887,28 @@
 tNames()) %7B%0A
+    
             
@@ -2974,16 +2974,20 @@
 %22: %22);%0A%0A
+    
         
@@ -3039,32 +3039,36 @@
                 
+    
 builder.append(C
@@ -3148,32 +3148,36 @@
 ));%0A            
+    
 %7D else %7B%0A       
@@ -3161,32 +3161,36 @@
        %7D else %7B%0A
+    
                 
@@ -3214,37 +3214,41 @@
 atColor.GOLD + %22
-In us
+First nam
 e%22);%0A           
@@ -3240,35 +3240,43 @@
 %22);%0A            
-%7D%0A%0A
+    %7D%0A%0A    
             send
@@ -3311,34 +3311,42 @@
 ing());%0A        
+  
-%7D%0A
+  %7D%0A    
         return t
@@ -3346,20 +3346,30 @@
 eturn true;%0A
+        %7D%0A
     %7D%0A%7D%0A

Handle earliest name correctly