/*
 * MIT License
 *
 * Copyright (c) 2022 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.
 */

#include "basicmessages.qpb.h"
#include "repeatedmessages.qpb.h"

#include <QObject>

#include <QtTest/QtTest>
#include <QProtobufSerializer>

#include <limits>

class QtProtobufRepeatedTypesDeserializationTest : public QObject
{
    Q_OBJECT
public:
    QtProtobufRepeatedTypesDeserializationTest()
    {
        qRegisterProtobufTypes();
    }
private slots:
    void init()
    {
        serializer.reset(new QProtobufSerializer);
    }

    void RepeatedStringMessageTest();
    void RepeatedBytesMessageTest();
    void RepeatedFloatMessageTest();
    void RepeatedDoubleMessageTest();
    void RepeatedIntMessageTest();
    void RepeatedSIntMessageTest();
    void RepeatedUIntMessageTest();
    void RepeatedInt64MessageTest();
    void RepeatedSInt64MessageTest();
    void RepeatedUInt64MessageTest();
    void RepeatedFixedIntMessageTest();
    void RepeatedSFixedIntMessageTest();
    void RepeatedFixedInt64MessageTest();
    void RepeatedSFixedInt64MessageTest();
    void RepeatedComplexMessageTest();
private:
    std::unique_ptr<QProtobufSerializer> serializer;
};

using namespace qtprotobufnamespace::tests;

void QtProtobufRepeatedTypesDeserializationTest::RepeatedStringMessageTest()
{
    RepeatedStringMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("0a04616161610a0562626262620a036363630a066464646464640a056565656565"));
    QCOMPARE(5, test.testRepeatedString().count());
    QVERIFY(test.testRepeatedString() == QStringList({"aaaa","bbbbb","ccc","dddddd","eeeee"}));
}

void QtProtobufRepeatedTypesDeserializationTest::RepeatedBytesMessageTest()
{
    RepeatedBytesMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("0a060102030405060a04ffffffff0a05eaeaeaeaea0a06010203040506"));
    QCOMPARE(4, test.testRepeatedBytes().count());
    QVERIFY(test.testRepeatedBytes() == QByteArrayList({QByteArray::fromHex("010203040506"),
                                                             QByteArray::fromHex("ffffffff"),
                                                             QByteArray::fromHex("eaeaeaeaea"),
                                                             QByteArray::fromHex("010203040506")}));
    //TODO: Serialization for list types works partially incorrect because of appending of values to existing
    //Need to make decision if deserialize should reset all protobuf properties or not
    RepeatedBytesMessage test2;
    test2.deserialize(serializer.get(), QByteArray::fromHex("0a060102030405060a000a05eaeaeaeaea0a06010203040506"));
    QCOMPARE(4, test2.testRepeatedBytes().count());
    QVERIFY(test2.testRepeatedBytes() == QByteArrayList({QByteArray::fromHex("010203040506"),
                                                             QByteArray::fromHex(""),
                                                             QByteArray::fromHex("eaeaeaeaea"),
                                                             QByteArray::fromHex("010203040506")}));
}

void QtProtobufRepeatedTypesDeserializationTest::RepeatedFloatMessageTest()
{
    RepeatedFloatMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("0a14cdcccc3e9a99993f0000003f3333b33f9a99193f"));
    QCOMPARE(5, test.testRepeatedFloat().count());
    QVERIFY(test.testRepeatedFloat() == QtProtobuf::FloatList({0.4f, 1.2f, 0.5f, 1.4f, 0.6f}));
}

void QtProtobufRepeatedTypesDeserializationTest::RepeatedDoubleMessageTest()
{
    RepeatedDoubleMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("0a289a9999999999b93f9a9999999999c93f333333333333d33f9a9999999999d93f000000000000e03f"));
    QCOMPARE(5, test.testRepeatedDouble().count());
    QVERIFY(test.testRepeatedDouble() == QtProtobuf::DoubleList({0.1, 0.2, 0.3, 0.4, 0.5}));
}

void QtProtobufRepeatedTypesDeserializationTest::RepeatedIntMessageTest()
{
    RepeatedIntMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("0a1101c102b1fcfbff0fedc207fdffffff0f03"));
    QCOMPARE(6, test.testRepeatedInt().count());
    QVERIFY(test.testRepeatedInt() == QtProtobuf::int32List({1, 321, -65999, 123245, -3, 3}));

    RepeatedIntMessage test2;
    test2.deserialize(serializer.get(), QByteArray::fromHex("0a1b01c102b1fcfbffffffffffff01edc207fdffffffffffffffff0103"));
    QCOMPARE(6, test2.testRepeatedInt().count());
    QVERIFY(test2.testRepeatedInt() == QtProtobuf::int32List({1, 321, -65999, 123245, -3, 3}));
}

void QtProtobufRepeatedTypesDeserializationTest::RepeatedSIntMessageTest()
{
    RepeatedSIntMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("0a0b0282059d8708da850f0506"));
    QCOMPARE(6, test.testRepeatedInt().count());
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sint32List({1, 321, -65999, 123245, -3, 3}));
}

