#include <MaceratorRuleRegistry/MaceratorRuleRegistry.hpp>
#include <MaceratorRule/MaceratorGrindingRule/MaceratorGrindingRule.hpp>
#include <MaceratorRule/OreToDustMaceratorRule/OreToDustMaceratorRule.hpp>
#include <MaceratorRule/BasicMaceratorRule/BasicMaceratorRule.hpp>
#include <MaceratorRule/IngotToDustMaceratorRule/IngotToDustMaceratorRule.hpp>
#include <BlockRegistry/BlockRegistry.hpp>

MaceratorRuleRegistry maceratorRuleRegistry;

void MaceratorRuleRegistry::registerRule(MaceratorRule *rule)
{
    rules.push_back(rule);
}

void MaceratorRuleRegistry::registerBasicRule(ItemStack input, ItemStack output)
{
    registerRule(new BasicMaceratorRule(input, output));
}

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

void MaceratorRuleRegistry::registerAllRules()
{
    registerRule(new MaceratorGrindingRule());
    registerRule(new OreToDustMaceratorRule());
    registerRule(new IngotToDustMaceratorRule());
    registerBasicRule(blockRegistry.getBlockByName("main:stoneCopperOre").toItemStack(), ItemStack("main:copperDust", 2));
    registerBasicRule(blockRegistry.getBlockByName("main:stoneTinOre").toItemStack(), ItemStack("main:tinDust", 2));
    registerBasicRule(blockRegistry.getBlockByName("main:uraniumOreMantle").toItemStack(), ItemStack("main:uraniumDust", 2));
    registerBasicRule(blockRegistry.getBlockByName("main:lapis").toItemStack(), ItemStack("main:blueDye", 2));
    registerBasicRule(blockRegistry.getBlockByName("main:stoneLimestone").toItemStack(), ItemStack("main:whiteDye", 2));
}

ItemStack MaceratorRuleRegistry::getMaceratorOutput(ItemStack &input)
{
    for (MaceratorRule* rule : rules)
    {
        ItemStack potentialOutput = rule->getMaceratorOutput(input);
        if (!potentialOutput.isEmpty())
        {
            return potentialOutput;
        }
    }

    return ItemStack();
}

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