package net.minecraft.server.commands;

import com.mojang.brigadier.Message;
import com.google.common.collect.ImmutableList;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import java.util.Iterator;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.LivingEntity;
import javax.annotation.Nullable;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.entity.Entity;
import java.util.Collection;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import net.minecraft.commands.arguments.MobEffectArgument;
import com.mojang.brigadier.arguments.ArgumentType;
import net.minecraft.commands.arguments.EntityArgument;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import net.minecraft.commands.Commands;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.commands.CommandSourceStack;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;

public class EffectCommands {
    private static final SimpleCommandExceptionType ERROR_GIVE_FAILED;
    private static final SimpleCommandExceptionType ERROR_CLEAR_EVERYTHING_FAILED;
    private static final SimpleCommandExceptionType ERROR_CLEAR_SPECIFIC_FAILED;
    
    public static void register(final CommandDispatcher<CommandSourceStack> commandDispatcher) {
        commandDispatcher.register((LiteralArgumentBuilder)((LiteralArgumentBuilder)((LiteralArgumentBuilder)Commands.literal("effect").requires(cq -> cq.hasPermission(2))).then(((LiteralArgumentBuilder)Commands.literal("clear").executes(commandContext -> clearEffects((CommandSourceStack)commandContext.getSource(), ImmutableList.<Entity>of(((CommandSourceStack)commandContext.getSource()).getEntityOrException())))).then(((RequiredArgumentBuilder)Commands.argument("targets", (com.mojang.brigadier.arguments.ArgumentType<Object>)EntityArgument.entities()).executes(commandContext -> clearEffects((CommandSourceStack)commandContext.getSource(), EntityArgument.getEntities((CommandContext<CommandSourceStack>)commandContext, "targets")))).then(Commands.argument("effect", (com.mojang.brigadier.arguments.ArgumentType<Object>)MobEffectArgument.effect()).executes(commandContext -> clearEffect((CommandSourceStack)commandContext.getSource(), EntityArgument.getEntities((CommandContext<CommandSourceStack>)commandContext, "targets"), MobEffectArgument.getEffect((CommandContext<CommandSourceStack>)commandContext, "effect"))))))).then(Commands.literal("give").then(Commands.argument("targets", (com.mojang.brigadier.arguments.ArgumentType<Object>)EntityArgument.entities()).then(((RequiredArgumentBuilder)Commands.argument("effect", (com.mojang.brigadier.arguments.ArgumentType<Object>)MobEffectArgument.effect()).executes(commandContext -> giveEffect((CommandSourceStack)commandContext.getSource(), EntityArgument.getEntities((CommandContext<CommandSourceStack>)commandContext, "targets"), MobEffectArgument.getEffect((CommandContext<CommandSourceStack>)commandContext, "effect"), null, 0, true))).then(((RequiredArgumentBuilder)Commands.argument("seconds", (com.mojang.brigadier.arguments.ArgumentType<Object>)IntegerArgumentType.integer(1, 1000000)).executes(commandContext -> giveEffect((CommandSourceStack)commandContext.getSource(), EntityArgument.getEntities((CommandContext<CommandSourceStack>)commandContext, "targets"), MobEffectArgument.getEffect((CommandContext<CommandSourceStack>)commandContext, "effect"), IntegerArgumentType.getInteger(commandContext, "seconds"), 0, true))).then(((RequiredArgumentBuilder)Commands.argument("amplifier", (com.mojang.brigadier.arguments.ArgumentType<Object>)IntegerArgumentType.integer(0, 255)).executes(commandContext -> giveEffect((CommandSourceStack)commandContext.getSource(), EntityArgument.getEntities((CommandContext<CommandSourceStack>)commandContext, "targets"), MobEffectArgument.getEffect((CommandContext<CommandSourceStack>)commandContext, "effect"), IntegerArgumentType.getInteger(commandContext, "seconds"), IntegerArgumentType.getInteger(commandContext, "amplifier"), true))).then(Commands.argument("hideParticles", (com.mojang.brigadier.arguments.ArgumentType<Object>)BoolArgumentType.bool()).executes(commandContext -> giveEffect((CommandSourceStack)commandContext.getSource(), EntityArgument.getEntities((CommandContext<CommandSourceStack>)commandContext, "targets"), MobEffectArgument.getEffect((CommandContext<CommandSourceStack>)commandContext, "effect"), IntegerArgumentType.getInteger(commandContext, "seconds"), IntegerArgumentType.getInteger(commandContext, "amplifier"), !BoolArgumentType.getBool(commandContext, "hideParticles"))))))))));
    }
    
