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 BlindfoldCommands {

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

    static int blindfold(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.isBlindfolded()) {
            context
                .getSource()
                .sendFailure(
                    Component.translatable(
                        "command.tiedup.blindfold.already_blindfolded",
                        targetPlayer.getName().getString()
                    )
                );
            return 0;
        }

        ItemStack blindfold = DataDrivenBondageItem.createStack(ResourceLocation.fromNamespaceAndPath("tiedup", "classic_blindfold"));
        state.putBlindfoldOn(blindfold);

        CommandHelper.syncPlayerState(targetPlayer, state);

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

        return 1;
    }

    static int unblind(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.isBlindfolded()) {
            context
                .getSource()
                .sendFailure(
                    Component.translatable(
                        "command.tiedup.blindfold.not_blindfolded",
                        targetPlayer.getName().getString()
                    )
                );
            return 0;
        }

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

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

        return 1;
    }

}
