/*
 * MIT License
 *
 * Copyright (c) 2020 Alexey Edelev <semlanik@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_NESTEDMESSAGES_TEST_MULTI
#   include "nestedmessages.qpb.h"
#   include "externalpackage.qpb.h"
#endif

#include <QtTest/QtTest>
#include <qtprotobuftestscommon.h>
#include <QProtobufSerializer>

using namespace qtprotobufnamespace::tests::nested;

class QtProtobufNestedTest : public QObject
{
    Q_OBJECT
public:
    QtProtobufNestedTest()
    {
        qRegisterProtobufTypes();
    }
private slots:
    void init()
    {
        m_serializer.reset(new QProtobufSerializer);
    }
    void NestedMessageTest();
    void SimpleTest();
    void SerializationTest();
    void DeserializationTest();
    void NeighborTest();
    void NestedNoFieldsTest();
    void NestedCyclingTest();
private:
    std::unique_ptr<QProtobufSerializer> m_serializer;
};

void QtProtobufNestedTest::NestedMessageTest()
{
    qProtobufAssertMessagePropertyRegistered<NestedFieldMessage::NestedMessage, QtProtobuf::sint32>(1, "QtProtobuf::sint32", "testFieldInt");
}

void QtProtobufNestedTest::SimpleTest()
{
    const char *propertyName = "nested";

    qProtobufAssertMessagePropertyRegistered<NestedFieldMessage, NestedFieldMessage::NestedMessage*>(2, "qtprotobufnamespace::tests::nested::NestedFieldMessage::NestedMessage*", "nested");

    NestedFieldMessage test{10, {15}};
    QCOMPARE(test.nested().testFieldInt(), 15);

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

    qProtobufAssertMessagePropertyRegistered<NestedFieldMessage2::NestedMessageLevel1, NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*>(1, "qtprotobufnamespace::tests::nested::NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*", "nested");

    NestedFieldMessage2::NestedMessageLevel1 level1{{20}};
    QCOMPARE(level1.nested().testFieldInt(), 20);
    QVERIFY(level1.setProperty(propertyName, QVariant::fromValue<NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*>(new NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2{55})));
    QVERIFY(*(level1.property(propertyName).value<NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*>()) == NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2{55});
    QVERIFY(level1.nested() == NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2{55});

    qProtobufAssertMessagePropertyRegistered<NestedFieldMessage2, NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*>(3, "qtprotobufnamespace::tests::nested::NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*", "nested2");

    NestedFieldMessage2 test2{level1, {20}};
    QCOMPARE(test2.nested1().nested().testFieldInt(), 55);
    QCOMPARE(test2.nested2().testFieldInt(), 20);

    QVERIFY(test2.setProperty("nested1", QVariant::fromValue<NestedFieldMessage2::NestedMessageLevel1*>(new NestedFieldMessage2::NestedMessageLevel1{{65}})));
    QVERIFY(*(test2.property("nested1").value<NestedFieldMessage2::NestedMessageLevel1*>()) == NestedFieldMessage2::NestedMessageLevel1{{65}});
    QVERIFY(test2.nested1() == NestedFieldMessage2::NestedMessageLevel1{{65}});

    QVERIFY(test2.setProperty("nested2", QVariant::fromValue<NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*>(new NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2{75})));
    QVERIFY(*(test2.property("nested2").value<NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*>()) == NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2{75});
    QVERIFY(test2.nested2() == NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2{75});
}

void QtProtobufNestedTest::SerializationTest()
{
    NestedFieldMessage::NestedMessage nested{15};
    QByteArray result = nested.serialize(m_serializer.get());
    QCOMPARE(result.size(), 2);
    QCOMPARE(result.toHex().toStdString().c_str(), "081e");

    NestedFieldMessage test{10, nested};
    result = test.serialize(m_serializer.get());
    QVERIFY(result == QByteArray::fromHex("1202081e0814")
                || result == QByteArray::fromHex("08141202081e"));

    NestedFieldMessage2::NestedMessageLevel1 level1{{10}};
    result = level1.serialize(m_serializer.get());
    QCOMPARE(result.toHex().toStdString().c_str(), "0a020814");

    NestedFieldMessage2 test2{level1, {15}};
    result = test2.serialize(m_serializer.get());
    QVERIFY(result == QByteArray::fromHex("1a02081e12040a020814")
                || result == QByteArray::fromHex("12040a0208141a02081e"));

    NeighborNested neigbor{{15},{20}};
    result = neigbor.serialize(m_serializer.get());
    QVERIFY(result == QByteArray::fromHex("120208280a02081e")
                || result == QByteArray::fromHex("0a02081e12020828"));
}

void QtProtobufNestedTest::DeserializationTest()
{
    NestedFieldMessage::NestedMessage nested;

    nested.deserialize(m_serializer.get(), QByteArray::fromHex("081e"));
    QCOMPARE(nested.testFieldInt(), 15);

    NestedFieldMessage test;
    test.deserialize(m_serializer.get(), QByteArray::fromHex("1202081e0814"));
    QCOMPARE(test.nested().testFieldInt(), 15);

    NestedFieldMessage2::NestedMessageLevel1 level1;
    level1.deserialize(m_serializer.get(), QByteArray::fromHex("0a020814"));
    QCOMPARE(level1.nested().testFieldInt(), 10);

    NestedFieldMessage2 test2;
    test2.deserialize(m_serializer.get(), QByteArray::fromHex("1a02081e12040a020814"));
    QCOMPARE(test2.nested1().nested().testFieldInt(), 10);
    QCOMPARE(test2.nested2().testFieldInt(), 15);

    NeighborNested neigbor;
    neigbor.deserialize(m_serializer.get(), QByteArray::fromHex("120208280a02081e"));
    QCOMPARE(neigbor.neighborNested().testFieldInt(), 15);
    QCOMPARE(neigbor.neighborNested2().testFieldInt(), 20);
}


void QtProtobufNestedTest::NeighborTest()
{
    qProtobufAssertMessagePropertyRegistered<NeighborNested, NestedFieldMessage::NestedMessage*>(1, "qtprotobufnamespace::tests::nested::NestedFieldMessage::NestedMessage*", "neighborNested");
    qProtobufAssertMessagePropertyRegistered<NeighborNested, NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*>(2, "qtprotobufnamespace::tests::nested::NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*", "neighborNested2");

    NeighborNested test{{15},{20}};
    QCOMPARE(test.neighborNested().testFieldInt(), 15);
    QCOMPARE(test.neighborNested2().testFieldInt(), 20);

    const char *propertyName = "neighborNested";

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

    propertyName = "neighborNested2";

    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*>(new NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2{75})));
    QVERIFY(*(test.property(propertyName).value<NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*>()) == NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2{75});
    QVERIFY(test.neighborNested2() == NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2{75});
}

void QtProtobufNestedTest::NestedNoFieldsTest()
{
    qProtobufAssertMessagePropertyRegistered<NestedNoFields::Nested, QtProtobuf::sint32>(1, "QtProtobuf::sint32", "testFieldInt");

    NestedNoFields::Nested test{15};
    QCOMPARE(test.testFieldInt(), 15);

    const char *propertyName = "testFieldInt";

    QVERIFY(test.setProperty(propertyName, 55));
    QVERIFY(test.property(propertyName).value<QtProtobuf::sint32>() == 55);
    QVERIFY(test.testFieldInt() == 55);
}

void QtProtobufNestedTest::NestedCyclingTest()
{
    qProtobufAssertMessagePropertyRegistered<NestedCyclingA::NestedCyclingB, qtprotobufnamespace::tests::nested::NestedCyclingAA::NestedCyclingBB*>(1, "qtprotobufnamespace::tests::nested::NestedCyclingAA::NestedCyclingBB*", "testField");
    qProtobufAssertMessagePropertyRegistered<NestedCyclingAA::NestedCyclingBB, qtprotobufnamespace::tests::nested::NestedCyclingA::NestedCyclingB*>(1, "qtprotobufnamespace::tests::nested::NestedCyclingA::NestedCyclingB*", "testField");

    NestedCyclingA::NestedCyclingB test;
    NestedCyclingAA::NestedCyclingBB test2;
    test.setTestField(test2);
    test2.setTestField(test);
}

QTEST_MAIN(QtProtobufNestedTest)
#include "nestedtypes.moc"
