#include <ItemFilter/ItemFilterByName/ItemFilterByName.hpp>

ItemFilterByName::ItemFilterByName(std::string query)
{
    setQuery(query);
}

void ItemFilterByName::setQuery(std::string query)
{
    normalizedQuery = query;
    std::transform(normalizedQuery.begin(), normalizedQuery.end(), normalizedQuery.begin(),
        [](unsigned char c){ return std::tolower(c); });
}

bool ItemFilterByName::matches(ItemStack &stack)
{
    std::string displayName = stack.getDisplayName();
    if (displayName[0] == '<')
    {
        // Don't match the special items.
        return false;
    }

    std::transform(displayName.begin(), displayName.end(), displayName.begin(),
        [](unsigned char c){ return std::tolower(c); });

    return displayName.find(normalizedQuery) != std::string::npos;
}