    private static int giveEffect(final CommandSourceStack cq, final Collection<? extends Entity> collection, final MobEffect aki, @Nullable final Integer integer, final int integer, final boolean boolean6) throws CommandSyntaxException {
        int integer2 = 0;
        int integer3;
        if (integer != null) {
            if (aki.isInstantenous()) {
                integer3 = integer;
            }
            else {
                integer3 = integer * 20;
            }
        }
        else if (aki.isInstantenous()) {
            integer3 = 1;
        }
        else {
            integer3 = 600;
        }
        for (final Entity akq10 : collection) {
            if (akq10 instanceof LivingEntity) {
                final MobEffectInstance akk11 = new MobEffectInstance(aki, integer3, integer, false, boolean6);
                if (!((LivingEntity)akq10).addEffect(akk11)) {
                    continue;
                }
                ++integer2;
            }
        }
        if (integer2 == 0) {
            throw EffectCommands.ERROR_GIVE_FAILED.create();
        }
        if (collection.size() == 1) {
            cq.sendSuccess(new TranslatableComponent("commands.effect.give.success.single", new Object[] { aki.getDisplayName(), ((Entity)collection.iterator().next()).getDisplayName(), integer3 / 20 }), true);
        }
        else {
            cq.sendSuccess(new TranslatableComponent("commands.effect.give.success.multiple", new Object[] { aki.getDisplayName(), collection.size(), integer3 / 20 }), true);
        }
        return integer2;
    }
    
    private static int clearEffects(final CommandSourceStack cq, final Collection<? extends Entity> collection) throws CommandSyntaxException {
        int integer3 = 0;
        for (final Entity akq5 : collection) {
            if (akq5 instanceof LivingEntity && ((LivingEntity)akq5).removeAllEffects()) {
                ++integer3;
            }
        }
        if (integer3 == 0) {
            throw EffectCommands.ERROR_CLEAR_EVERYTHING_FAILED.create();
        }
        if (collection.size() == 1) {
            cq.sendSuccess(new TranslatableComponent("commands.effect.clear.everything.success.single", new Object[] { ((Entity)collection.iterator().next()).getDisplayName() }), true);
        }
        else {
            cq.sendSuccess(new TranslatableComponent("commands.effect.clear.everything.success.multiple", new Object[] { collection.size() }), true);
        }
        return integer3;
    }
    
    private static int clearEffect(final CommandSourceStack cq, final Collection<? extends Entity> collection, final MobEffect aki) throws CommandSyntaxException {
        int integer4 = 0;
        for (final Entity akq6 : collection) {
            if (akq6 instanceof LivingEntity && ((LivingEntity)akq6).removeEffect(aki)) {
                ++integer4;
            }
        }
        if (integer4 == 0) {
            throw EffectCommands.ERROR_CLEAR_SPECIFIC_FAILED.create();
        }
        if (collection.size() == 1) {
            cq.sendSuccess(new TranslatableComponent("commands.effect.clear.specific.success.single", new Object[] { aki.getDisplayName(), ((Entity)collection.iterator().next()).getDisplayName() }), true);
        }
        else {
            cq.sendSuccess(new TranslatableComponent("commands.effect.clear.specific.success.multiple", new Object[] { aki.getDisplayName(), collection.size() }), true);
        }
        return integer4;
    }
    
    static {
        ERROR_GIVE_FAILED = new SimpleCommandExceptionType((Message)new TranslatableComponent("commands.effect.give.failed", new Object[0]));
        ERROR_CLEAR_EVERYTHING_FAILED = new SimpleCommandExceptionType((Message)new TranslatableComponent("commands.effect.clear.everything.failed", new Object[0]));
        ERROR_CLEAR_SPECIFIC_FAILED = new SimpleCommandExceptionType((Message)new TranslatableComponent("commands.effect.clear.specific.failed", new Object[0]));
    }
}