void QtProtobufRepeatedTypesDeserializationTest::RepeatedUIntMessageTest()
{
    RepeatedUIntMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("0a0a01c102cf8304edc20703"));
    QCOMPARE(5, test.testRepeatedInt().count());
    QVERIFY(test.testRepeatedInt() == QtProtobuf::uint32List({1, 321, 65999, 123245, 3}));
}

void QtProtobufRepeatedTypesDeserializationTest::RepeatedInt64MessageTest()
{
    RepeatedInt64Message test;
    test.deserialize(serializer.get(), QByteArray::fromHex("0a1f01c102b1fcfbffffffffffff01b3c3cab6d8e602fdffffffffffffffff0103"));
    QCOMPARE(6, test.testRepeatedInt().count());
    QVERIFY(test.testRepeatedInt() == QtProtobuf::int64List({1, 321, -65999, 12324523123123, -3, 3}));
}

void QtProtobufRepeatedTypesDeserializationTest::RepeatedSInt64MessageTest()
{
    RepeatedSInt64Message test;
    test.deserialize(serializer.get(), QByteArray::fromHex("0a0f0282059d8708e68695edb0cd050506"));
    QCOMPARE(6, test.testRepeatedInt().count());
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sint64List({1, 321, -65999, 12324523123123, -3, 3}));
}

void QtProtobufRepeatedTypesDeserializationTest::RepeatedUInt64MessageTest()
{
    RepeatedUInt64Message test;
    test.deserialize(serializer.get(), QByteArray::fromHex("0a1301c102cf8304edc207d28b9fda82dff6da0103"));
    QCOMPARE(6, test.testRepeatedInt().count());
    QVERIFY(test.testRepeatedInt() == QtProtobuf::uint64List({1, 321, 65999, 123245, 123245324235425234, 3}));
}

void QtProtobufRepeatedTypesDeserializationTest::RepeatedFixedIntMessageTest()
{
    RepeatedFixedIntMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("0a180100000041010000cf010100ab0ebc000300000003000000"));
    QCOMPARE(6, test.testRepeatedInt().count());
    QVERIFY(test.testRepeatedInt() == QtProtobuf::fixed32List({1, 321, 65999, 12324523, 3, 3}));
}

void QtProtobufRepeatedTypesDeserializationTest::RepeatedSFixedIntMessageTest()
{
    RepeatedSFixedIntMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("0a18010000004101000031fefeffab0ebc00fdffffff03000000"));
    QCOMPARE(6, test.testRepeatedInt().count());
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sfixed32List({1, 321, -65999, 12324523, -3, 3}));
}

void QtProtobufRepeatedTypesDeserializationTest::RepeatedFixedInt64MessageTest()
{
    RepeatedFixedInt64Message test;
    test.deserialize(serializer.get(), QByteArray::fromHex("0a3001000000000000004101000000000000cf01010000000000d2c5472bf8dab50103000000000000000300000000000000"));
    QCOMPARE(6, test.testRepeatedInt().count());
    QVERIFY(test.testRepeatedInt() == QtProtobuf::fixed64List({1, 321, 65999, 123245324235425234, 3, 3}));
}

void QtProtobufRepeatedTypesDeserializationTest::RepeatedSFixedInt64MessageTest()
{
    RepeatedSFixedInt64Message test;
    test.deserialize(serializer.get(), QByteArray::fromHex("0a300100000000000000410100000000000031fefeffffffffffd2c5472bf8dab501fdffffffffffffff0300000000000000"));
    QCOMPARE(6, test.testRepeatedInt().count());
    QVERIFY(test.testRepeatedInt() == QtProtobuf::sfixed64List({1, 321, -65999, 123245324235425234, -3, 3}));
}

void QtProtobufRepeatedTypesDeserializationTest::RepeatedComplexMessageTest()
{
    RepeatedComplexMessage test;
    test.deserialize(serializer.get(), QByteArray::fromHex("0a0c0819120832067177657274790a0c0819120832067177657274790a0c081912083206717765727479"));
    QCOMPARE(3, test.testRepeatedComplex().count());
    QCOMPARE(25, test.testRepeatedComplex().at(0)->testFieldInt());
    QVERIFY(test.testRepeatedComplex().at(0)->testComplexField().testFieldString() == QString("qwerty"));
    QCOMPARE(25, test.testRepeatedComplex().at(1)->testFieldInt());
    QVERIFY(test.testRepeatedComplex().at(1)->testComplexField().testFieldString() == QString("qwerty"));
    QCOMPARE(25, test.testRepeatedComplex().at(2)->testFieldInt());
    QVERIFY(test.testRepeatedComplex().at(2)->testComplexField().testFieldString() == QString("qwerty"));

    test.deserialize(serializer.get(), QByteArray::fromHex("0a1508d3feffffffffffffff0112083206717765727479"));
    QVERIFY(0 < test.testRepeatedComplex().count());
    QCOMPARE(-173, test.testRepeatedComplex().at(0)->testFieldInt());
    QVERIFY(test.testRepeatedComplex().at(0)->testComplexField().testFieldString() == QString("qwerty"));
}

QTEST_MAIN(QtProtobufRepeatedTypesDeserializationTest)
#include "deserializationrepeatedtypes.moc"
