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

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

#include <qtprotobuftestscommon.h>

class QtProtobufRepeatedTypesGenerationTest : public QObject
{
    Q_OBJECT
public:
    QtProtobufRepeatedTypesGenerationTest()
    {
        qRegisterProtobufTypes();
    }
private slots:
    void RepeatedStringMessageTest();
    void RepeatedIntMessageTest();
    void RepeatedDoubleMessageTest();
    void RepeatedFloatMessageTest();
    void RepeatedBytesMessageTest();
    void RepeatedSIntMessageTest();
    void RepeatedUIntMessageTest();
    void RepeatedInt64MessageTest();
    void RepeatedSInt64MessageTest();
    void RepeatedUInt64MessageTest();
    void RepeatedFixedIntMessageTest();
    void RepeatedFixedInt64MessageTest();
    void RepeatedSFixedIntMessageTest();
    void RepeatedSFixedInt64MessageTest();

    void MoveOperatorRepeatedTest();
    void RepeatedComplexMessageCompareTest();
};

using namespace qtprotobufnamespace::tests;

void QtProtobufRepeatedTypesGenerationTest::RepeatedStringMessageTest()
{
    const char *propertyName = "testRepeatedString";
    qProtobufAssertMessagePropertyRegistered<RepeatedStringMessage, QStringList>(1, "QStringList", propertyName);

    RepeatedStringMessage test;
    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<QStringList>({"Text", "tryam"})));
    QVERIFY(test.property(propertyName).value<QStringList>() == QStringList({"Text", "tryam"}));
    QVERIFY(test.testRepeatedString() == QStringList({"Text", "tryam"}));
}

void QtProtobufRepeatedTypesGenerationTest::RepeatedIntMessageTest()
{
    const char *propertyName = "testRepeatedInt";
    qProtobufAssertMessagePropertyRegistered<RepeatedIntMessage, QtProtobuf::int32List>(1, "QtProtobuf::int32List", propertyName);

    RepeatedIntMessage 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.testRepeatedInt() == QtProtobuf::int32List({1, 2, 3, 4, 5}));

    test.testRepeatedInt().append(66);
    QVERIFY(test.testRepeatedInt() == QtProtobuf::int32List({1, 2, 3, 4, 5, 66}));

    test.testRepeatedInt().pop_back();
    QVERIFY(test.testRepeatedInt() == QtProtobuf::int32List({1, 2, 3, 4, 5}));
}

void QtProtobufRepeatedTypesGenerationTest::RepeatedDoubleMessageTest()
{
    const char *propertyName = "testRepeatedDouble";
    qProtobufAssertMessagePropertyRegistered<RepeatedDoubleMessage, QtProtobuf::DoubleList>(1, "QtProtobuf::DoubleList", propertyName);

    RepeatedDoubleMessage test;
    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::DoubleList>({1.0, 2.3, 3, 4.7, 5.9})));
    QVERIFY(test.property(propertyName).value<QtProtobuf::DoubleList>() == QtProtobuf::DoubleList({1.0, 2.3, 3, 4.7, 5.9}));
    QVERIFY(test.testRepeatedDouble() == QtProtobuf::DoubleList({1.0, 2.3, 3, 4.7, 5.9}));

    test.testRepeatedDouble().append(6.6);
    QVERIFY(test.testRepeatedDouble() == QtProtobuf::DoubleList({1.0, 2.3, 3, 4.7, 5.9, 6.6}));

    test.testRepeatedDouble().pop_back();
    QVERIFY(test.testRepeatedDouble() == QtProtobuf::DoubleList({1.0, 2.3, 3, 4.7, 5.9}));
}

