#include "HighlighterFactory.h"

#include <QFileInfo>
#include <QHash>
#include <functional>

SyntaxHighlighter *HighlighterFactory::createForFile(const QString &filePath,
                                                     QTextDocument *document)
{
    // Erweiterung → Highlighter-Fabrik
    // Neue Sprache hinzufügen: einfach einen Eintrag ergänzen.
    static const QHash<QString, std::function<SyntaxHighlighter *(QTextDocument *)>> registry =
    {
        // C / C++
        { "c",    [](QTextDocument *d) { return new CppHighlighter(d); } },
        { "cc",   [](QTextDocument *d) { return new CppHighlighter(d); } },
        { "cpp",  [](QTextDocument *d) { return new CppHighlighter(d); } },
        { "cxx",  [](QTextDocument *d) { return new CppHighlighter(d); } },
        { "h",    [](QTextDocument *d) { return new CppHighlighter(d); } },
        { "hpp",  [](QTextDocument *d) { return new CppHighlighter(d); } },
        { "hxx",  [](QTextDocument *d) { return new CppHighlighter(d); } },

        // CSS
        { "css",  [](QTextDocument *d) { return new CssHighlighter(d); } },

        // HTML / Templates
        { "html", [](QTextDocument *d) { return new HtmlHighlighter(d); } },
        { "htm",  [](QTextDocument *d) { return new HtmlHighlighter(d); } },
        { "xhtml",[](QTextDocument *d) { return new HtmlHighlighter(d); } },

        // PHP (HTML + eingebettetes PHP)
        { "php",  [](QTextDocument *d) { return new PhpHighlighter(d); } },
        { "phtml",[](QTextDocument *d) { return new PhpHighlighter(d); } },
        { "php3", [](QTextDocument *d) { return new PhpHighlighter(d); } },
        { "php4", [](QTextDocument *d) { return new PhpHighlighter(d); } },
        { "php5", [](QTextDocument *d) { return new PhpHighlighter(d); } },
        { "php7", [](QTextDocument *d) { return new PhpHighlighter(d); } },
        { "php8", [](QTextDocument *d) { return new PhpHighlighter(d); } },
    };

    const QString ext = QFileInfo(filePath).suffix().toLower();
    const auto it = registry.find(ext);

    return (it != registry.end()) ? it.value()(document) : nullptr;
}
