/*
 * MIT License
 *
 * Copyright (c) 2022 Alexey Edelev <semlanik@gmail.com>, Viktor Kopp <vifactor@gmail.com>
 *
 * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this
 * software and associated documentation files (the "Software"), to deal in the Software
 * without restriction, including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies
 * or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#ifndef QT_PROTOBUF_BASIC_MULTI_TEST
#include "nopackage.qpb.h"
#include "nopackageexternal.qpb.h"
#endif

#include <QMetaProperty>
#include <QSignalSpy>
#include <QtTest/QtTest>

#include <qtprotobuftestscommon.h>

class QtProtobufNoPackageTypesGenerationTest : public QObject
{
    Q_OBJECT
public:
    QtProtobufNoPackageTypesGenerationTest()
    {
        qRegisterProtobufTypes();
    }
private slots:
    void NoPackageEmptyMessageMessageTest();
    void NoPackageSimpleIntMessageTest();
    void NoPackageEnumTest();
    void NoPackageExternalTest();
    void NoPackageMessageTest();
};

void QtProtobufNoPackageTypesGenerationTest::NoPackageEmptyMessageMessageTest()
{
    QCOMPARE(::EmptyMessage::propertyOrdering.size(), 0);
    QCOMPARE(::EmptyMessage::staticMetaObject.propertyCount(), 1);
}

void QtProtobufNoPackageTypesGenerationTest::NoPackageSimpleIntMessageTest()
{
    const char *propertyName = "testFieldInt" QT_PROTOBUF_PROPERTY_SUFFIX;
    qProtobufAssertMessagePropertyRegistered<::SimpleIntMessage, QtProtobuf::int32>(1, "QtProtobuf::int32", propertyName);

    ::SimpleIntMessage test;
    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::int32>(1)));
    QCOMPARE(test.property(propertyName).value<QtProtobuf::int32>(), 1);
    QCOMPARE(test.testFieldInt(), 1);

    QCOMPARE(::SimpleIntMessage::TestFieldIntProtoFieldNumber, 1);
}

void QtProtobufNoPackageTypesGenerationTest::NoPackageEnumTest()
{
    QVERIFY(::TestEnumGadget::staticMetaObject.enumeratorCount() > 0);
    QMetaEnum testEnum;
    for (int i = 0; i < ::TestEnumGadget::staticMetaObject.enumeratorCount(); i++) {
        QMetaEnum tmp = ::TestEnumGadget::staticMetaObject.enumerator(i);
        if (QString(tmp.name()) == QString("TestEnum")) {
            testEnum = tmp;
            break;
        }
    }
    QCOMPARE(testEnum.keyCount(), 4);
    QVERIFY(testEnum.isValid());
    QCOMPARE(testEnum.key(0), "LOCAL_ENUM_VALUE0");
    QCOMPARE(testEnum.key(1), "LOCAL_ENUM_VALUE1");
    QCOMPARE(testEnum.key(2), "LOCAL_ENUM_VALUE2");
    QCOMPARE(testEnum.key(3), "LOCAL_ENUM_VALUE3");

    QCOMPARE(testEnum.value(0), 0);
    QCOMPARE(testEnum.value(1), 1);
    QCOMPARE(testEnum.value(2), 2);
    QCOMPARE(testEnum.value(3), 5);
}

void QtProtobufNoPackageTypesGenerationTest::NoPackageExternalTest()
{
    const char *propertyName = "testField";
    qProtobufAssertMessagePropertyRegistered<NoPackageExternalMessage, SimpleIntMessageExt*>(1, "SimpleIntMessageExt*", propertyName);

    NoPackageExternalMessage test;
    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<SimpleIntMessageExt*>(new SimpleIntMessageExt{42})));
    QCOMPARE(test.property(propertyName).value<SimpleIntMessageExt*>()->testFieldInt(), 42);
    QCOMPARE(test.testField().testFieldInt(), 42);
}

void QtProtobufNoPackageTypesGenerationTest::NoPackageMessageTest()
{
    const char *propertyName = "testField";
    qProtobufAssertMessagePropertyRegistered<NoPackageMessage, SimpleIntMessage*>(1, "SimpleIntMessage*", propertyName);

    NoPackageMessage test;
    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<SimpleIntMessage*>(new SimpleIntMessage{42})));
    QCOMPARE(test.property(propertyName).value<SimpleIntMessage*>()->testFieldInt(), 42);
    QCOMPARE(test.testField().testFieldInt(), 42);
}

QTEST_MAIN(QtProtobufNoPackageTypesGenerationTest)
#include "nopackagetypes.moc"