void QtProtobufRepeatedTypesGenerationTest::RepeatedFloatMessageTest()
{
    const char *propertyName = "testRepeatedFloat";
    qProtobufAssertMessagePropertyRegistered<RepeatedFloatMessage, QtProtobuf::FloatList>(1, "QtProtobuf::FloatList", propertyName);

    RepeatedFloatMessage test;
    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::FloatList>({1.0f, 2.3f, 3, 4.7f, 5.9f})));
    QVERIFY(test.property(propertyName).value<QtProtobuf::FloatList>() == QtProtobuf::FloatList({1.0f, 2.3f, 3, 4.7f, 5.9f}));
    QVERIFY(test.testRepeatedFloat() == QtProtobuf::FloatList({1.0f, 2.3f, 3, 4.7f, 5.9f}));

    test.testRepeatedFloat().append(6.6f);
    QVERIFY(test.testRepeatedFloat() == QtProtobuf::FloatList({1.0f, 2.3f, 3, 4.7f, 5.9f, 6.6f}));

    test.testRepeatedFloat().pop_back();
    QVERIFY(test.testRepeatedFloat() == QtProtobuf::FloatList({1.0f, 2.3f, 3, 4.7f, 5.9f}));
}

void QtProtobufRepeatedTypesGenerationTest::RepeatedBytesMessageTest()
{
    const char *propertyName = "testRepeatedBytes";
    qProtobufAssertMessagePropertyRegistered<RepeatedBytesMessage, QByteArrayList>(1, "QByteArrayList", propertyName);

    QByteArrayList bList;
    bList << "\x01\x02\x03\x04\x05";
    bList << "\x01\x05\x03\x04\x03";

    RepeatedBytesMessage test;
    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<QByteArrayList>(bList)));
    QVERIFY(test.property(propertyName).value<QByteArrayList>() == bList);
    QVERIFY(test.testRepeatedBytes() == bList);

    bList << "\x01\x05\x03\x03";
    test.testRepeatedBytes() << "\x01\x05\x03\x03";
    QVERIFY(test.testRepeatedBytes() == bList);

    bList.pop_back();
    test.testRepeatedBytes().pop_back();
    QVERIFY(test.testRepeatedBytes() == bList);
}

void QtProtobufRepeatedTypesGenerationTest::RepeatedSIntMessageTest()
{
    const char *propertyName = "testRepeatedInt";
    qProtobufAssertMessagePropertyRegistered<RepeatedSIntMessage, QtProtobuf::sint32List>(1, "QtProtobuf::sint32List", propertyName);

    RepeatedSIntMessage test;
    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::sint32List>({1, 2, 3, 4, 5})));
    QVERIFY(test.property(propertyName).value<QtProtobuf::sint32List>() == QtProtobuf::sint32List({1, 2, 3, 4, 5}));
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sint32List({1, 2, 3, 4, 5}));

    test.testRepeatedInt() << 6;
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sint32List({1, 2, 3, 4, 5, 6}));

    test.testRepeatedInt().pop_back();
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sint32List({1, 2, 3, 4, 5}));
}

void QtProtobufRepeatedTypesGenerationTest::RepeatedUIntMessageTest()
{
    const char *propertyName = "testRepeatedInt";
    qProtobufAssertMessagePropertyRegistered<RepeatedUIntMessage, QtProtobuf::uint32List>(1, "QtProtobuf::uint32List", propertyName);

    RepeatedUIntMessage test;
    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::uint32List>({1, 2, 3, 4, 5})));
    QVERIFY(test.property(propertyName).value<QtProtobuf::uint32List>() == QtProtobuf::uint32List({1, 2, 3, 4, 5}));
    QVERIFY(test.testRepeatedInt() == QtProtobuf::uint32List({1, 2, 3, 4, 5}));

    test.testRepeatedInt().append(6);
    QVERIFY(test.testRepeatedInt() == QtProtobuf::uint32List({1, 2, 3, 4, 5,6}));

    test.testRepeatedInt().pop_back();
    QVERIFY(test.testRepeatedInt() == QtProtobuf::uint32List({1, 2, 3, 4, 5}));
}

void QtProtobufRepeatedTypesGenerationTest::RepeatedInt64MessageTest()
{
    const char *propertyName = "testRepeatedInt";
    qProtobufAssertMessagePropertyRegistered<RepeatedInt64Message, QtProtobuf::int64List>(1, "QtProtobuf::int64List", propertyName);

    RepeatedInt64Message test;
    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::int64List>({1, 2, 3, 4, 5})));
    QVERIFY(test.property(propertyName).value<QtProtobuf::int64List>() == QtProtobuf::int64List({1, 2, 3, 4, 5}));
    QVERIFY(test.testRepeatedInt() == QtProtobuf::int64List({1, 2, 3, 4, 5}));

    test.testRepeatedInt().append(69);
    QVERIFY(test.testRepeatedInt() == QtProtobuf::int64List({1, 2, 3, 4, 5, 69}));

    test.testRepeatedInt().pop_back();
    QVERIFY(test.testRepeatedInt() == QtProtobuf::int64List({1, 2, 3, 4, 5}));
}

