/*
 *
 * AttributeCommands
 *
 * This class handles all the possible in-game commands for the attribute system
 *
 *
*/

package jesse.keeblarcraft.Commands;

import java.util.Map.Entry;

import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;

import jesse.keeblarcraft.Keeblarcraft;
import jesse.keeblarcraft.AttributeMgr.AttributeMgr;
import jesse.keeblarcraft.AttributeMgr.AttributeNodes.AbstractNode;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;

public class AttributeCommands {
    public void RegisterCommands() {

        // Command Root: "/attributes"
        // Subcommands: "apply <name> <attributename>", "remove <name> <attributename>"
        CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
            var attributeNode = CommandManager.literal("attributes").build();

            var applyNode = CommandManager.literal("apply").build();
            var removeNode = CommandManager.literal("delete").build();
            var listNode = CommandManager.literal("list").executes(context -> ListAttributes(context)).build();

            var playerArgAdd = CommandManager.argument("targetPlayer", EntityArgumentType.player()).build();
            var playerArgRemove = CommandManager.argument("targetPlayer", EntityArgumentType.player()).build();

            var attributeNameAdd = CommandManager.argument("attributeName", StringArgumentType.greedyString())
                                    .executes(context ->    ApplyAttribute
                                                            (
                                                            EntityArgumentType.getPlayer(context, "targetPlayer"),
                                                            StringArgumentType.getString(context, "attributeName"),
                                                            context
                                                            )
                                    ).build();

            var attributeNameDelete = CommandManager.argument("attributeName", StringArgumentType.greedyString())
                                    .executes(context ->    DeleteAttribute
                                                            (
                                                            EntityArgumentType.getPlayer(context, "targetPlayer"),
                                                            StringArgumentType.getString(context, "attributeName"),
                                                            context
                                                            )
                                    ).build();

            // Build out the argument tree here
            dispatcher.getRoot().addChild(attributeNode);
            attributeNode.addChild(applyNode);
            attributeNode.addChild(listNode);
            attributeNode.addChild(removeNode);

            // Subcommands "/apply playerArg", "/remove playerArg"
            applyNode.addChild(playerArgAdd);
            removeNode.addChild(playerArgRemove);

            // name argument
            playerArgAdd.addChild(attributeNameAdd);
            playerArgRemove.addChild(attributeNameDelete);
        });
    }

    public int ApplyAttribute(ServerPlayerEntity targetPlayer, String attributeName, CommandContext<ServerCommandSource> context) {
        int ret = -1;
        if (context.getSource().isExecutedByPlayer()) {
            String result = AttributeMgr.ApplyAttribute(targetPlayer.getUuidAsString(), attributeName);
            Keeblarcraft.LOGGER.info("[ApplyAttribute] -> " + result);
            context.getSource().getPlayer().sendMessage(Text.of(result));
            ret = 0;
        }

        return ret;
    }

    public int DeleteAttribute(ServerPlayerEntity username, String attributeName, CommandContext<ServerCommandSource> context) {
        int ret = -1;



        return ret;
    }

    public int ListAttributes(CommandContext<ServerCommandSource> context) {
        int ret = -1;

        if (context.getSource().isExecutedByPlayer()) {
            ServerPlayerEntity player = context.getSource().getPlayer();

            for (Entry<String, Class<? extends AbstractNode>> entry : AttributeMgr.attributes.entrySet()) {
                Keeblarcraft.LOGGER.debug("ATTR-LIST: " + entry.getKey() + " LINKS " + entry.getValue());
                player.sendMessage(Text.of(entry.getKey()));
            }
        }

        return ret;
    }
}
