/*
    SPDX-FileCopyrightText: 2010, 2011, 2012 Alex Richardson <alex.richardson@gmx.de>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#include "osdparser.hpp"

// lib
#include "datainformationfactory.hpp"
#include <arraydatainformation.hpp>
#include <uniondatainformation.hpp>
#include <structuredatainformation.hpp>
#include <stringdatainformation.hpp>
#include <stringdata.hpp>
#include <primitivefactory.hpp>
#include <topleveldatainformation.hpp>
#include <dummydatainformation.hpp>
#include <structuredefinitionfile.hpp>
#include <scriptlogger.hpp>
#include <scriptengineinitializer.hpp>
#include <structureslogging.hpp>
// KF
#include <KLocalizedString>
// Qt
#include <QFile>
#include <QFileInfo>
#include <QDomDocument>
#include <QScriptEngine>
// Std
#include <memory>
#include <utility>

using namespace ParserStrings;

OsdParser::OsdParser(const QString& pluginName, const QString& absolutePath)
    : AbstractStructureParser(pluginName, absolutePath)
{
}

OsdParser::OsdParser(const QString& xml)
    : AbstractStructureParser(QString(), QString())
    , mXmlString(xml)
{
}

OsdParser::~OsdParser() = default;

QDomDocument OsdParser::openDoc(ScriptLogger* logger) const
{
    return mXmlString.isEmpty() ? openDocFromFile(logger) : openDocFromString(logger);
}

QDomDocument OsdParser::openDocFromString(ScriptLogger* logger) const
{
    Q_ASSERT(!mXmlString.isEmpty());
    QDomDocument doc;
    const QDomDocument::ParseResult result =  doc.setContent(mXmlString);
    if (!result) {
        const QString errorOutput =
            QStringLiteral("Error reading XML: %1\n error line=%2\nerror column=%3")
            .arg(result.errorMessage, QString::number(result.errorLine), QString::number(result.errorColumn));
        logger->error() << errorOutput;
        logger->info() << "XML was:" << mXmlString;

        return {};
    }
    return doc;
}

QDomDocument OsdParser::openDocFromFile(ScriptLogger* logger) const
{
    QFileInfo fileInfo(mAbsolutePath);
    if (!fileInfo.exists()) {
        logger->error().nospace() << "File " << mAbsolutePath << " does not exist.";
        return {};
    }
    QFile file(fileInfo.absoluteFilePath());
    if (!file.open(QIODevice::ReadOnly)) {
        logger->error().nospace() << "Could not open file " << mAbsolutePath << ".";
        return {};
    }
    QDomDocument doc;
    const QDomDocument::ParseResult result =  doc.setContent(&file);
    if (!result) {
        const QString errorOutput =
            QStringLiteral("Error reading XML: %1\n error line=%2\nerror column=%3")
            .arg(result.errorMessage, QString::number(result.errorLine), QString::number(result.errorColumn));
        logger->error() << errorOutput;
        logger->info() << "File was:" << mAbsolutePath;
    }
    file.close();
    return doc;
}

QStringList OsdParser::parseStructureNames() const
{
    QStringList ret;
    auto rootLogger = std::make_unique<ScriptLogger>(); // only needed if we get an error right now
    rootLogger->setLogToStdOut(true); // we cannot get our messages into the script console, so do this instead
    QDomDocument document = openDoc(rootLogger.get());
    if (document.isNull()) {
        return {};
    }
    QDomElement rootElem = document.firstChildElement(QStringLiteral("data"));
    if (rootElem.isNull()) {
        return {};
    }
    for (QDomElement childElement = rootElem.firstChildElement();
         !childElement.isNull(); childElement = childElement.nextSiblingElement()) {
        QString tag = childElement.tagName();
        if (tag == TYPE_STRUCT() || tag == TYPE_ARRAY() || tag == TYPE_BITFIELD() || tag == TYPE_PRIMITIVE()
            || tag == TYPE_UNION() || tag == TYPE_ENUM() || tag == TYPE_FLAGS() || tag == TYPE_STRING()) {
            // TODO allow e.g. <uint8 name="asfd">
            ret.append(readProperty(childElement, PROPERTY_NAME(), i18n("<invalid name>")));
        } else if (tag == TYPE_ENUMDEF()) {
            // skip enum defs
        } else {
            rootLogger->error(QString()).nospace() << "Unknown tag name in plugin '" << mPluginName << "' : "
                                                   << tag;
        }
    }

    return ret;
}

std::vector<std::unique_ptr<TopLevelDataInformation>> OsdParser::parseStructures() const
{
    std::vector<std::unique_ptr<TopLevelDataInformation>> structures;

    QFileInfo fileInfo(mAbsolutePath);

    auto rootLogger = std::make_unique<ScriptLogger>(); // only needed in we get an error right now
    QDomDocument document = openDoc(rootLogger.get());
    if (document.isNull()) {
        structures.emplace_back(std::make_unique<TopLevelDataInformation>(
                                std::make_unique<DummyDataInformation>(nullptr, fileInfo.fileName()), std::move(rootLogger), nullptr, fileInfo));
        return structures;
    }

    QDomElement rootElem = document.firstChildElement(QStringLiteral("data"));
    if (rootElem.isNull()) {
        rootLogger->error() << "Missing top level <data> element.";
        structures.emplace_back(std::make_unique<TopLevelDataInformation>(
                                std::make_unique<DummyDataInformation>(nullptr, fileInfo.fileName()), std::move(rootLogger), nullptr, fileInfo));
        return structures;
    }
    int count = 1;
    for (QDomElement elem = rootElem.firstChildElement(); !elem.isNull(); elem = elem.nextSiblingElement()) {
        if (elem.tagName() == TYPE_ENUMDEF()) {
            continue; // skip enum defs

        }
        std::unique_ptr<QScriptEngine> eng = ScriptEngineInitializer::newEngine(); // we need this for dynamic arrays
        auto logger = std::make_unique<ScriptLogger>();
        std::vector<std::shared_ptr<EnumDefinition>> enums = parseEnums(rootElem, logger.get());
        OsdParserInfo info(QString(), logger.get(), nullptr, eng.get(), std::move(enums));

        auto data = parseElement(elem, info);

        if (!data) {
            QString name = readProperty(elem, PROPERTY_NAME());
            if (name.isEmpty()) {
                name = fileInfo.absoluteFilePath() + QLatin1String("_element") + QString::number(count);
            }
            qCDebug(LOG_KASTEN_OKTETA_CONTROLLERS_STRUCTURES) << "Failed to parse element" << elem.tagName() << name;
            qCDebug(LOG_KASTEN_OKTETA_CONTROLLERS_STRUCTURES) << "Parsing messages were:" << logger->messages();
            data = std::make_unique<DummyDataInformation>(nullptr, name);
        }
        auto& topData = structures.emplace_back(std::make_unique<TopLevelDataInformation>(std::move(data), std::move(logger), std::move(eng), fileInfo));
        QString lockOffsetStr = readProperty(elem, PROPERTY_DEFAULT_LOCK_OFFSET());
        if (!lockOffsetStr.isEmpty()) {
            ParsedNumber<quint64> offset = ParserUtils::uint64FromString(lockOffsetStr);
            if (!offset.isValid) {
                data->logError() << "Default lock offset is not a valid number:" << offset.string;
            } else {
                data->logInfo() << "Default lock offset is:" << offset.string;
                topData->setDefaultLockOffset(offset.value);
            }
        }
        count++;
    }

    return structures;
}

// TODO make type depend on the user not the definition
std::vector<std::shared_ptr<EnumDefinition>> OsdParser::parseEnums(const QDomElement& rootElem, ScriptLogger* logger)
{
    std::vector<std::shared_ptr<EnumDefinition>> ret;
    for (QDomElement elem = rootElem.firstChildElement(); !elem.isNull(); elem = elem.nextSiblingElement()) {
        if (elem.tagName() != TYPE_ENUMDEF()) {
            continue;
        }

        std::map<AllPrimitiveTypes, QString> defs;
        const QString enumName = readProperty(elem, PROPERTY_NAME(), i18n("<invalid name>"));
        const QString typeStr = readProperty(elem, PROPERTY_TYPE());
        if (typeStr.isEmpty()) {
            logger->error(enumName) << "Skipping enum definition, since no type attribute was found.";
            continue;
        }
        LoggerWithContext lwc(logger, QLatin1String("enum values (") + enumName + QLatin1Char(')'));
        PrimitiveDataType type = PrimitiveFactory::typeStringToType(typeStr, lwc);
        // handle all entries
        for (QDomElement child = elem.firstChildElement(); !child.isNull(); child =
                 child.nextSiblingElement()) {
            if (child.tagName() != QLatin1String("entry")) {
                continue;
            }

            QString name = readProperty(child, PROPERTY_NAME());
            if (name.isEmpty()) {
                lwc.warn() << "Entry is missing name, skipping it.";
                continue;
            }
            QString value = readProperty(child, PROPERTY_VALUE());
            const EnumEntry converted = EnumDefinition::convertToEnumEntry(name, value, lwc, type);
            if (converted.isEmpty()) {
                continue;
            }
            defs.emplace(converted.value, converted.name);
        }

        // now add this enum to the list of enums
        if (defs.empty()) {
            lwc.error() << "Enum definition contains no valid elements.";
        } else {
            ret.emplace_back(std::make_shared<EnumDefinition>(std::move(defs), enumName, type));
        }
    }

    return ret;
}

// Datatypes

std::unique_ptr<ArrayDataInformation> OsdParser::arrayFromXML(const QDomElement& xmlElem, const OsdParserInfo& info)
{
    ArrayParsedData apd(info);
    QString lengthStr = readProperty(xmlElem, PROPERTY_LENGTH());
    if (lengthStr.isEmpty()) {
        info.error() << "No array length specified.";
        return {};
    }
    // must wrap in parentheses, cannot just evaluate. See https://bugreports.qt-project.org/browse/QTBUG-5757
    const QScriptValue lengthFunc = ParserUtils::functionSafeEval(info.engine, lengthStr);
    if (lengthFunc.isValid()) {
        apd.length = lengthFunc;
    } else {
        apd.length = QScriptValue(lengthStr);
    }
    // first check whether there is a <type> element and use the inner element
    // if that doesn't exist use the first child element as the type, but only if there is only one child
    apd.arrayType = parseType(xmlElem, info, NAME_ARRAY_TYPE());
    if (!apd.arrayType) {
        // was not specified as <type> element or type="attribute", use first child
        DummyDataInformation dummy(info.parent, info.name); // dummy so that we have a proper chain
        OsdChildrenParser typeParser(info, xmlElem.firstChildElement());
        typeParser.setParent(&dummy);
        if (typeParser.hasNext()) {
            apd.arrayType = typeParser.next();
            if (typeParser.hasNext()) {
                info.error() << "More than one possible type for array.";
                apd.arrayType.reset();
                return {};
            }
        }
    }
    return DataInformationFactory::newArray(apd);
}

std::unique_ptr<DataInformation> OsdParser::parseChildElement(const QDomElement& xmlElem, const OsdParserInfo& info, const QString& name)
{
    OsdParserInfo newInfo(info);
    // instantiate a dummy so that a property chain up to the root element exists
    DummyDataInformation dummy(info.parent, info.name);
    newInfo.parent = &dummy;
    newInfo.name = name;
    return parseElement(xmlElem, newInfo);
}

std::unique_ptr<DataInformation> OsdParser::parseType(const QDomElement& xmlElem, const OsdParserInfo& info, const QString& name)
{
    const QString typeAttribute = xmlElem.attribute(PROPERTY_TYPE());
    if (!typeAttribute.isEmpty()) {
        // type was specified as a primitive string
        LoggerWithContext lwc(info.logger, info.context() + name);
        auto ret = PrimitiveFactory::newInstance(name, typeAttribute, lwc);
        if (!ret) {
            info.error() << "Not a valid type identifier:" << typeAttribute;
        }
        return ret;
    }
    // we have to parse the first child element of the <type> element
    const QDomElement toParse = xmlElem.firstChildElement(PROPERTY_TYPE()).firstChildElement();
    if (toParse.isNull()) {
        // don't log an error here, it may be okay (i.e. in arrays <type> can be omitted)
        return nullptr;
    }
    if (!toParse.nextSiblingElement().isNull()) {
        info.warn() << "<type> element has more than one child.";
    }
    // TODO have this newInfo code only in one location
    std::unique_ptr<DataInformation> ret = parseChildElement(toParse, info, name);
    if (!ret) {
        info.error() << "Failed to parse element defined in <type>.";
    }
    return ret;
}

std::unique_ptr<PointerDataInformation> OsdParser::pointerFromXML(const QDomElement& xmlElem, const OsdParserInfo& info)
{
    PointerParsedData ppd(info);

    ppd.valueType = parseType(xmlElem, info, NAME_POINTER_VALUE_TYPE());

    // first check whether there is a <target> element and use the inner element
    // if that doesn't exist use the first child element as the type, but only if there is only one child
    QDomElement childElement = xmlElem.firstChildElement(PROPERTY_TARGET()).firstChildElement();
    if (childElement.isNull()) {
        childElement = xmlElem.firstChildElement();
        if (childElement.isNull()) {
            info.error() << "Pointer target is missing. Please add a <target> child element.";
            return {};
        }
        if (childElement != xmlElem.lastChildElement()) {
            // there is more than one child element
            info.error() << "There is more than one child element, cannot determine which one "
                "is the pointer target. Wrap the correct one in a <target> element.";
            return {};
        }
    }
    ppd.pointerTarget = parseChildElement(childElement, info, NAME_POINTER_TARGET());
    return DataInformationFactory::newPointer(ppd);
}

std::unique_ptr<PrimitiveDataInformation> OsdParser::primitiveFromXML(const QDomElement& xmlElem, const OsdParserInfo& info)
{
    PrimitiveParsedData ppd(info);
    ppd.type = readProperty(xmlElem, PROPERTY_TYPE());
    return DataInformationFactory::newPrimitive(ppd);
}

std::unique_ptr<AbstractBitfieldDataInformation> OsdParser::bitfieldFromXML(const QDomElement& xmlElem,
                                                                            const OsdParserInfo& info)
{
    BitfieldParsedData bpd(info);
    bpd.type = readProperty(xmlElem, PROPERTY_TYPE());
    QString width = readProperty(xmlElem, PROPERTY_WIDTH());
    bpd.width = ParserUtils::intFromString(width);
    return DataInformationFactory::newBitfield(bpd);
}

std::unique_ptr<UnionDataInformation> OsdParser::unionFromXML(const QDomElement& xmlElem, const OsdParserInfo& info)
{
    StructOrUnionParsedData supd(info);
    supd.children = std::make_unique<OsdChildrenParser>(info, xmlElem.firstChildElement());
    return DataInformationFactory::newUnion(supd);
}

std::unique_ptr<StructureDataInformation> OsdParser::structFromXML(const QDomElement& xmlElem, const OsdParserInfo& info)
{
    StructOrUnionParsedData supd(info);
    supd.children = std::make_unique<OsdChildrenParser>(info, xmlElem.firstChildElement());
    return DataInformationFactory::newStruct(supd);
}

std::unique_ptr<EnumDataInformation> OsdParser::enumFromXML(const QDomElement& xmlElem, bool isFlags,
                                                            const OsdParserInfo& info)
{
    EnumParsedData epd(info);
    epd.type = readProperty(xmlElem, PROPERTY_TYPE());
    epd.enumName = readProperty(xmlElem, PROPERTY_ENUM_NAME());
    if (epd.enumName.isEmpty()) {
        epd.enumName = readProperty(xmlElem, TYPE_ENUM()); // used again here as property
    }
    epd.enumDef = findEnum(epd.enumName, info);
    if (!epd.enumDef) {
        info.error() << "Enum definition does not exist:" << epd.enumName ;
        return {};
    }

    if (isFlags) {
        return DataInformationFactory::newFlags(epd);
    }

    return DataInformationFactory::newEnum(epd);
}

std::unique_ptr<StringDataInformation> OsdParser::stringFromXML(const QDomElement& xmlElem,
                                                                const OsdParserInfo& info)
{
    StringParsedData spd(info);
    spd.encoding = readProperty(xmlElem, PROPERTY_ENCODING());
    spd.termination = ParserUtils::uintFromString(readProperty(xmlElem, PROPERTY_TERMINATED_BY()));
    spd.maxByteCount = ParserUtils::uintFromString(readProperty(xmlElem, PROPERTY_MAX_BYTE_COUNT()));
    spd.maxCharCount = ParserUtils::uintFromString(readProperty(xmlElem, PROPERTY_MAX_CHAR_COUNT()));
    return DataInformationFactory::newString(spd);
}

std::unique_ptr<DataInformation> OsdParser::parseElement(const QDomElement& elem, const OsdParserInfo& oldInfo)
{
    Q_ASSERT(!elem.isNull());
    std::unique_ptr<DataInformation> data;
    const QString tag = elem.tagName();
    OsdParserInfo info(oldInfo);
    info.name = readProperty(elem, PROPERTY_NAME(), QStringLiteral("<anonymous>"));
    if (tag == TYPE_STRUCT()) {
        data = structFromXML(elem, info);
    } else if (tag == TYPE_ARRAY()) {
        data = arrayFromXML(elem, info);
    } else if (tag == TYPE_BITFIELD()) {
        data = bitfieldFromXML(elem, info);
    } else if (tag == TYPE_PRIMITIVE()) {
        data = primitiveFromXML(elem, info);
    } else if (tag == TYPE_UNION()) {
        data = unionFromXML(elem, info);
    } else if (tag == TYPE_ENUM()) {
        data = enumFromXML(elem, false, info);
    } else if (tag == TYPE_FLAGS()) {
        data = enumFromXML(elem, true, info);
    } else if (tag == TYPE_STRING()) {
        data = stringFromXML(elem, info);
    } else if (tag == TYPE_POINTER()) {
        data = pointerFromXML(elem, info);
    } else if (tag == TYPE_TAGGED_UNION()) {
        data = taggedUnionFromXML(elem, info);
    } else {
        LoggerWithContext lwc(info.logger, info.context());
        // use the type tag as a primitive type
        data = PrimitiveFactory::newInstance(info.name, tag, lwc);
        if (!data) {
            info.error() << "Cannot parse unknown tag:" << tag;
        }
    }

    if (data) {
        CommonParsedData cpd(info);
        QString byteOrderStr = readProperty(elem, PROPERTY_BYTEORDER());
        if (!byteOrderStr.isEmpty()) {
            cpd.endianness = ParserUtils::byteOrderFromString(byteOrderStr,
                                                              LoggerWithContext(info.logger, info.context()));
        }
        cpd.updateFunc = ParserUtils::functionSafeEval(info.engine, readProperty(elem, PROPERTY_UPDATE_FUNC()));
        cpd.validationFunc = ParserUtils::functionSafeEval(info.engine, readProperty(elem, PROPERTY_VALIDATION_FUNC()));
        cpd.toStringFunc = ParserUtils::functionSafeEval(info.engine, readProperty(elem, PROPERTY_TO_STRING_FUNC()));
        cpd.customTypeName = readProperty(elem, PROPERTY_CUSTOM_TYPE_NAME());
        if (!DataInformationFactory::commonInitialization(data.get(), cpd)) {
            data.reset(); // error message has already been logged
            return {};
        }
    }
    return data;
}

std::shared_ptr<EnumDefinition> OsdParser::findEnum(const QString& defName, const OsdParserInfo& info)
{
    for (const auto& def : info.enums) {
        if (def->name() == defName) {
            return def;
        }
    }

    info.error().nospace() << "Could not find enum definition with name '" << defName << "'.";
    return {};
}

QString OsdParser::readProperty(const QDomElement& elem, const QString& property, const QString& defaultVal)
{
    const QString attrib = elem.attribute(property);
    if (!attrib.isEmpty()) {
        return attrib;
    }
    // check for element now
    const QDomElement childElem = elem.firstChildElement(property);
    if (!elem.isNull()) {
        return elem.text();
    }

    return defaultVal;
}

std::unique_ptr<TaggedUnionDataInformation> OsdParser::taggedUnionFromXML(const QDomElement& xmlElem,
                                                                          const OsdParserInfo& info)
{
    TaggedUnionParsedData tpd(info);
    // can be null
    QDomElement defaultChildren = xmlElem.firstChildElement(PROPERTY_DEFAULT_CHILDREN()).firstChildElement();
    if (defaultChildren.isNull()) {
        info.info() << "No default fields specified, defaulting to none.";
    }
    tpd.defaultFields = std::make_unique<OsdChildrenParser>(info, defaultChildren);
    tpd.children = std::make_unique<OsdChildrenParser>(info, xmlElem.firstChildElement());
    // now handle alternatives
    QDomElement alternatives = xmlElem.firstChildElement(PROPERTY_ALTERNATIVES());
    if (alternatives.isNull()) {
        info.error() << "Missing <alternatives> element, tagged union cannot exist without at least one alternative.";
        return {};
    }

    int alternativeCount = 0;
    for (QDomElement elem = alternatives.firstChildElement(); !elem.isNull(); elem = elem.nextSiblingElement()) {
        ++alternativeCount;
    }
    tpd.alternatives.reserve(alternativeCount);

    for (QDomElement elem = alternatives.firstChildElement(); !elem.isNull(); elem = elem.nextSiblingElement()) {
        auto& alt = tpd.alternatives.emplace_back(TaggedUnionParsedData::Alternatives());
        alt.name = readProperty(elem, PROPERTY_STRUCT_NAME());
        QString selectIfStr = readProperty(elem, PROPERTY_SELECT_IF());
        QScriptValue selectIf = ParserUtils::functionSafeEval(info.engine, selectIfStr);
        if (!selectIf.isValid()) {
            selectIf = selectIfStr;
        }
        alt.selectIf = selectIf;
        if (elem.tagName() == TYPE_GROUP()) {
            alt.fields = std::make_shared<OsdChildrenParser>(info, elem.firstChildElement());
        } else {
            alt.fields = std::make_shared<SingleElementOsdChildrenParser>(info, elem);
        }
    }

    return DataInformationFactory::newTaggedUnion(tpd);
}

OsdChildrenParser::OsdChildrenParser(const OsdParserInfo& info, const QDomElement& firstChild)
    : mInfo(info)
    , mElem(firstChild)
{
}

OsdChildrenParser::~OsdChildrenParser() = default;

std::unique_ptr<DataInformation> OsdChildrenParser::next()
{
    Q_ASSERT(!mElem.isNull());
    // skip all known properties
    const QStringList allProperties = ALL_PROPERTIES();
    while (allProperties.contains(mElem.tagName())) {
        mElem = mElem.nextSiblingElement();
    }
    if (mElem.isNull()) {
        mInfo.warn() << "Reached end of fields, but next() was requested.";
        return {};
    }
    std::unique_ptr<DataInformation> ret = OsdParser::parseElement(mElem, mInfo);
    mElem = mElem.nextSiblingElement();
    return ret;
}

bool OsdChildrenParser::hasNext()
{
    if (mElem.isNull()) {
        return false;
    }
    const QStringList allProperties = ALL_PROPERTIES();
    while (allProperties.contains(mElem.tagName())) {
        mElem = mElem.nextSiblingElement(); // skip known properties
    }
    return !mElem.isNull();
}

void OsdChildrenParser::setParent(DataInformation* newParent)
{
    mInfo.parent = newParent;
}

SingleElementOsdChildrenParser::SingleElementOsdChildrenParser(const OsdParserInfo& info, const QDomElement& element)
    : OsdChildrenParser(info, element)
{
    if (mElem.isNull()) {
        info.warn() << "Null element passed to child parser.";
    }
}

SingleElementOsdChildrenParser::~SingleElementOsdChildrenParser() = default;

std::unique_ptr<DataInformation> SingleElementOsdChildrenParser::next()
{
    Q_ASSERT(!mParsed);
    mParsed = true;
    return OsdParser::parseElement(mElem, mInfo);
}

bool SingleElementOsdChildrenParser::hasNext()
{
    return !mParsed && !mElem.isNull();
}
