#include <BlockRegistry/BlockRegistry.hpp>
#include <ChatCommand/GiveMeCommand/GiveMeCommand.hpp>
#include <ItemRegistry/ItemRegistry.hpp>
#include <Log/Log.hpp>

GiveMeCommand::GiveMeCommand()
{
    require(UserPermission::GiveSelfItems);

    addVariant(
        "<name> [options...] - Give yourself a block or item by internal name.",
        [](Player &player, std::string name, std::vector<std::string> options)
        {
            ItemStack stack;

            Item *item = itemRegistry.getItemByName(name);
            if (item != nullptr)
            {
                stack = ItemStack(*item, item->getMaxStackSize());
                player.writeChat(
                    formatString("You gave yourself a stack of '$+' items.", stack.getDisplayName()),
                    Color::success
                );
            }
            else if (blockRegistry.isValidBlock(name))
            {
                Block &block = blockRegistry.getBlockByName(name);
                stack = block.toItemStack(256);
                player.writeChat(
                    formatString("You gave yourself a stack of '$+' blocks.", block.displayName),
                    Color::success
                );
            }
            else
            {
                player.writeChat(formatString("There is no item or block with the name '$+'.", name), Color::error);
                return;
            }

            for (const auto &optionStr : options)
            {
                std::string error = stack.trySetOption(optionStr);
                if (!error.empty())
                {
                    player.writeChat(error, Color::error);
                }
            }

            player.items.store("inventory").insertStack(stack);
        }
    );
}