void QtProtobufRepeatedTypesGenerationTest::RepeatedSInt64MessageTest()
{
    const char *propertyName = "testRepeatedInt";
    qProtobufAssertMessagePropertyRegistered<RepeatedSInt64Message, QtProtobuf::sint64List>(1, "QtProtobuf::sint64List", propertyName);

    RepeatedSInt64Message test;
    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::sint64List>({1, 2, 3, 4, 5})));
    QVERIFY(test.property(propertyName).value<QtProtobuf::sint64List>() == QtProtobuf::sint64List({1, 2, 3, 4, 5}));
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sint64List({1, 2, 3, 4, 5}));

    test.testRepeatedInt() << 96;
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sint64List({1, 2, 3, 4, 5, 96}));

    test.testRepeatedInt().pop_back();
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sint64List({1, 2, 3, 4, 5}));
}

void QtProtobufRepeatedTypesGenerationTest::RepeatedUInt64MessageTest()
{
    const char *propertyName = "testRepeatedInt";
    qProtobufAssertMessagePropertyRegistered<RepeatedUInt64Message, QtProtobuf::uint64List>(1, "QtProtobuf::uint64List", propertyName);

    RepeatedUInt64Message test;
    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::uint64List>({1, 2, 3, 4, 5})));
    QVERIFY(test.property(propertyName).value<QtProtobuf::uint64List>() == QtProtobuf::uint64List({1, 2, 3, 4, 5}));
    QVERIFY(test.testRepeatedInt() == QtProtobuf::uint64List({1, 2, 3, 4, 5}));

    test.testRepeatedInt().append(96);
    QVERIFY(test.testRepeatedInt() == QtProtobuf::uint64List({1, 2, 3, 4, 5, 96}));

    test.testRepeatedInt().pop_back();
    QVERIFY(test.testRepeatedInt() == QtProtobuf::uint64List({1, 2, 3, 4, 5}));
}

void QtProtobufRepeatedTypesGenerationTest::RepeatedFixedIntMessageTest()
{
    const char *propertyName = "testRepeatedInt";
    qProtobufAssertMessagePropertyRegistered<RepeatedFixedIntMessage, QtProtobuf::fixed32List>(1, "QtProtobuf::fixed32List", propertyName);

    RepeatedFixedIntMessage test;
    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::fixed32List>({1, 2, 3, 4, 5})));
    QVERIFY(test.property(propertyName).value<QtProtobuf::fixed32List>() == QtProtobuf::fixed32List({1, 2, 3, 4, 5}));
    QVERIFY(test.testRepeatedInt() == QtProtobuf::fixed32List({1, 2, 3, 4, 5}));

    test.testRepeatedInt() << 0;
    QVERIFY(test.testRepeatedInt() == QtProtobuf::fixed32List({1, 2, 3, 4, 5, 0}));

    test.testRepeatedInt().pop_back();
    QVERIFY(test.testRepeatedInt() == QtProtobuf::fixed32List({1, 2, 3, 4, 5}));
}

void QtProtobufRepeatedTypesGenerationTest::RepeatedFixedInt64MessageTest()
{
    const char *propertyName = "testRepeatedInt";
    qProtobufAssertMessagePropertyRegistered<RepeatedFixedInt64Message, QtProtobuf::fixed64List>(1, "QtProtobuf::fixed64List", propertyName);

    RepeatedFixedInt64Message test;
    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::fixed64List>({1, 2, 3, 4, 5})));
    QVERIFY(test.property(propertyName).value<QtProtobuf::fixed64List>() == QtProtobuf::fixed64List({1, 2, 3, 4, 5}));
    QVERIFY(test.testRepeatedInt() == QtProtobuf::fixed64List({1, 2, 3, 4, 5}));

    test.testRepeatedInt() << 0;
    QVERIFY(test.testRepeatedInt() == QtProtobuf::fixed64List({1, 2, 3, 4, 5, 0}));

    test.testRepeatedInt().pop_back();
    QVERIFY(test.testRepeatedInt() == QtProtobuf::fixed64List({1, 2, 3, 4, 5}));
}

