package net.minecraft.server.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import java.util.Collection;
import java.util.List;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.commands.arguments.SwingAnimationArgument;
import net.minecraft.commands.arguments.TimeArgument;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.SwingAnimationType;
import net.minecraft.world.item.component.SwingAnimation;

public class SwingCommand {
	private static final SimpleCommandExceptionType ERROR_NO_LIVING_ENTITY = new SimpleCommandExceptionType(
		Component.translatable("commands.swing.failed.notliving")
	);
	private static final CommandResponseTracker.Messages<LivingEntity> RESPONSE_SWING = CommandResponseTracker.messages(
		ERROR_NO_LIVING_ENTITY,
		(entity, var1) -> Component.translatable("commands.swing.success.single", entity.getDisplayName()),
		(entityCount, var1) -> Component.translatable("commands.swing.success.multiple", entityCount)
	);

	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
		dispatcher.register(
			Commands.literal("swing")
				.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
				.executes(c -> swing(c.getSource(), List.of(c.getSource().getEntityOrException()), InteractionHand.MAIN_HAND))
				.then(
					Commands.argument("targets", EntityArgument.entities())
						.executes(c -> swing(c.getSource(), EntityArgument.getEntities(c, "targets"), InteractionHand.MAIN_HAND))
						.then(handSwing("mainhand", InteractionHand.MAIN_HAND))
						.then(handSwing("offhand", InteractionHand.OFF_HAND))
				)
		);
	}

	private static LiteralArgumentBuilder<CommandSourceStack> handSwing(final String name, final InteractionHand hand) {
		return Commands.literal(name)
			.executes(c -> swing(c.getSource(), EntityArgument.getEntities(c, "targets"), hand))
			.then(
				Commands.argument("animation", SwingAnimationArgument.swingAnimationType())
					.executes(
						c -> swing(
							c.getSource(),
							EntityArgument.getEntities(c, "targets"),
							hand,
							SwingAnimationArgument.getSwingAnimationType(c, "animation"),
							SwingAnimation.DEFAULT.duration()
						)
					)
					.then(
						Commands.argument("duration", TimeArgument.time(1))
							.executes(
								c -> swing(
									c.getSource(),
									EntityArgument.getEntities(c, "targets"),
									hand,
									SwingAnimationArgument.getSwingAnimationType(c, "animation"),
									IntegerArgumentType.getInteger(c, "duration")
								)
							)
					)
			);
	}

	private static int swing(final CommandSourceStack source, final Collection<? extends Entity> targets, final InteractionHand hand) throws CommandSyntaxException {
		return swing(source, targets, hand, SwingAnimation.DEFAULT.type(), SwingAnimation.DEFAULT.duration());
	}

	private static int swing(
		final CommandSourceStack source,
		final Collection<? extends Entity> targets,
		final InteractionHand hand,
		final SwingAnimationType animationType,
		final int duration
	) throws CommandSyntaxException {
		CommandResponseTracker<LivingEntity> tracker = CommandResponseTracker.create();
		SwingAnimation animation = new SwingAnimation(animationType, duration);

		for (Entity entity : targets) {
			if (entity instanceof LivingEntity livingEntity) {
				livingEntity.swing(hand, animation, true);
				tracker.track(livingEntity);
			}
		}

		return tracker.sendFeedback(source, true, RESPONSE_SWING);
	}
}
