/*
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.command;

import server.Emote;
import server.Player;
import server.SyntaxException;

public class EmoteCommand extends Command {

    private enum TextType {
        audio {
            
            @Override
            public String get(Emote spec) {
                return spec.getAudio();
            }

            @Override
            public void set(Emote spec, String text) {
                spec.setAudio(text);
            }
            
            @Override
            public String toString() {
                return "Audio";
            }
            
        },
        visual {
            
            @Override
            public String get(Emote spec) {
                return spec.getVisual();
            }

            @Override
            public void set(Emote spec, String text) {
                spec.setVisual(text);
            }
            
            @Override
            public String toString() {
                return "Visual";
            }

        },
        both {
            
            @Override
            public String get(Emote spec) {
                return spec.getBoth();
            }

            @Override
            public void set(Emote spec, String text) {
                spec.setBoth(text);
            }
            
            @Override
            public String toString() {
                return "Both";
            }

        };
        
        public abstract String get(Emote spec);
        
        public abstract void set(Emote spec, String text);
        
    }
    
    public EmoteCommand() {
        super(CommandCategory.ADMIN, "Maintain the emote system.");
    }

    @Override
    public void run(Player player) throws SyntaxException {
        String subcmd = CommandProcessor.nextToken();
        if (subcmd == null) {
            throw new SyntaxException();
        }
        else if ("add".equals(subcmd)) {
            String name = CommandProcessor.nextToken();
            if (name == null || CommandProcessor.nextToken() != null) {
                throw new SyntaxException();
            }
            if (Emote.add(name) == null) {
                player.sendText(true, "That emote already exists.");
            }
            else {
                player.sendText(true, "Emote created.");
            }
        }
        else if ("delete".equals(subcmd)) {
            String name = CommandProcessor.nextToken();
            if (name == null || CommandProcessor.nextToken() != null) {
                throw new SyntaxException();
            }
            if (Emote.remove(name) == null) {
                player.sendText(true, "That emote does not exist.");
            }
            else {
                player.sendText(true, "Emote deleted.");
            }
        }
        else if ("list".equals(subcmd)) {
            if (CommandProcessor.nextToken() != null) {
                throw new SyntaxException();
            }
            StringBuffer sb = new StringBuffer();
            for (String name : Emote.list()) {
                if (sb.length() > 0) {
                    sb.append(", ");
                }
                sb.append(name);
            }
            sb.insert(0, "The following emotes are available: @10F");
            player.sendText(true, sb.toString());
        }
        else {
            Emote spec = Emote.get(subcmd);
            if (spec == null) {
                throw new SyntaxException();
            }
            subcmd = CommandProcessor.nextToken();
            if (subcmd == null) {
                throw new SyntaxException();
            }
            TextType type = null;
            try {
                type = TextType.valueOf(subcmd);
            }
            catch (IllegalArgumentException ex) {
                /* ignore */
            }
            if (type != null) {
                String text = CommandProcessor.getRemaining();
                if (text.length() == 0) {
                    player.sendText(true, type + ": " + (type.get(spec) == null ? "-" : type.get(spec)));
                }
                else if ("-".equals(text)) {
                    type.set(spec, null);
                    player.sendText(true, type + " cleared.");
                }
                else {
                    type.set(spec, text);
                    player.sendText(true, type + " is now set to " + text);
                }
            }
            else if ("info".equals(subcmd)) {
                if (CommandProcessor.nextToken() != null) {
                    throw new SyntaxException();
                }
                player.sendText(true, "Audio: " + (spec.getAudio() == null ? "-" : spec.getAudio()));
                player.sendText(false, "Visual: " + (spec.getVisual() == null ? "-" : spec.getVisual()));
                player.sendText(false, "Both: " + (spec.getBoth() == null ? "-" : spec.getBoth()));
                player.sendText(false, "Target Type: " + spec.getTargetType());
            }
            else if ("target".equals(subcmd)) {
                String token = CommandProcessor.nextToken();
                if (CommandProcessor.nextToken() != null) {
                    throw new SyntaxException();
                }
                if (token == null) {
                    player.sendText(true, "Target type is " + spec.getTargetType());
                }
                else {
                    Emote.TargetType targetType;
                    try {
                        targetType = Emote.TargetType.valueOf(token.toUpperCase());
                    }
                    catch (IllegalArgumentException ex) {
                        throw new SyntaxException();
                    }
                    spec.setTargetType(targetType);
                    player.sendText(true, "Target type is now " + targetType);
                }
            }
            else if ("add".equals(subcmd)) {
                String modifier = CommandProcessor.nextToken();
                if (modifier == null) {
                    throw new SyntaxException();
                }
                String modifierCommand;
                String as = CommandProcessor.nextToken();
                if (as != null) {
                    if ("as".equals(as)) {
                        modifierCommand = CommandProcessor.nextToken();
                        if (modifierCommand == null) {
                            throw new SyntaxException();
                        }
                    }
                    else {
                        throw new SyntaxException();
                    }
                }
                else {
                    modifierCommand = modifier;
                }
                if (CommandProcessor.nextToken() != null) {
                    throw new SyntaxException();
                }
                spec.addModifier(modifierCommand, modifier);
                player.sendText(true, "Added modifier " + modifier.toUpperCase() + " as " + modifierCommand.toUpperCase());
            }
            else if ("delete".equals(subcmd)) {
                String modifier = CommandProcessor.nextToken();
                if (modifier == null || CommandProcessor.nextToken() != null) {
                    throw new SyntaxException();
                }
                if (spec.removeModifier(modifier)) {
                    player.sendText(true, "Removed modifier " + modifier.toUpperCase());
                }
                else {
                    player.sendText(true, "That modifier does not exist.");
                }
            }
            else {
                throw new SyntaxException();
            }
        }
    }

    @Override
    public String[] getSyntax(Player player) {
        return new String[] {
                "LIST",
                "ADD <emote>",
                "DELETE <emote>",
                "<emote> ADD <modifier> [AS <modifier>]",
                "<emote> DELETE <modifier>",
                "<emote> INFO",
                "<emote> TARGET [ALLOWED|DISALLOWED|REQUIRED]",
                "<emote> AUDIO [tokenstring|-]",
                "<emote> VISUAL [tokenstring|-]",
                "<emote> BOTH [tokenstring|-]",
                };
    }

}