void QtProtobufRepeatedTypesGenerationTest::RepeatedSFixedIntMessageTest()
{
    const char *propertyName = "testRepeatedInt";
    qProtobufAssertMessagePropertyRegistered<RepeatedSFixedIntMessage, QtProtobuf::sfixed32List>(1, "QtProtobuf::sfixed32List", propertyName);

    RepeatedSFixedIntMessage test;
    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::sfixed32List>({1, 2, 3, 4, 5})));
    QVERIFY(test.property(propertyName).value<QtProtobuf::sfixed32List>() == QtProtobuf::sfixed32List({1, 2, 3, 4, 5}));
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sfixed32List({1, 2, 3, 4, 5}));

    test.testRepeatedInt() << 0;
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sfixed32List({1, 2, 3, 4, 5, 0}));

    test.testRepeatedInt().pop_back();
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sfixed32List({1, 2, 3, 4, 5}));
}

void QtProtobufRepeatedTypesGenerationTest::RepeatedSFixedInt64MessageTest()
{
    const char *propertyName = "testRepeatedInt";
    qProtobufAssertMessagePropertyRegistered<RepeatedSFixedInt64Message, QtProtobuf::sfixed64List>(1, "QtProtobuf::sfixed64List", propertyName);

    RepeatedSFixedInt64Message test;
    QVERIFY(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::sfixed64List>({1, 2, 3, 4, 5})));
    QVERIFY(test.property(propertyName).value<QtProtobuf::sfixed64List>() == QtProtobuf::sfixed64List({1, 2, 3, 4, 5}));
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sfixed64List({1, 2, 3, 4, 5}));

    test.testRepeatedInt() << 0;
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sfixed64List({1, 2, 3, 4, 5, 0}));

    test.testRepeatedInt().pop_back();
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sfixed64List({1, 2, 3, 4, 5}));
}

void QtProtobufRepeatedTypesGenerationTest::MoveOperatorRepeatedTest()
{
    const char *propertyName = "testRepeatedInt";
    RepeatedIntMessage test;
    RepeatedIntMessage test2{{55,44,11,33}};

    QSignalSpy updateSpy(&test, &RepeatedIntMessage::testRepeatedIntChanged);
    QSignalSpy movedUpdateSpy(&test2, &RepeatedIntMessage::testRepeatedIntChanged);

    RepeatedIntMessage test3(std::move(test2));
    test2.setTestRepeatedInt({55,44,11,35});

    test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::int32List>({55}));
    test.setTestRepeatedInt({44});
    test = std::move(test2);
    QCOMPARE(QtProtobuf::int32List({55,44,11,35}), test.testRepeatedInt());
    QVERIFY(test2.testRepeatedInt().isEmpty());
    QCOMPARE(3, updateSpy.count());
    QCOMPARE(3, movedUpdateSpy.count());
}

void QtProtobufRepeatedTypesGenerationTest::RepeatedComplexMessageCompareTest()
{
    QSharedPointer<ComplexMessage> msg1(new ComplexMessage(10, {"qwerty"}));
    QSharedPointer<ComplexMessage> msg2(new ComplexMessage(20, {"ytrewq"}));

    QSharedPointer<ComplexMessage> msg3(new ComplexMessage(10, {"qwerty"}));
    QSharedPointer<ComplexMessage> msg4(new ComplexMessage(20, {"ytrewq"}));

    QVERIFY(*msg1 == *msg3);
    QVERIFY(*msg2 == *msg4);

    RepeatedComplexMessage test1 = RepeatedComplexMessage({msg1, msg2});
    RepeatedComplexMessage test2 = RepeatedComplexMessage({msg3, msg4});
    RepeatedComplexMessage test3 = RepeatedComplexMessage({msg4, msg3});
    QVERIFY(test1 == test2);
    QVERIFY(!(test3 == test2));
}

QTEST_MAIN(QtProtobufRepeatedTypesGenerationTest)
#include "repeatedtypes.moc"
