package com.tiedup.remake.commands.subcommands;

import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.tiedup.remake.commands.CommandHelper;
import com.tiedup.remake.network.ModNetwork;
import com.tiedup.remake.network.sync.PacketPlayTestAnimation;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;

/**
 * Test animation sub-commands for /tiedup.
 * Handles: testanim <animId> [player], testanim stop [player]
 */
@SuppressWarnings("null")
public class TestAnimSubCommand {

    public static void register(
        LiteralArgumentBuilder<CommandSourceStack> root
    ) {
        // /tiedup testanim <animId> [player]
        // /tiedup testanim stop [player]
        root.then(
            Commands.literal("testanim")
                .requires(CommandHelper.REQUIRES_OP)
                .then(
                    Commands.literal("stop")
                        .executes(ctx -> testAnimStop(ctx, null))
                        .then(
                            Commands.argument(
                                "player",
                                EntityArgument.player()
                            ).executes(ctx ->
                                testAnimStop(
                                    ctx,
                                    EntityArgument.getPlayer(ctx, "player")
                                )
                            )
                        )
                )
                .then(
                    Commands.argument("animId", StringArgumentType.string())
                        .executes(ctx -> testAnim(ctx, null))
                        .then(
                            Commands.argument(
                                "player",
                                EntityArgument.player()
                            ).executes(ctx ->
                                testAnim(
                                    ctx,
                                    EntityArgument.getPlayer(ctx, "player")
                                )
                            )
                        )
                )
        );
    }

    // Command Implementations

    /**
     * /tiedup testanim <animId> [player]
     * Play an animation from player_animation/ on a player.
     */
    private static int testAnim(
        CommandContext<CommandSourceStack> context,
        ServerPlayer target
    ) throws CommandSyntaxException {
        if (target == null) {
            target = context.getSource().getPlayerOrException();
        }
        String animId = StringArgumentType.getString(context, "animId");

        ModNetwork.sendToAllTrackingAndSelf(
            new PacketPlayTestAnimation(target.getUUID(), animId),
            target
        );

        final String name = target.getName().getString();
        final String anim = animId;
        context
            .getSource()
            .sendSuccess(
                () ->
                    Component.translatable(
                        "command.tiedup.testanim.playing", anim, name
                    ),
                false
            );
        return 1;
    }

    /**
     * /tiedup testanim stop [player]
     * Stop animation on a player.
     */
    private static int testAnimStop(
        CommandContext<CommandSourceStack> context,
        ServerPlayer target
    ) throws CommandSyntaxException {
        if (target == null) {
            target = context.getSource().getPlayerOrException();
        }

        ModNetwork.sendToAllTrackingAndSelf(
            new PacketPlayTestAnimation(target.getUUID(), ""),
            target
        );

        final String name = target.getName().getString();
        context
            .getSource()
            .sendSuccess(
                () -> Component.translatable("command.tiedup.testanim.stopped", name),
                false
            );
        return 1;
    }
}
