#include <FuelRuleRegistry/FuelRuleRegistry.hpp>
#include <FuelRule/BasicFuelRule/BasicFuelRule.hpp>
#include <FuelRule/LogFuelRule/LogFuelRule.hpp>
#include <BlockRegistry/BlockRegistry.hpp>

FuelRuleRegistry fuelRuleRegistry;

void FuelRuleRegistry::registerRule(FuelRule *rule)
{
    rules.push_back(rule);
}

void FuelRuleRegistry::registerBasicFuel(ItemStack fuelStack, uint32_t amount)
{
    registerRule(new BasicFuelRule(fuelStack, amount));
}

void FuelRuleRegistry::shutdown()
{
    for (auto& rule : rules)
    {
        delete rule;
    }
}

void FuelRuleRegistry::registerAllRules()
{
    registerRule(new LogFuelRule());
    registerBasicFuel(blockRegistry.getBlockByName("main:planks").toItemStack(), 150);
    registerBasicFuel(ItemStack("main:stick"), 30);
    registerBasicFuel(ItemStack("main:charcoal"), 300);
    registerBasicFuel(ItemStack("main:coal"), 1200);
}

bool FuelRuleRegistry::isValidFuel(ItemStack &stack)
{
    for (FuelRule *rule : rules)
    {
        if (rule->isValidFuel(stack))
        {
            return true;
        }
    }

    return false;
}

uint32_t FuelRuleRegistry::getFuelAmount(ItemStack &stack)
{
    uint32_t amount = 0;

    for (FuelRule *rule : rules)
    {
        if (rule->isValidFuel(stack))
        {
            uint32_t thisOne = rule->getFuelAmount(stack);
            if (thisOne > amount)
            {
                amount = thisOne;
            }
        }
    }

    return amount;
}

int FuelRuleRegistry::getRuleCount()
{
    return (int) rules.size();
}