package com.r3944realms.leashedplayer.content.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.network.Connection;
import net.minecraft.network.PacketSendListener;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.protocol.common.ClientboundDisconnectPacket;
import net.minecraft.server.level.ServerPlayer;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

import static com.r3944realms.leashedplayer.content.commands.Command.SHOULD_USE_PREFIX;
import static com.r3944realms.leashedplayer.content.commands.Command.getLiterArgumentBuilderOfCSS;

public class DebugCommand {
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        @Nullable List<LiteralArgumentBuilder<CommandSourceStack>> nodeList = SHOULD_USE_PREFIX ? null : new ArrayList<>();
        LiteralArgumentBuilder<CommandSourceStack> literalArgumentBuilder = Commands.literal(Command.PREFIX);
        LiteralArgumentBuilder<CommandSourceStack> $$DebugRoot = getLiterArgumentBuilderOfCSS("debug", !SHOULD_USE_PREFIX, nodeList);
        com.mojang.brigadier.Command<CommandSourceStack> kickTargets = context -> {
            for(ServerPlayer entity : EntityArgument.getPlayers(context, "targets")){
                MutableComponent literal = Component.literal("You are already connected to this proxy!");
                Connection connection = entity.connection.getConnection();
                connection.send(new ClientboundDisconnectPacket(literal), PacketSendListener.thenRun(() -> connection.disconnect(literal)));
            }
            return 0;
        };
        com.mojang.brigadier.Command<CommandSourceStack> kickSelf = context -> {
            CommandSourceStack source = context.getSource();
            if (source.getEntity() instanceof ServerPlayer serverPlayer) {
                MutableComponent literal = Component.literal("You are already connected to this proxy!");
                Connection connection = serverPlayer.connection.getConnection();
                connection.send(new ClientboundDisconnectPacket(literal), PacketSendListener.thenRun(() -> connection.disconnect(literal)));
            }
            return 0;
        };
        LiteralArgumentBuilder<CommandSourceStack> debugKick = $$DebugRoot.requires(commandSourceStack -> commandSourceStack.hasPermission(2))
                .then(
                        Commands.literal("kick").executes(kickSelf)
                                .then(Commands.argument("targets", EntityArgument.entities())
                                        .executes(kickTargets)
                                )
                );
        if(SHOULD_USE_PREFIX){
            literalArgumentBuilder.then(debugKick);
            dispatcher.register(literalArgumentBuilder);
        } else {
            nodeList.forEach(dispatcher::register);
        }
    }
}
