#include "console_output_model.h"

#include <algorithm>
#include <cctype>

namespace dragonx {
namespace ui {

namespace {
std::string lowerCopy(std::string value)
{
    std::transform(value.begin(), value.end(), value.begin(),
                   [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
    return value;
}
}

bool consoleLinePassesFilter(const std::string& lineText,
                             ConsoleChannel channel,
                             const ConsoleOutputFilter& filter)
{
    if (!filter.daemonMessagesEnabled &&
        (channel == ConsoleChannel::Daemon || channel == ConsoleChannel::Xmrig)) return false;
    if (!filter.rpcTraceEnabled && channel == ConsoleChannel::Rpc) return false;
    if (!filter.appMessagesEnabled && channel == ConsoleChannel::App) return false;
    if (filter.errorsOnly && channel != ConsoleChannel::Error) return false;
    if (!filter.text.empty()) {
        std::string needle = lowerCopy(filter.text);
        std::string haystack = lowerCopy(lineText);
        if (haystack.find(needle) == std::string::npos) return false;
    }
    return true;
}

} // namespace ui
} // namespace dragonx
