/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.common.mixin.core.command;

import net.minecraft.command.CommandException;
import net.minecraft.command.CommandHandler;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.ChatComponentTranslation;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.Surrogate;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import org.spongepowered.common.Sponge;
import org.spongepowered.common.command.MinecraftCommandWrapper;
import org.spongepowered.common.interfaces.IMixinWorld;

@Mixin(CommandHandler.class)
public abstract class MixinCommandHandler {

    @Redirect(method = "tryExecute", at = @At(value = "INVOKE", target="Lnet/minecraft/command/ICommand;processCommand(Lnet/minecraft/command/ICommandSender;[Ljava/lang/String;)V"))
    public void onExecuteCommand(ICommand command, ICommandSender sender, String[] args) {
        IMixinWorld world = null;
        try {
            if (sender.getEntityWorld() == null) {
                command.processCommand(sender, args);
                return;
            }

            world = (IMixinWorld) sender.getEntityWorld();
            world.setProcessingCaptureCause(true);
            command.processCommand(sender, args);
            world.handlePostTickCaptures(Cause.of(command, sender));
            world.setProcessingCaptureCause(false);
        } catch (Throwable t) {
            Sponge.getLogger().error("[Command " + command + " issued by " + sender + " caused ]", t);
            if (world != null) {
                world.setProcessingCaptureCause(false);
            }
        }
    }

    @Inject(method = "tryExecute", at = @At(value = "INVOKE", target = "Lnet/minecraft/command/ICommandSender;addChatMessage"
            + "(Lnet/minecraft/util/IChatComponent;)V", ordinal = 2), cancellable = true, locals = LocalCapture.CAPTURE_FAILHARD)
    public void onCommandError(ICommandSender sender, String[] args, ICommand command, String input, CallbackInfoReturnable<Boolean> cir,
                    ChatComponentTranslation comp, Throwable error) {
        MinecraftCommandWrapper.setError(error);
        cir.setReturnValue(false);
    }

    @Surrogate
    public void onCommandError(ICommandSender sender, String[] args, ICommand command, String input, CallbackInfoReturnable<Boolean> cir,
                               Throwable error, ChatComponentTranslation comp) {
        MinecraftCommandWrapper.setError(error);
        cir.setReturnValue(false);
    }
}

@@ -1299,55 +1299,8 @@
 d;%0A%0A
-import net.minecraft.command.CommandException;%0A
 impo
@@ -1672,63 +1672,8 @@
 ct;%0A
-import org.spongepowered.asm.mixin.injection.Redirect;%0A
 impo
@@ -1874,48 +1874,8 @@
 re;%0A
-import org.spongepowered.common.Sponge;%0A
 impo
@@ -2075,13 +2075,11 @@
    @
-Redir
+Inj
 ect(
@@ -2123,131 +2123,12 @@
  = %22
-INVOKE%22, target=%22Lnet/minecraft/command/ICommand;processCommand(Lnet/minecraft/command/ICommandSender;%5BLjava/lang/String;)V
+HEAD
 %22))%0A
@@ -2163,27 +2163,13 @@
 mand
-(ICommand command, 
+Head(
 ICom
@@ -2204,57 +2204,77 @@
 args
-) %7B%0A        IMixinWorld world = null;%0A        try
+, ICommand command, String input, CallbackInfoReturnable%3CBoolean%3E ci)
  %7B%0A 
@@ -2276,28 +2276,24 @@
 ) %7B%0A        
- 
-   
 if (sender.g
@@ -2308,17 +2308,17 @@
 World() 
-=
+!
 = null) 
@@ -2335,100 +2335,19 @@
     
- 
-   command.processCommand(sender, args);%0A                return;%0A            %7D%0A%0A           
+IMixinWorld
  wor
@@ -2453,343 +2453,414 @@
     
+%7D%0A
     
-command.processCommand(sender, args);%0A            world.handlePostTickCaptures(Cause.of(command, sender));%0A            world.setProcessingCaptureCause(false);%0A        %7D catch (Throwable t) %7B%0A            Sponge.getLogger().error(%22%5BCommand %22 + command + %22 issued by %22 + sender + %22 caused %5D%22, t);%0A            if (world != null) %7B%0A    
+%7D%0A%0A    @Inject(method = %22tryExecute%22, at = @At(value = %22RETURN%22))%0A    public void onExecuteCommandReturn(ICommandSender sender, String%5B%5D args, ICommand command, String input, CallbackInfoReturnable%3CBoolean%3E ci) %7B%0A        if (sender.getEntityWorld() != null) %7B%0A            IMixinWorld world = (IMixinWorld) sender.getEntityWorld();%0A            world.handlePostTickCaptures(Cause.of(command, sender));%0A
     
@@ -2911,22 +2911,8 @@
 e);%0A
-            %7D%0A
     

Fix command error messages not being sent to player.