/*
Copyright (C) 2005 David Green <green@couchpotato.net>
All Rights Reserved.

This file is part of Aelfengard.

Aelfengard is proprietary software. You may not redistribute it without
prior written permission from the copyright holder.
*/
package server;

import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import common.Direction;

import server.command.Command;
import server.command.CommandCategory;

public class CommandSet {

    private final Map<String,Command> commands;
    private EnumMap<CommandCategory,Set<String>> categoryMap;
    
    public CommandSet(Map<String,Command> commands) {
        this.commands = commands;
    }
    
    public Command get(String token) {
        return commands.get(token);
    }
    
    public Set<CommandCategory> getCommandCategories() {
        buildCategoryMap();
        return categoryMap.keySet();
    }
    
    public void showCommands(Player p, CommandCategory category) {
        buildCategoryMap();
        Collection<String> cmds = categoryMap.get(category);
        StringBuffer sb = new StringBuffer();
        if (category == CommandCategory.MOVEMENT) {
            // reorder so that directional commands are first, in proper order
            Set<String> temp = new LinkedHashSet<String>(cmds);
            cmds = new ArrayList<String>();
            for (Direction dir : Direction.values()) {
                boolean full = temp.remove(dir.getFullName());
                boolean abbr = temp.remove(dir.getAbbreviation());
                if (full || abbr) {
                    if (!full) {
                        cmds.add(dir.getAbbreviation());
                    }
                    else if (!abbr) {
                        cmds.add(dir.getFullName());
                    }
                    else {
                        cmds.add(dir.getFullName() + " @0ZZor@ZZZ " + dir.getAbbreviation());
                    }
                }
            }
            cmds.addAll(temp);
        }
        for (String name : cmds) {
            if (sb.length() > 0) {
                sb.append(", ");
            }
            sb.append(name);
        }
        p.sendText(true, "You have the following @10F" + category + "@ZZZ commands:");
        p.sendText(true, "@10F" + sb);
        p.sendText(true, "Typing @10FHELP @30F<command>@ZZZ will give you more information on a specific command.");
    }
    
    private void buildCategoryMap() {
        if (categoryMap != null) {
            return; // already built
        }
        categoryMap = new EnumMap<CommandCategory,Set<String>>(CommandCategory.class);
        for (Map.Entry<String,Command> entry : commands.entrySet()) {
            Command command = entry.getValue();
            if (command.isHidden()) {
                continue;
            }
            String name = entry.getKey();
            CommandCategory category = command.getCategory();
            Set<String> set = categoryMap.get(category);
            if (set == null) {
                set = new TreeSet<String>();
                categoryMap.put(category, set);
            }
            set.add(name);
        }
    }
    
}