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

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

#include <qtprotobuftestscommon.h>

class QtProtobufMapTypesGenerationTest : public QObject
{
    Q_OBJECT
public:
    QtProtobufMapTypesGenerationTest()
    {
        qRegisterProtobufTypes();
    }
private slots:
    void SInt32StringMapMessageTest();
    void StringStringMapMessageTest();

    void SimpleInt32ComplexMessageMapMessageCompareTest();
};

using namespace qtprotobufnamespace::tests;

void QtProtobufMapTypesGenerationTest::SInt32StringMapMessageTest()
{
    const char *propertyName = "mapField";
    qProtobufAssertMessagePropertyRegistered<SimpleSInt32StringMapMessage, SimpleSInt32StringMapMessage::MapFieldEntry>(1, "qtprotobufnamespace::tests::SimpleSInt32StringMapMessage::MapFieldEntry", propertyName);

    QVERIFY(QMetaType::isRegistered(qMetaTypeId<SimpleSInt32StringMapMessage::MapFieldEntry>()));

    SimpleSInt32StringMapMessage::MapFieldEntry testMap = {{10, {"Some 10"}}, {0, {"Some 0"}}, {44, {"Some 44"}}};
    SimpleSInt32StringMapMessage test;
    test.setMapField(testMap);
    QVERIFY(test.property(propertyName).value<SimpleSInt32StringMapMessage::MapFieldEntry>() == testMap);
    QVERIFY(test.mapField() == testMap);
    QCOMPARE(test.mapField()[10], QStringLiteral("Some 10"));
    QCOMPARE(test.mapField()[0], QStringLiteral("Some 0"));
    QCOMPARE(test.mapField()[44], QStringLiteral("Some 44"));

    test.mapField()[66] = "Some 66";
    QCOMPARE(test.mapField()[66], QStringLiteral("Some 66"));

    test.mapField()[66] = "Some default";
    QCOMPARE(test.mapField()[66], QStringLiteral("Some default"));
}

void QtProtobufMapTypesGenerationTest::StringStringMapMessageTest()
{
    const char *propertyName = "mapField";
    qProtobufAssertMessagePropertyRegistered<SimpleStringStringMapMessage, SimpleStringStringMapMessage::MapFieldEntry>(13, "qtprotobufnamespace::tests::SimpleStringStringMapMessage::MapFieldEntry", propertyName);

    QVERIFY(QMetaType::isRegistered(qMetaTypeId<SimpleStringStringMapMessage::MapFieldEntry>()));

    SimpleStringStringMapMessage::MapFieldEntry testMap = {{"key 10", "Some 10"}, {"key 0", "Some 0"}, {"key 44", "Some 44"}};
    SimpleStringStringMapMessage test;
    test.setMapField(testMap);
    QVERIFY(test.property(propertyName).value<SimpleStringStringMapMessage::MapFieldEntry>() == testMap);
    QVERIFY(test.mapField() == testMap);
    QCOMPARE(test.mapField()["key 10"], QStringLiteral("Some 10"));
    QCOMPARE(test.mapField()["key 0"], QStringLiteral("Some 0"));
    QCOMPARE(test.mapField()["key 44"], QStringLiteral("Some 44"));

    test.mapField()["key 66"] = "Some 66";
    QCOMPARE(test.mapField()["key 66"], QStringLiteral("Some 66"));

    test.mapField()["key 66"] = "Some default";
    QCOMPARE(test.mapField()["key 66"], QStringLiteral("Some default"));
}

void QtProtobufMapTypesGenerationTest::SimpleInt32ComplexMessageMapMessageCompareTest()
{
    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"}));

    SimpleInt32ComplexMessageMapMessage test1 = SimpleInt32ComplexMessageMapMessage({{20, msg1}, {30, msg2}});
    SimpleInt32ComplexMessageMapMessage test2 = SimpleInt32ComplexMessageMapMessage({{20, msg3}, {30, msg4}});
    QVERIFY(test1 == test2);
}

QTEST_MAIN(QtProtobufMapTypesGenerationTest)
#include "maptypes.moc"
