/*
 * 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 "externalpackageconsumer.qpb.h"
#    include "externalpackage.qpb.h"
#endif

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

#include <qtprotobuftestscommon.h>

class QtProtobufExternalPackageGenerationTest : public QObject
{
    Q_OBJECT
public:
    QtProtobufExternalPackageGenerationTest()
    {
        qRegisterProtobufTypes();
    }
private slots:
    void RepeatedExternalComplexMessageTest();
    void ExternalEnumMessageTest();
    void ExternalComplexMessageTest();
    void NestedMessageTest();
};

using namespace qtprotobufnamespace::tests;

void QtProtobufExternalPackageGenerationTest::RepeatedExternalComplexMessageTest()
{
    const char *propertyName = "testExternalComplexData";
    qProtobufAssertMessagePropertyRegistered<RepeatedExternalComplexMessage, qtprotobufnamespace1::externaltests::ExternalComplexMessageRepeated>(
                1, "qtprotobufnamespace1::externaltests::ExternalComplexMessageRepeated", propertyName);

    qtprotobufnamespace1::externaltests::ExternalInt32Message complexMessage;
    complexMessage.setLocalList({1, 2, 3, 4, 5});

    QSharedPointer<qtprotobufnamespace1::externaltests::ExternalComplexMessage> externalMessage(new qtprotobufnamespace1::externaltests::ExternalComplexMessage);
    externalMessage->setTestFieldInt(complexMessage);

    qtprotobufnamespace1::externaltests::ExternalComplexMessageRepeated complexMessageList;
    complexMessageList << externalMessage;

    RepeatedExternalComplexMessage test;
    QVERIFY(test.setProperty(propertyName, QVariant::fromValue(complexMessageList)));
    QVERIFY(test.property(propertyName).value<qtprotobufnamespace1::externaltests::ExternalComplexMessageRepeated>() == complexMessageList);
    QVERIFY(test.testExternalComplex() == complexMessageList);
}

void QtProtobufExternalPackageGenerationTest::ExternalEnumMessageTest()
{
    using ExternalGlobalEnums = qtprotobufnamespace1::externaltests::ExternalTestEnumGadget;

    const char *propertyName = "externalEnum";
    qProtobufAssertMessagePropertyRegistered<SimpleExternalEnumMessage, ExternalGlobalEnums::ExternalTestEnum>(1, "qtprotobufnamespace1::externaltests::ExternalTestEnumGadget::ExternalTestEnum", propertyName);

    SimpleExternalEnumMessage test;
    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<ExternalGlobalEnums::ExternalTestEnum>(ExternalGlobalEnums::EXTERNAL_TEST_ENUM_VALUE4)));
    QCOMPARE(test.property(propertyName).value<ExternalGlobalEnums::ExternalTestEnum>(), ExternalGlobalEnums::EXTERNAL_TEST_ENUM_VALUE4);
    QCOMPARE(test.externalEnum(), ExternalGlobalEnums::EXTERNAL_TEST_ENUM_VALUE4);
}

void QtProtobufExternalPackageGenerationTest::ExternalComplexMessageTest()
{
    const char *propertyName = "localList";
    qProtobufAssertMessagePropertyRegistered<qtprotobufnamespace1::externaltests::ExternalInt32Message, QtProtobuf::int32List>(
                1, "QtProtobuf::int32List", propertyName);

    qtprotobufnamespace1::externaltests::ExternalInt32Message test;
    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::int32List>({1, 2, 3, 4, 5})));
    QVERIFY(test.property(propertyName).value<QtProtobuf::int32List>() == QtProtobuf::int32List({1, 2, 3, 4, 5}));
    QVERIFY(test.localList() == QtProtobuf::int32List({1, 2, 3, 4, 5}));
}

void QtProtobufExternalPackageGenerationTest::NestedMessageTest()
{
    qProtobufAssertMessagePropertyRegistered<NestedExternal, qtprotobufnamespace1::externaltests::NestedFieldMessage::NestedMessage*>(1, "qtprotobufnamespace1::externaltests::NestedFieldMessage::NestedMessage*", "externalNested");

    NestedExternal test{{15}};
    QCOMPARE(test.externalNested().field(), 15);

    const char *propertyName = "externalNested";

    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<qtprotobufnamespace1::externaltests::NestedFieldMessage::NestedMessage*>(new qtprotobufnamespace1::externaltests::NestedFieldMessage::NestedMessage{55})));
    QVERIFY(*(test.property(propertyName).value<qtprotobufnamespace1::externaltests::NestedFieldMessage::NestedMessage*>()) == qtprotobufnamespace1::externaltests::NestedFieldMessage::NestedMessage{55});
    QVERIFY(test.externalNested() == qtprotobufnamespace1::externaltests::NestedFieldMessage::NestedMessage{55});
}

QTEST_MAIN(QtProtobufExternalPackageGenerationTest)
#include "externalpackage.moc"
