package com.gmail.nossr50.chat.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import com.gmail.nossr50.chat.ChatMode;
import com.gmail.nossr50.datatypes.McMMOPlayer;
import com.gmail.nossr50.util.Users;

public abstract class ChatCommand implements CommandExecutor {
    protected McMMOPlayer mcMMOPlayer;
    protected ChatMode chatMode;

    public ChatCommand (ChatMode chatMode) {
        this.chatMode = chatMode;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        switch (args.length) {
        case 0:
            if (!(sender instanceof Player)) {
                return false;
            }

            mcMMOPlayer = Users.getPlayer((Player) sender);

            if (chatMode.isEnabled(mcMMOPlayer)) {
                disableChatMode(sender);
                return true;
            }

            enableChatMode(sender);
            return true;

        default:
            if (args.length == 1) {
                if (!(sender instanceof Player)) {
                    return false;
                }

                mcMMOPlayer = Users.getPlayer((Player) sender);

                if (args[0].equalsIgnoreCase("on")) {
                    enableChatMode(sender);
                    return true;
                }

                if (args[0].equalsIgnoreCase("off")) {
                    disableChatMode(sender);
                    return true;
                }
            }

            handleChatSending(sender, args);
            return true;
        }
    }

    private void enableChatMode(CommandSender sender) {
        chatMode.enable(mcMMOPlayer);
        sender.sendMessage(chatMode.getEnabledMessage());
    }

    private void disableChatMode(CommandSender sender) {
        chatMode.disable(mcMMOPlayer);
        sender.sendMessage(chatMode.getDisabledMessage());
    }

    protected String buildChatMessage(String[] args, int index) {
        StringBuilder builder = new StringBuilder();
        builder.append(args[index]);

        for (int i = index + 1; i < args.length; i++) {
            builder.append(" ");
            builder.append(args[i]);
        }

        return builder.toString();
    }

    protected abstract void handleChatSending(CommandSender sender, String[] args);
}

@@ -988,43 +988,35 @@
     
+%7D%0D%0A
     
-return true;%0D%0A            %7D%0D%0A%0D%0A
+        else %7B%0D%0A    
     
@@ -1040,32 +1040,49 @@
 tMode(sender);%0D%0A
+            %7D%0D%0A%0D%0A
             retu
@@ -1144,24 +1144,83 @@
 th == 1) %7B%0D%0A
+                if (args%5B0%5D.equalsIgnoreCase(%22on%22)) %7B%0D%0A    
             
@@ -1271,32 +1271,36 @@
                 
+  
+  
 return false;%0D%0A 
@@ -1290,32 +1290,36 @@
  return false;%0D%0A
+    
                 
@@ -1343,55 +1343,88 @@
     
-mcMMOPlayer = Users.getPlayer((Player) sender);
+    enableChatMode(sender);%0D%0A                    return true;%0D%0A                %7D
 %0D%0A%0D%0A
@@ -1462,33 +1462,34 @@
 alsIgnoreCase(%22o
-n
+ff
 %22)) %7B%0D%0A         
@@ -1499,39 +1499,50 @@
         
-enableChatMode(sender);
+if (!(sender instanceof Player)) %7B
 %0D%0A      
@@ -1551,34 +1551,39 @@
             
+ 
+   
 return 
-tru
+fals
 e;%0D%0A        
@@ -1594,67 +1594,15 @@
     
-%7D%0D%0A%0D%0A                if (args%5B0%5D.equalsIgnoreCase(%22off%22)) %7B
+    %7D%0D%0A
 %0D%0A  

Fix party & admin chat not working right from console.