// SPDX-FileCopyrightText: 2021 Melvin Keskin <melvo@olomono.de>
//
// SPDX-License-Identifier: LGPL-2.1-or-later

#include "QXmppMixInvitation.h"

#include "QXmppConstants_p.h"
#include "QXmppUtils_p.h"

#include "StringLiterals.h"
#include "XmlWriter.h"

#include <QDomElement>
#include <QSharedData>

using namespace QXmpp::Private;

class QXmppMixInvitationPrivate : public QSharedData
{
public:
    QString inviterJid;
    QString inviteeJid;
    QString channelJid;
    QString token;
};

/*!
    \class QXmppMixInvitation
    \inmodule QXmpp

    \brief The QXmppMixInvitation class is used to invite a user to a
    \xep{0369}{Mediated Information eXchange (MIX)} channel as defined by
    \xep{0407}{Mediated Information eXchange (MIX): Miscellaneous Capabilities}.

    \ingroup Stanzas

    \since QXmpp 1.4
*/

/*! Default constructor */
QXmppMixInvitation::QXmppMixInvitation()
    : d(new QXmppMixInvitationPrivate)
{
}

/*! Copy constructor. */
QXmppMixInvitation::QXmppMixInvitation(const QXmppMixInvitation &other) = default;
/*! Copy constructor. */
QXmppMixInvitation::QXmppMixInvitation(QXmppMixInvitation &&) = default;
/*! Default assignment operator. */
QXmppMixInvitation &QXmppMixInvitation::operator=(const QXmppMixInvitation &other) = default;
/*! Default assignment operator. */
QXmppMixInvitation &QXmppMixInvitation::operator=(QXmppMixInvitation &&) = default;
QXmppMixInvitation::~QXmppMixInvitation() = default;

/*!
    Returns the JID of the inviter.
*/
QString QXmppMixInvitation::inviterJid() const
{
    return d->inviterJid;
}

/*!
    Sets the \a inviterJid of the inviter.
*/
void QXmppMixInvitation::setInviterJid(const QString &inviterJid)
{
    d->inviterJid = inviterJid;
}

/*!
    Returns the JID of the invitee.
*/
QString QXmppMixInvitation::inviteeJid() const
{
    return d->inviteeJid;
}

/*!
    Sets the \a inviteeJid of the invitee.
*/
void QXmppMixInvitation::setInviteeJid(const QString &inviteeJid)
{
    d->inviteeJid = inviteeJid;
}

/*!
    Returns the JID of the channel.
*/
QString QXmppMixInvitation::channelJid() const
{
    return d->channelJid;
}

/*!
    Sets the \a channelJid of the channel.
*/
void QXmppMixInvitation::setChannelJid(const QString &channelJid)
{
    d->channelJid = channelJid;
}

/*!
    Returns the token which is generated by the server and used by the invitee
    for authentication.
*/
QString QXmppMixInvitation::token() const
{
    return d->token;
}

/*!
    Sets the authentication \a token which is generated by the server and used
    by the invitee for authentication.
*/
void QXmppMixInvitation::setToken(const QString &token)
{
    d->token = token;
}

void QXmppMixInvitation::parse(const QDomElement &element)
{
    d->inviterJid = element.firstChildElement(u"inviter"_s).text();
    d->inviteeJid = element.firstChildElement(u"invitee"_s).text();
    d->channelJid = element.firstChildElement(u"channel"_s).text();
    d->token = element.firstChildElement(u"token"_s).text();
}

void QXmppMixInvitation::toXml(QXmlStreamWriter *writer) const
{
    XmlWriter(writer).write(Element {
        XmlTag,
        OptionalTextElement { u"inviter", d->inviterJid },
        OptionalTextElement { u"invitee", d->inviteeJid },
        OptionalTextElement { u"channel", d->channelJid },
        OptionalTextElement { u"token", d->token },
    });
}

/*!
    Determines whether the given DOM \a element is a MIX invitation.

    Returns true if \a element is a MIX invitation, otherwise false.
*/
bool QXmppMixInvitation::isMixInvitation(const QDomElement &element)
{
    return element.tagName() == u"invitation" &&
        element.namespaceURI() == ns_mix_misc;
}
