package com.tiedup.remake.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.tiedup.remake.items.ModItems;
import com.tiedup.remake.items.base.BindVariant;
import com.tiedup.remake.items.base.BlindfoldVariant;
import com.tiedup.remake.items.base.EarplugsVariant;
import com.tiedup.remake.items.base.GagVariant;
import com.tiedup.remake.items.base.KnifeVariant;
import java.util.Optional;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;

/**
 * Utility commands.
 *
 * Commands:
 *   /kidnapset - Get a starter kit of mod items
 *   /kidnapreload [type] - Reload data files (jobs, sales, gagtalk)
 */
public class KidnapSetCommand {

    public static void register(
        CommandDispatcher<CommandSourceStack> dispatcher
    ) {
        dispatcher.register(createKidnapSetCommand());
        dispatcher.register(createKidnapReloadCommand());
    }

    /**
     * Create the kidnapset command builder (for use as subcommand of /tiedup).
     * @return The command builder
     */
    public static com.mojang.brigadier.builder.LiteralArgumentBuilder<
        CommandSourceStack
    > createKidnapSetCommand() {
        return Commands.literal("kidnapset")
            .requires(CommandHelper.REQUIRES_OP)
            .executes(KidnapSetCommand::giveSet);
    }

    /**
     * Create the kidnapreload command builder (for use as subcommand of /tiedup).
     * @return The command builder
     */
    public static com.mojang.brigadier.builder.LiteralArgumentBuilder<
        CommandSourceStack
    > createKidnapReloadCommand() {
        return Commands.literal("kidnapreload")
            .requires(CommandHelper.REQUIRES_OP)
            .executes(ctx -> reload(ctx, "all"))
            .then(Commands.literal("jobs").executes(ctx -> reload(ctx, "jobs")))
            .then(
                Commands.literal("sales").executes(ctx -> reload(ctx, "sales"))
            )
            .then(
                Commands.literal("gagtalk").executes(ctx ->
                    reload(ctx, "gagtalk")
                )
            )
            .then(Commands.literal("all").executes(ctx -> reload(ctx, "all")));
    }

    /**
     * Give a starter kit of mod items to the player.
     */
    private static int giveSet(CommandContext<CommandSourceStack> context)
        throws CommandSyntaxException {
        CommandSourceStack source = context.getSource();

        Optional<ServerPlayer> playerOpt = CommandHelper.getPlayerOrFail(
            source
        );
        if (playerOpt.isEmpty()) return 0;
        ServerPlayer player = playerOpt.get();

        int given = 0;

        // Binds
        given += giveItem(
            player,
            new ItemStack(ModItems.getBind(BindVariant.ROPES), 8)
        );
        given += giveItem(
            player,
            new ItemStack(ModItems.getBind(BindVariant.CHAIN), 4)
        );
        given += giveItem(
            player,
            new ItemStack(ModItems.getBind(BindVariant.LEATHER_STRAPS), 4)
        );

        // Gags
        given += giveItem(
            player,
            new ItemStack(ModItems.getGag(GagVariant.CLOTH_GAG), 4)
        );
        given += giveItem(
            player,
            new ItemStack(ModItems.getGag(GagVariant.BALL_GAG), 4)
        );
        given += giveItem(
            player,
            new ItemStack(ModItems.getGag(GagVariant.TAPE_GAG), 4)
        );

        // Blindfolds
        given += giveItem(
            player,
            new ItemStack(ModItems.getBlindfold(BlindfoldVariant.CLASSIC), 4)
        );
        given += giveItem(
            player,
            new ItemStack(ModItems.getBlindfold(BlindfoldVariant.MASK), 2)
        );

        // Collars
        given += giveItem(
            player,
            new ItemStack(ModItems.CLASSIC_COLLAR.get(), 4)
        );
        given += giveItem(
            player,
            new ItemStack(ModItems.SHOCK_COLLAR.get(), 2)
        );
        given += giveItem(player, new ItemStack(ModItems.GPS_COLLAR.get(), 2));

        // Tools
        given += giveItem(
            player,
            new ItemStack(ModItems.getKnife(KnifeVariant.IRON), 2)
        );
        given += giveItem(
            player,
            new ItemStack(ModItems.getKnife(KnifeVariant.GOLDEN), 1)
        );
        given += giveItem(player, new ItemStack(ModItems.WHIP.get(), 1));
        given += giveItem(player, new ItemStack(ModItems.PADDLE.get(), 1));

        // Controllers
        given += giveItem(
            player,
            new ItemStack(ModItems.SHOCKER_CONTROLLER.get(), 1)
        );
        given += giveItem(player, new ItemStack(ModItems.GPS_LOCATOR.get(), 1));

        // Keys and locks
        given += giveItem(player, new ItemStack(ModItems.PADLOCK.get(), 4));
        given += giveItem(player, new ItemStack(ModItems.COLLAR_KEY.get(), 2));
        given += giveItem(player, new ItemStack(ModItems.MASTER_KEY.get(), 1));

        // Earplugs
        given += giveItem(
            player,
            new ItemStack(ModItems.getEarplugs(EarplugsVariant.CLASSIC), 4)
        );

        // Rope arrows
        given += giveItem(player, new ItemStack(ModItems.ROPE_ARROW.get(), 16));

        // Chloroform
        given += giveItem(
            player,
            new ItemStack(ModItems.CHLOROFORM_BOTTLE.get(), 2)
        );
        given += giveItem(player, new ItemStack(ModItems.RAG.get(), 4));

        int finalGiven = given;
        source.sendSuccess(
            () ->
                Component.literal(
                    "§aGave kidnap set (" + finalGiven + " item stacks)"
                ),
            true
        );

        return finalGiven;
    }

    private static int giveItem(ServerPlayer player, ItemStack stack) {
        if (!player.getInventory().add(stack)) {
            // Drop on ground if inventory full
            player.drop(stack, false);
        }
        return 1;
    }

    /**
     * Reload data files.
     */
    private static int reload(
        CommandContext<CommandSourceStack> context,
        String type
    ) {
        CommandSourceStack source = context.getSource();
        int reloaded = 0;

        if (type.equals("all") || type.equals("jobs")) {
            com.tiedup.remake.util.tasks.JobLoader.init();
            reloaded++;
        }

        if (type.equals("all") || type.equals("sales")) {
            com.tiedup.remake.util.tasks.SaleLoader.init();
            reloaded++;
        }

        if (type.equals("all") || type.equals("gagtalk")) {
            // GagTalkManager is code-based (no external data files)
            // No reload needed - materials/logic defined in GagMaterial enum
            reloaded++;
        }

        int finalReloaded = reloaded;
        source.sendSuccess(
            () ->
                Component.literal(
                    "§aReloaded " +
                        (type.equals("all") ? "all data files" : type) +
                        " (" +
                        finalReloaded +
                        " files)"
                ),
            true
        );

        return reloaded;
    }
}
