package com.tiedup.remake.commands.subcommands;

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.core.SystemMessageManager;
import com.tiedup.remake.v2.bondage.datadriven.DataDrivenBondageItem;
import net.minecraft.ChatFormatting;
import net.minecraft.resources.ResourceLocation;
import com.tiedup.remake.state.PlayerBindState;
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;
import net.minecraft.world.item.ItemStack;

@SuppressWarnings("null")
public class GagCommands {

    public static void register(LiteralArgumentBuilder<CommandSourceStack> root) {
        // /tiedup gag <player>
        root.then(
            Commands.literal("gag")
                .requires(CommandHelper.REQUIRES_OP)
                .then(
                    Commands.argument(
                        "player",
                        EntityArgument.player()
                    ).executes(GagCommands::gag)
                )
        );
        // /tiedup ungag <player>
        root.then(
            Commands.literal("ungag")
                .requires(CommandHelper.REQUIRES_OP)
                .then(
                    Commands.argument(
                        "player",
                        EntityArgument.player()
                    ).executes(GagCommands::ungag)
                )
        );
    }

    static int gag(CommandContext<CommandSourceStack> context)
        throws CommandSyntaxException {
        ServerPlayer targetPlayer = EntityArgument.getPlayer(context, "player");
        PlayerBindState state = PlayerBindState.getInstance(targetPlayer);

        if (state == null) {
            context
                .getSource()
                .sendFailure(Component.translatable("command.tiedup.error.no_state"));
            return 0;
        }

        if (state.isGagged()) {
            context
                .getSource()
                .sendFailure(
                    Component.translatable(
                        "command.tiedup.gag.already_gagged",
                        targetPlayer.getName().getString()
                    )
                );
            return 0;
        }

        ItemStack gag = DataDrivenBondageItem.createStack(ResourceLocation.fromNamespaceAndPath("tiedup", "cloth_gag"));
        state.putGagOn(gag);

        CommandHelper.syncPlayerState(targetPlayer, state);

        context
            .getSource()
            .sendSuccess(
                () ->
                    Component.translatable(
                        "command.tiedup.gag.gagged",
                        targetPlayer.getName().getString()
                    ).withStyle(ChatFormatting.GREEN),
                true
            );
        SystemMessageManager.sendGagged(
            context.getSource().getEntity(),
            targetPlayer
        );

        return 1;
    }

    static int ungag(CommandContext<CommandSourceStack> context)
        throws CommandSyntaxException {
        ServerPlayer targetPlayer = EntityArgument.getPlayer(context, "player");
        PlayerBindState state = PlayerBindState.getInstance(targetPlayer);

        if (state == null) {
            context
                .getSource()
                .sendFailure(Component.translatable("command.tiedup.error.no_state"));
            return 0;
        }

        if (!state.isGagged()) {
            context
                .getSource()
                .sendFailure(
                    Component.translatable(
                        "command.tiedup.gag.not_gagged",
                        targetPlayer.getName().getString()
                    )
                );
            return 0;
        }

        state.takeGagOff();
        CommandHelper.syncPlayerState(targetPlayer, state);

        context
            .getSource()
            .sendSuccess(
                () ->
                    Component.translatable(
                        "command.tiedup.gag.removed",
                        targetPlayer.getName().getString()
                    ).withStyle(ChatFormatting.GREEN),
                true
            );
        SystemMessageManager.sendToTarget(
            context.getSource().getEntity(),
            targetPlayer,
            SystemMessageManager.MessageCategory.UNGAGGED
        );

        return 1;
    }

}
