#include <SmeltingRule/OreSmeltingRule/OreSmeltingRule.hpp>
#include <Item/StatefulItem/BlockItem/BlockItem.hpp>
#include <ItemRegistry/ItemRegistry.hpp>

Item* OreSmeltingRule::getIngotFor(std::string oreName)
{
    return itemRegistry.getItemByName(oreName.substr(0, oreName.size()-3) + "Ingot");
}

bool OreSmeltingRule::canSmelt(ItemStack &stack)
{
    BlockItem *blockItem = stack.getItemAs<BlockItem>();
    if (blockItem == nullptr)
    {
        return false;
    }

    Block &block = blockItem->getBlockInStack(stack);
    if (!block.isSuffixed("Ore"))
    {
        return false;
    }

    return getIngotFor(block.name) != nullptr;
}

ItemStack OreSmeltingRule::getSmeltingResult(ItemStack &stack)
{
    BlockItem *blockItem = stack.getItemAs<BlockItem>();
    Block &block = blockItem->getBlockInStack(stack);
    return ItemStack(*getIngotFor(block.name), 1);
}