#include <BlockRegistry/BlockRegistry.hpp>
#include <Widget/ItemIcon/ItemIcon.hpp>
#include <Widget/Label/Label.hpp>
#include <Widget/Layout/Center/Center.hpp>
#include <Widget/Layout/CompoundWidget/Deck/Deck.hpp>
#include <Widget/Layout/CompoundWidget/DocItemTag/DocItemTag.hpp>
#include <Widget/Layout/Frame/Frame.hpp>
#include <Widget/Layout/Row/Row.hpp>

DocItemTag::DocItemTag(const std::vector<std::string> &args)
{
    if (args.size() < 2 || args.size() > 3)
    {
        addChildren(new Label("<!!!>", Color::error));
        return;
    }

    int itemCount = 1;
    if (args.size() > 2)
    {
        try
        {
            itemCount = std::stoi(args[2]);
        }
        catch (std::invalid_argument &e)
        {
            addChildren(new Label("<!!!>", Color::error));
            return;
        }
    }

    ItemStack itemStack;
    if (args[0] == "item")
    {
        itemStack = ItemStack(args[1], itemCount);
    }
    else if (args[0] == "block")
    {
        itemStack = blockRegistry.getBlockByName(args[1]).toItemStack(itemCount);
    }
    else
    {
        addChildren(new Label("<!!!>", Color::error));
        return;
    }

    addChildren(
        new Deck<Row, Center>(
            new Label(formatString("$+x", itemCount), Color::itemCountEmphasis),
            (new Frame(Color::widgetBackground, new ItemIcon(itemStack)))->withMargin(1),
            new Label(itemStack.getDisplayName(), Color::itemEmphasis)
        )
    );
}