package works.chatterbox.chatterbox.commands.impl;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import works.chatterbox.chatterbox.Chatterbox;
import works.chatterbox.chatterbox.channels.Channel;
import works.chatterbox.chatterbox.commands.ReflectCommand;
import works.chatterbox.chatterbox.commands.TabCommand;
import works.chatterbox.chatterbox.wrappers.CPlayer;

import java.util.Collection;
import java.util.List;

@ReflectCommand(
    name = "channel",
    description = "Switches to (and joins, if necessary) a new channel.",
    aliases = {"ch"},
    usage = "/<command> [channel]"
)
public class ChannelCommand extends TabCommand<Chatterbox> {

    private final Joiner spaceJoiner = Joiner.on(' ');

    public ChannelCommand(final Chatterbox instance, final String name) {
        super(instance, name, true, new Short[]{CompletionType.LIST.getShort()});
    }

    @Override
    protected List<String> customList(final CommandSender cs, final Command cmd, final String label, final String[] args, final String arg) {
        final Collection<String> namesAndTags = this.plugin.getAPI().getChannelAPI().getAllChannelNames();
        namesAndTags.addAll(this.plugin.getAPI().getChannelAPI().getAllChannelTags());
        return Lists.newArrayList(namesAndTags);
    }

    @Override
    public boolean runCommand(final CommandSender cs, final Command cmd, final String label, final String[] eargs, final CommandArguments ca) {
        if (eargs.length < 1 || !(cs instanceof Player)) {
            cs.sendMessage(cmd.getDescription());
            return false;
        }
        final Player p = (Player) cs;
        final String channelName = this.spaceJoiner.join(eargs);
        final Channel channel = this.plugin.getAPI().getChannelAPI().getChannel(channelName);
        if (channel == null) {
            // TODO: Localization
            cs.sendMessage(ChatColor.RED + "No such channel.");
            return true;
        }
        final CPlayer cp = this.plugin.getAPI().getPlayerAPI().getCPlayer(p);
        cp.joinChannel(channel);
        cp.setMainChannel(channel);
        cs.sendMessage(ChatColor.BLUE + "Joined channel " + ChatColor.GRAY + channel.getName() + ChatColor.BLUE + ".");
        return true;
    }
}

@@ -405,23 +405,26 @@
 ommands.
-Reflect
+ChannelTab
 Command;
@@ -468,19 +468,23 @@
 ommands.
-Tab
+Reflect
 Command;
@@ -799,16 +799,23 @@
 extends 
+Channel
 TabComma
@@ -820,20 +820,8 @@
 mand
-%3CChatterbox%3E
  %7B%0A%0A
@@ -1000,41 +1000,10 @@
 ort%5B
+0
 %5D
-%7BCompletionType.LIST.getShort()%7D
 );%0A 
@@ -1949,42 +1949,8 @@
 ) %7B%0A
-            // TODO: Localization%0A
     
@@ -1992,26 +1992,62 @@
 D + 
-%22No such channel.%22
+this.plugin.getLanguage().getString(%22NO_SUCH_CHANNEL%22)
 );%0A 
@@ -2271,27 +2271,70 @@
 E + 
-%22Joined channel %22 +
+this.plugin.getLanguage().getFormattedString(%22JOINED_CHANNEL%22,
  Cha
@@ -2385,14 +2385,9 @@
 BLUE
- + %22.%22
+)
 );%0A 

Use ChannelTabCommand