package com.forgeessentials.commands.player;

import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraftforge.permissions.PermissionsManager.RegisteredPermValue;

import com.forgeessentials.api.APIRegistry;
import com.forgeessentials.api.UserIdent;
import com.forgeessentials.commands.util.FEcmdModuleCommands;
import com.forgeessentials.core.commands.CommandFeSettings;
import com.forgeessentials.core.misc.Translator;
import com.forgeessentials.util.OutputHandler;
import com.forgeessentials.util.PlayerInfo;
import com.forgeessentials.util.ServerUtil;

public class CommandAFK extends FEcmdModuleCommands
{

    public static final String PERM = "fe.commands.afk";

    public static final String PERM_ANNOUNCE = PERM + ".announce";

    public static final String PERM_WARMUP = PERM + ".warmup";

    public static final String PERM_AUTOTIME = PERM + ".autotime";

    public static final String PERM_AUTOKICK = PERM + ".autokick";

    public CommandAFK()
    {
        CommandFeSettings.addAlias("afktime", PERM_AUTOTIME);
    }

    @Override
    public String getCommandName()
    {
        return "afk";
    }

    @Override
    public String getCommandUsage(ICommandSender sender)
    {
        return "/afk: Mark yourself as away.";
    }

    @Override
    public RegisteredPermValue getDefaultPermission()
    {
        return RegisteredPermValue.TRUE;
    }

    @Override
    public void registerExtraPermissions()
    {
        APIRegistry.perms.registerPermission(PERM_ANNOUNCE, RegisteredPermValue.TRUE);
        APIRegistry.perms.registerPermissionProperty(PERM_WARMUP, "10", "Time a player needs to wait before he can go afk with /afk");
        APIRegistry.perms.registerPermissionProperty(PERM_AUTOTIME, "480", "Auto afk time in seconds. Set to 0 to disable.");
        APIRegistry.perms.registerPermission(PERM_AUTOKICK, RegisteredPermValue.FALSE, "Automatically kick a player, when he is AFK");
    }

    @Override
    public boolean canConsoleUseCommand()
    {
        return false;
    }

    @Override
    public void processCommandPlayer(EntityPlayerMP sender, String[] args)
    {
        UserIdent ident = UserIdent.get(sender);

        // expected syntax: /afk timeout <group|player> <timeout>
        // to set custom afk timeout for yourself, replace <player> with your own username
        if (args[0].equalsIgnoreCase("timeout"))
        {
            UserIdent applyTo = UserIdent.get(args[1], true);
            if (applyTo != null)
            {
                APIRegistry.perms.setPlayerPermissionProperty(applyTo, PERM_AUTOTIME, args[2]);
            }
            else
            {
                APIRegistry.perms.setGroupPermissionProperty(args[1], PERM_AUTOTIME, args[2]);
            }
        }
        // expected syntax: /afk timeout <group|player> [true|false}
        else if (args[0].equalsIgnoreCase("autokick"))
        {
            UserIdent applyTo = UserIdent.get(args[1], true);
            if (applyTo != null)
            {
                APIRegistry.perms.setPlayerPermissionProperty(applyTo, PERM_AUTOKICK, args[2]);
            }
            else
            {
                APIRegistry.perms.setGroupPermissionProperty(args[1], PERM_AUTOKICK, args[2]);
            }
        }
        else
        {
            int autoTime = ServerUtil.parseIntDefault(ident.getPermissionProperty(CommandAFK.PERM_AUTOTIME), 60 * 2);
            int warmup = ServerUtil.parseIntDefault(ident.getPermissionProperty(PERM_WARMUP), 0);
            PlayerInfo.get(sender).setActive(autoTime * 1000 - warmup * 1000);
            OutputHandler.chatConfirmation(sender, Translator.format("Stand still for %d seconds.", warmup));
        }
    }

}

@@ -2213,16 +2213,60 @@
 nder);%0A%0A
+        if (args.length %3E= 1)%0A        %7B%0A    
         
@@ -2323,16 +2323,20 @@
 imeout%3E%0A
+    
         
@@ -2418,32 +2418,36 @@
 sername%0A        
+  
+  
 if (args%5B0%5D.equa
@@ -2471,34 +2471,42 @@
 eout%22))%0A        
-%7B%0A
+    %7B%0A    
             User
@@ -2555,32 +2555,36 @@
 e);%0A            
+    
 if (applyTo != n
@@ -2592,34 +2592,42 @@
 ll)%0A            
-%7B%0A
+    %7B%0A    
                 
@@ -2710,34 +2710,41 @@
 %5D);%0A            
-%7D%0A
+    %7D%0A   
             else
@@ -2731,32 +2731,33 @@
 %0A               
+ 
 else%0A           
@@ -2749,34 +2749,42 @@
 lse%0A            
-%7B%0A
+    %7B%0A    
                 
@@ -2870,34 +2870,40 @@
             
-%7D%0A
+    %7D%0A  
         
 %7D%0A        //
@@ -2890,26 +2890,31 @@
         
-%7D%0A
+  %7D%0A   
         
 // expec
@@ -2897,32 +2897,33 @@
    %7D%0A           
+ 
 // expected synt
@@ -2971,24 +2971,28 @@
 se%7D%0A        
+    
 else if (arg
@@ -3026,32 +3026,36 @@
 kick%22))%0A        
+    
 %7B%0A            Us
@@ -3044,32 +3044,36 @@
   %7B%0A            
+ 
+   
 UserIdent applyT
@@ -3110,32 +3110,36 @@
 e);%0A            
+    
 if (applyTo != n
@@ -3147,34 +3147,42 @@
 ll)%0A            
+   
+ 
 %7B%0A
+    
                 
@@ -3265,34 +3265,42 @@
 %5D);%0A            
-%7D%0A
+    %7D%0A    
             else
@@ -3304,34 +3304,41 @@
 lse%0A            
-%7B%0A
+    %7B%0A   
                 
@@ -3329,32 +3329,33 @@
                 
+ 
 APIRegistry.perm
@@ -3409,32 +3409,50 @@
 KICK, args%5B2%5D);%0A
+                %7D%0A
             %7D%0A  

Fix #1600