/*
    This file is part of libkabc.
    Copyright (C) 2015-2019 Laurent Montel <montel@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "impptest.h"
#include "impp.h"
#include <QTest>
#include "vcardtool_p.h"

ImppTest::ImppTest(QObject *parent)
    : QObject(parent)
{
}

ImppTest::~ImppTest()
{
}

void ImppTest::shouldHaveDefaultValue()
{
    KContacts::Impp impp;
    QVERIFY(!impp.isValid());
    QVERIFY(impp.address().isEmpty());
    QVERIFY(impp.serviceType().isEmpty());
    QVERIFY(impp.parameters().isEmpty());
}

void ImppTest::shouldAssignValue()
{
    const QUrl address(QStringLiteral("icq:address"));
    QMap<QString, QStringList> params;
    params.insert(QStringLiteral("Foo1"), QStringList() << QStringLiteral("bla1") << QStringLiteral("blo1"));
    params.insert(QStringLiteral("Foo2"), QStringList() << QStringLiteral("bla2") << QStringLiteral("blo2"));
    KContacts::Impp impp;
    impp.setParameters(params);
    impp.setAddress(address);
    QVERIFY(impp.isValid());
    QVERIFY(!impp.address().isEmpty());
    QCOMPARE(impp.address(), address);
    QCOMPARE(impp.serviceType(), QLatin1String("icq"));
    QCOMPARE(impp.serviceIcon(), QLatin1String("im-icq"));
    QVERIFY(!impp.parameters().isEmpty());
    QCOMPARE(impp.parameters(), params);
}

void ImppTest::shouldSerialized()
{
    const QUrl address(QStringLiteral("icq:address"));
    QMap<QString, QStringList> params;
    params.insert(QStringLiteral("Foo1"), QStringList() << QStringLiteral("bla1") << QStringLiteral("blo1"));
    params.insert(QStringLiteral("Foo2"), QStringList() << QStringLiteral("bla2") << QStringLiteral("blo2"));
    KContacts::Impp impp;
    impp.setParameters(params);
    impp.setAddress(address);

    QByteArray data;
    QDataStream s(&data, QIODevice::WriteOnly);
    s << impp;

    KContacts::Impp result;
    QDataStream t(&data, QIODevice::ReadOnly);
    t >> result;

    QVERIFY(impp == result);
}

void ImppTest::shouldEqualImpp()
{
    const QUrl address(QStringLiteral("icq:address"));
    QMap<QString, QStringList> params;
    params.insert(QStringLiteral("Foo1"), QStringList() << QStringLiteral("bla1") << QStringLiteral("blo1"));
    params.insert(QStringLiteral("Foo2"), QStringList() << QStringLiteral("bla2") << QStringLiteral("blo2"));
    KContacts::Impp impp;
    impp.setParameters(params);
    impp.setAddress(address);

    KContacts::Impp result(impp);
    QVERIFY(impp == result);
}

void ImppTest::shouldParseWithoutImpp()
{
    QByteArray vcarddata("BEGIN:VCARD\n"
                         "VERSION:3.0\n"
                         "N:LastName;FirstName;;;\n"
                         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
                         "LANG:fr"
                         "REV:2015-03-14T09:24:45+00:00\n"
                         "FN:FirstName LastName\n"
                         "END:VCARD\n");

    KContacts::VCardTool vcard;
    const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
    QCOMPARE(lst.count(), 1);
    QCOMPARE(lst.at(0).imppList().count(), 0);
}

void ImppTest::shouldParseImpp()
{
    QByteArray vcarddata("BEGIN:VCARD\n"
                         "VERSION:3.0\n"
                         "N:LastName;FirstName;;;\n"
                         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
                         "IMPP;X-SERVICE-TYPE=skype:skype:xxxxxxxx\n"
                         "REV:2015-03-14T09:24:45+00:00\n"
                         "FN:FirstName LastName\n"
                         "END:VCARD\n");

    KContacts::VCardTool vcard;
    const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
    QCOMPARE(lst.count(), 1);
    QCOMPARE(lst.at(0).imppList().count(), 1);
    KContacts::Impp impp = lst.at(0).imppList().at(0);
    QCOMPARE(impp.address(), QUrl(QStringLiteral("skype:xxxxxxxx")));
    QCOMPARE(impp.serviceType(), QLatin1String("skype"));
}

void ImppTest::shouldParseImppVcard4()
{
    QByteArray vcarddata("BEGIN:VCARD\n"
                         "VERSION:4.0\n"
                         "N:LastName;FirstName;;;\n"
                         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
                         "IMPP;PREF=1:skype:xxxxxxxx\n"
                         "IMPP:skype:1234567890\n"
                         "REV:2015-03-14T09:24:45+00:00\n"
                         "FN:FirstName LastName\n"
                         "END:VCARD\n");

    KContacts::VCardTool vcard;
    const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
    QCOMPARE(lst.count(), 1);
    QCOMPARE(lst.at(0).imppList().count(), 2);
    KContacts::Impp impp = lst.at(0).imppList().at(0);
    QCOMPARE(impp.address(), QUrl(QStringLiteral("skype:xxxxxxxx")));
    QCOMPARE(impp.serviceType(), QLatin1String("skype"));
    QVERIFY(impp.isPreferred());
    impp = lst.at(0).imppList().at(1);
    QCOMPARE(impp.address(), QUrl(QStringLiteral("skype:1234567890")));
    QCOMPARE(impp.serviceType(), QLatin1String("skype"));
    QVERIFY(!impp.isPreferred());
}

QByteArray createCard(const QString &type)
{
    QByteArray expected("BEGIN:VCARD\n"
                        "VERSION:3.0\n"
                        "EMAIL:foo@kde.org\n");
    if (!type.isEmpty()) {
        expected += "IMPP;X-SERVICE-TYPE=" + type.toLatin1() + ":" + type.toLatin1() + ":address\n";
    }
    expected += QByteArray(
        "N:;;;;\n"
        "UID:testuid\n"
        "END:VCARD\n\n");
    return expected;
}

void ImppTest::shouldParseServiceType_data()
{
    QTest::addColumn<QString>("type");
    QTest::addColumn<bool>("hasImpp");
    QTest::newRow("withoutimpp") << QString() << false;
    for (const auto &type : KContacts::Impp::serviceTypes()) {
        QTest::newRow(type.toLatin1().constData()) << type << true;
    }
}

void ImppTest::shouldParseServiceType()
{
    QFETCH(QString, type);
    QFETCH(bool, hasImpp);

    QByteArray vcarddata = createCard(type);

    KContacts::VCardTool vcard;
    const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
    QCOMPARE(lst.count(), 1);

    QCOMPARE(!lst.at(0).imppList().isEmpty(), hasImpp);
    if (hasImpp) {
        KContacts::Impp impp = lst.at(0).imppList().at(0);
        QCOMPARE(impp.address(), QUrl(QStringLiteral("%1:address").arg(type)));
        QCOMPARE(impp.serviceType(), type);
    }
}

QByteArray expectedVcard(const QString &type)
{
    QByteArray expected("BEGIN:VCARD\r\n"
                        "VERSION:4.0\r\n"
                        "EMAIL:foo@kde.org\r\n");
    if (!type.isEmpty()) {
        expected += "IMPP:" + type.toLatin1() + ":address\r\n";
    }

    expected += ("N:;;;;\r\n"
                 "UID:testuid\r\n"
                 "END:VCARD\r\n\r\n");
    return expected;
}

void ImppTest::shouldExportEmptyType()
{
    QByteArray expected("BEGIN:VCARD\r\n"
                        "VERSION:4.0\r\n"
                        "EMAIL:foo@kde.org\r\n"
                        "N:;;;;\r\n"
                        "UID:testuid\r\n"
                        "END:VCARD\r\n\r\n");
    KContacts::AddresseeList lst;
    KContacts::Addressee addr;
    addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
    addr.setUid(QStringLiteral("testuid"));
    lst << addr;
    KContacts::VCardTool vcard;
    const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
    QCOMPARE(ba, expected);
}

void ImppTest::shouldExportType_data()
{
    QTest::addColumn<QString>("type");
    for (const auto &type : KContacts::Impp::serviceTypes()) {
        QTest::newRow(type.toLatin1().constData()) << type;
    }
}

void ImppTest::shouldExportType()
{
    QFETCH(QString, type);

    QByteArray expected = expectedVcard(type);
    KContacts::AddresseeList lst;
    KContacts::Addressee addr;
    addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
    addr.setUid(QStringLiteral("testuid"));
    KContacts::Impp impp;
    impp.setAddress(QUrl(type + QStringLiteral(":address")));
    addr.insertImpp(impp);
    lst << addr;
    KContacts::VCardTool vcard;
    const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
    QCOMPARE(ba, expected);
}

void ImppTest::shouldExportWithParameters()
{
    QByteArray expected("BEGIN:VCARD\r\n"
                        "VERSION:4.0\r\n"
                        "EMAIL:foo@kde.org\r\n"
                        "IMPP;FOO1=bla1,blo1;FOO2=bla2,blo2:skype:address\r\n"
                        "N:;;;;\r\n"
                        "UID:testuid\r\n"
                        "END:VCARD\r\n\r\n");
    KContacts::AddresseeList lst;
    KContacts::Addressee addr;
    addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
    addr.setUid(QStringLiteral("testuid"));
    QMap<QString, QStringList> params;
    params.insert(QStringLiteral("Foo1"), QStringList() << QStringLiteral("bla1") << QStringLiteral("blo1"));
    params.insert(QStringLiteral("Foo2"), QStringList() << QStringLiteral("bla2") << QStringLiteral("blo2"));
    KContacts::Impp impp;
    impp.setAddress(QUrl(QStringLiteral("skype:address")));
    impp.setParameters(params);
    impp.setPreferred(false);
    addr.insertImpp(impp);
    lst << addr;

    KContacts::VCardTool vcard;
    const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
    QCOMPARE(ba, expected);
}

void ImppTest::shouldShouldNotExportTwiceServiceType()
{
    QByteArray expected("BEGIN:VCARD\r\n"
                        "VERSION:4.0\r\n"
                        "EMAIL:foo@kde.org\r\n"
                        "IMPP;FOO1=bla1,blo1;FOO2=bla2,blo2;PREF=1:skype:address\r\n"
                        "N:;;;;\r\n"
                        "UID:testuid\r\n"
                        "END:VCARD\r\n\r\n");
    KContacts::AddresseeList lst;
    KContacts::Addressee addr;
    addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
    addr.setUid(QStringLiteral("testuid"));
    QMap<QString, QStringList> params;
    params.insert(QStringLiteral("Foo1"), QStringList() << QStringLiteral("bla1") << QStringLiteral("blo1"));
    params.insert(QStringLiteral("Foo2"), QStringList() << QStringLiteral("bla2") << QStringLiteral("blo2"));
    params.insert(QStringLiteral("X-SERVICE-TYPE"), QStringList() << QStringLiteral("aim"));
    KContacts::Impp impp;
    impp.setAddress(QUrl(QStringLiteral("skype:address")));
    impp.setParameters(params);
    impp.setPreferred(true);
    addr.insertImpp(impp);
    lst << addr;

    KContacts::VCardTool vcard;
    const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
    QCOMPARE(ba, expected);
}

void ImppTest::testProtocolInformation()
{
    const auto types = KContacts::Impp::serviceTypes();
    QVERIFY(types.size() > 10);
    QVERIFY(types.contains(QLatin1String("xmpp")));

    for (const auto &type : types) {
        QVERIFY(!KContacts::Impp::serviceLabel(type).isEmpty());
    }

    QCOMPARE(KContacts::Impp::serviceIcon(QStringLiteral("xmpp")), QLatin1String("im-jabber"));
}

QTEST_MAIN(ImppTest)
