/*
 * Squawk messenger. 
 * Copyright (C) 2019  Yury Gubich <blue@macaw.me>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "messagehandler.h"
#include "core/account.h"

static const QMap<QString, QVariant> statePending({{"state", static_cast<uint8_t>(Shared::Message::State::pending)}});
static const QMap<QString, QVariant> stateDelivered({{"state", static_cast<uint8_t>(Shared::Message::State::delivered)}});
static const QMap<QString, QVariant> stateSent({{"state", static_cast<uint8_t>(Shared::Message::State::sent)}});

Core::MessageHandler::MessageHandler(Core::Account* account):
    QObject(),
    acc(account),
    pendingStateMessages(),
    uploadingSlotsQueue()
{}

void Core::MessageHandler::onMessageReceived(const QXmppMessage& msg) {
#if (QXMPP_VERSION) >= QT_VERSION_CHECK(1, 5, 0)
#ifdef WITH_OMEMO
    switch (msg.encryptionMethod()) {
        case QXmpp::NoEncryption:
            break;                          //just do nothing
        case QXmpp::UnknownEncryption:
            qDebug() << "Account" << acc->getName() << "received a message with unknown encryption type";
            break;                          //let it go the way it is, there is nothing I can do here
        case QXmpp::Otr:
            qDebug() << "Account" << acc->getName() << "received an OTR encrypted message, not supported yet";
            break;                          //let it go the way it is, there is nothing I can do yet
        case QXmpp::LegacyOpenPgp:
            qDebug() << "Account" << acc->getName() << "received an LegacyOpenPgp encrypted message, not supported yet";
            break;                          //let it go the way it is, there is nothing I can do yet
        case QXmpp::Ox:
            qDebug() << "Account" << acc->getName() << "received an Ox encrypted message, not supported yet";
            break;                          //let it go the way it is, there is nothing I can do yet
        case QXmpp::Omemo0:
            qDebug() << "Account" << acc->getName() << "received an Omemo0 encrypted message, not supported yet";
            break;                          //let it go the way it is, there is nothing I can do yet
        case QXmpp::Omemo1:
            qDebug() << "Account" << acc->getName() << "received an Omemo1 encrypted message, not supported yet";
            break;                          //let it go the way it is, there is nothing I can do yet
        case QXmpp::Omemo2:
            break;
    }
#endif
#endif
    bool handled = false;
    switch (msg.type()) {
        case QXmppMessage::Normal:
            qDebug() << "received a message with type \"Normal\", not sure what to do with it now, skipping";
            break;
        case QXmppMessage::Chat:
#if (QXMPP_VERSION) >= QT_VERSION_CHECK(1, 5, 0)
            handled = handleChatMessage(msg, false, msg.isCarbonForwarded(), true);
#else
            handled = handleChatMessage(msg);
#endif
            break;
        case QXmppMessage::GroupChat:
            handled = handleGroupMessage(msg);
            break;
        case QXmppMessage::Error:
            handled = handlePendingMessageError(msg.id(), msg.error().text());
            if (!handled)
                qDebug() << "received a message with type \"Error\", not sure what to do with it now, skipping";

            break;
        case QXmppMessage::Headline:
            qDebug() << "received a message with type \"Headline\", not sure what to do with it now, skipping";
            break;
    }
    if (!handled)
        logMessage(msg);
}

bool Core::MessageHandler::handlePendingMessageError(const QString& id, const QString& errorText) {
    return adjustPendingMessage(id, {
            {"state", static_cast<uint8_t>(Shared::Message::State::error)},
            {"errorText", errorText}
        }, true);
}

bool Core::MessageHandler::handleChatMessage(const QXmppMessage& msg, bool outgoing, bool forwarded, bool guessing) {
    if (msg.body().isEmpty() && msg.outOfBandUrl().isEmpty())
        return false;

    Shared::Message sMsg(Shared::Message::chat);
    initializeMessage(sMsg, msg, outgoing, forwarded, guessing);
    QString jid = sMsg.getPenPalJid();
    Contact* cnt = acc->rh->getContact(jid);
    if (cnt == 0) {
        cnt = acc->rh->addOutOfRosterContact(jid);
        qDebug() << "appending message" << sMsg.getId() << "to an out of roster contact";
    }
    if (sMsg.getOutgoing()) {
        if (sMsg.getForwarded())
            sMsg.setState(Shared::Message::State::sent);
    } else {
        sMsg.setState(Shared::Message::State::delivered);
    }
    QString oId = msg.replaceId();
    if (oId.size() > 0) {
        QMap<QString, QVariant> cData = {
            {"body", sMsg.getBody()},
            {"stamp", sMsg.getTime()}
        };
        cnt->correctMessageInArchive(oId, sMsg);
        emit acc->changeMessage(jid, oId, cData);
    } else {
        cnt->appendMessageToArchive(sMsg);
        emit acc->message(sMsg);
    }

    return true;
}

bool Core::MessageHandler::handleGroupMessage(const QXmppMessage& msg, bool outgoing, bool forwarded, bool guessing) {
    const QString& body(msg.body());
    if (body.isEmpty())
        return false;

    Shared::Message sMsg(Shared::Message::groupChat);
    initializeMessage(sMsg, msg, outgoing, forwarded, guessing);
    QString jid = sMsg.getPenPalJid();
    Conference* cnt = acc->rh->getConference(jid);
    if (cnt == 0)
        return false;

    bool result = adjustPendingMessage(msg.id(), stateDelivered, true);
    if (result)             //then it was an echo of my own sent message, nothing else needs to be done
        return result;

    QString oId = msg.replaceId();
    if (oId.size() > 0) {
        QMap<QString, QVariant> cData = {
            {"body", sMsg.getBody()},
            {"stamp", sMsg.getTime()}
        };
        cnt->correctMessageInArchive(oId, sMsg);
        emit acc->changeMessage(jid, oId, cData);
    } else {
        cnt->appendMessageToArchive(sMsg);
        QDateTime minAgo = QDateTime::currentDateTimeUtc().addSecs(-60);
        if (sMsg.getTime() > minAgo)     //otherwise it's considered a delayed delivery, most probably MUC history initial fetch
            emit acc->message(sMsg);
    }

    return true;
}

void Core::MessageHandler::initializeMessage(Shared::Message& target, const QXmppMessage& source, bool outgoing, bool forwarded, bool guessing) const {
    initializeIDs(target, source);
    QString messageId = target.getId();
    if (messageId.size() == 0) {
        target.generateRandomId();          //TODO out of desperation, I need at least a random ID
        messageId = target.getId();
        qDebug() << "Had do initialize a message with no id, assigning autogenerated" << messageId;
    }
    target.setFrom(source.from());
    target.setTo(source.to());
    target.setBody(source.body());
    target.setForwarded(forwarded);
#ifdef WITH_OMEMO
    #if (QXMPP_VERSION) >= QT_VERSION_CHECK(1, 5, 0)
    if (source.encryptionMethod() == QXmpp::EncryptionMethod::Omemo2)
        target.setEncryption(Shared::EncryptionProtocol::omemo2);
    #endif
#endif
    
    if (guessing)
        outgoing = target.getFromJid() == acc->getBareJid();

    const QDateTime& time(source.stamp());
    target.setOutgoing(outgoing);
    if (time.isValid())
        target.setTime(time);
    else
        target.setCurrentTime();

    QString oob = source.outOfBandUrl();
    if (oob.size() > 0)
        target.setAttachPath(acc->network->addMessageAndCheckForPath(oob, acc->getName(), target.getPenPalJid(), messageId));

    target.setOutOfBandUrl(oob);
}

void Core::MessageHandler::initializeIDs(Shared::Message& target, const QXmppMessage& source) const {
    QString id;
#if (QXMPP_VERSION) >= QT_VERSION_CHECK(1, 3, 0)
    id = source.originId();
    if (id.size() == 0)
        id = source.id();

    QString stanzaID;
#if (QXMPP_VERSION) >= QT_VERSION_CHECK(1, 5, 0)
    // here I'm looking preferably for id generated by myself, but if there isn't - any is better than nothing
    QVector<QXmppStanzaId> sIDs = source.stanzaIds();
    for (const QXmppStanzaId& sID : sIDs) {
        bool match = sID.by == acc->getBareJid();
        if (stanzaID.isEmpty() || match)
            stanzaID = sID.id;

        if (match)
            break;
    }
#else
    stanzaID = source.stanzaId();
#endif
    target.setStanzaId(stanzaID);
    qDebug() << "initializing message with originId:" << source.originId() << ", id:" << source.id() << ", stanzaId:" << stanzaID;
#else
    id = source.id();
#endif
    target.setId(id);
}


void Core::MessageHandler::logMessage(const QXmppMessage& msg, const QString& reason) {
    qDebug() << reason;
    qDebug() << "- from: " << msg.from();
    qDebug() << "- to: " << msg.to();
    qDebug() << "- body: " << msg.body();
    qDebug() << "- type: " << msg.type();
    qDebug() << "- state: " << msg.state();
    qDebug() << "- stamp: " << msg.stamp();
    qDebug() << "- id: " << msg.id();
    qDebug() << "- outOfBandUrl: " << msg.outOfBandUrl();
    qDebug() << "==============================";
}

#if (QXMPP_VERSION) < QT_VERSION_CHECK(1, 5, 0)
void Core::MessageHandler::onCarbonMessageReceived(const QXmppMessage& msg) {
    handleChatMessage(msg, false, true);
}

void Core::MessageHandler::onCarbonMessageSent(const QXmppMessage& msg) {
    handleChatMessage(msg, true, true);
}
#endif

std::optional<Shared::MessageInfo> Core::MessageHandler::getOriginalPendingMessageId(const QString& id, bool clear) {
    std::map<QString, QString>::const_iterator itr = pendingStateMessages.find(id);
    if (itr != pendingStateMessages.end()) {
        Shared::MessageInfo info(acc->name, itr->second, itr->first);
        std::map<QString, QString>::const_iterator itrC = pendingCorrectionMessages.find(id);
        if (itrC != pendingCorrectionMessages.end()) {
            if (itrC->second.size() > 0)
                info.jid = itrC->second;

            if (clear)
                pendingCorrectionMessages.erase(itrC);
        }

        if (clear)
            pendingStateMessages.erase(itr);

        return info;
    }

    return std::nullopt;
}

void Core::MessageHandler::onReceiptReceived(const QString& jid, const QString& id) {
    SHARED_UNUSED(jid);
    adjustPendingMessage(id, {{"state", static_cast<uint>(Shared::Message::State::delivered)}}, true);
}

void Core::MessageHandler::sendMessage(const Shared::Message& data, bool newMessage, QString originalId) {
    if (data.getOutOfBandUrl().size() == 0 && data.getAttachPath().size() > 0) {
        pendingCorrectionMessages.insert(std::make_pair(data.getId(), originalId));
        prepareUpload(data, newMessage);
    } else {
        performSending(data, originalId, newMessage);
    }
}

void Core::MessageHandler::performSending(Shared::Message data, const QString& originalId, bool newMessage) {
    QString jid = data.getPenPalJid();
    QString id = data.getId();
    qDebug() << "Sending message with id:" << id;
    if (originalId.size() > 0)
        qDebug() << "To replace the one with id:" << originalId;

    RosterItem* ri = acc->rh->getRosterItem(jid);
    if (newMessage && originalId.size() > 0)
        newMessage = false;

    QDateTime sendTime = QDateTime::currentDateTimeUtc();
    std::pair<Shared::Message::State, QString> result = scheduleSending(data, sendTime, originalId);
    data.setState(result.first);
    data.setErrorText(result.second);
    
    QMap<QString, QVariant> changes(getChanges(data, sendTime, newMessage, originalId));
    if (ri != nullptr) {
        if (newMessage)
            ri->appendMessageToArchive(data);
         else
            ri->changeMessage(originalId.isEmpty() ? id : originalId, changes);

        if (data.getState() != Shared::Message::State::error) {
            pendingStateMessages.insert(std::make_pair(id, jid));
            if (originalId.size() > 0)
                pendingCorrectionMessages.insert(std::make_pair(id, originalId));
        } else {
            pendingStateMessages.erase(id);
            pendingCorrectionMessages.erase(id);
        }
    }
    
    emit acc->changeMessage(jid, originalId.isEmpty() ? id : originalId, changes);
}

std::pair<Shared::Message::State, QString> Core::MessageHandler::scheduleSending(
    const Shared::Message& message,
    const QDateTime& sendTime,
    const QString& originalId
) {
    if (acc->state != Shared::ConnectionState::connected)
        return {Shared::Message::State::error, "You are is offline or reconnecting"};

    QXmppMessage msg = createPacket(message, sendTime, originalId);
    QString id = msg.id();
#ifdef WITH_OMEMO
    if (message.getEncryption() == Shared::EncryptionProtocol::omemo2) {
        QXmppTask<QXmppE2eeExtension::MessageEncryptResult> task = acc->om->encryptMessage(std::move(msg), std::nullopt);
        if (task.isFinished()) {
            const QXmppE2eeExtension::MessageEncryptResult& res = task.result();
            if (std::holds_alternative<std::unique_ptr<QXmppMessage>>(res)) {
                qDebug() << "Successfully encrypted a message";
                const std::unique_ptr<QXmppMessage>& encrypted = std::get<std::unique_ptr<QXmppMessage>>(res);
                encrypted->setBody(QString());
                encrypted->setOutOfBandUrl(QString());
                bool success = acc->client.sendPacket(*encrypted.get());
                if (success) {
                    qDebug() << "Successfully sent an encrypted message";
                    return {Shared::Message::State::sent, ""};
                } else {
                    qDebug() << "Couldn't sent an encrypted message";
                    return {Shared::Message::State::error, "Error sending successfully encrypted message"};
                }
            } else if (std::holds_alternative<QXmppError>(res)) {
                qDebug() << "Couldn't encrypt a message";
                const QXmppError& err = std::get<QXmppError>(res);
                return {Shared::Message::State::error, err.description};
            } else {
                qDebug() << "Couldn't encrypt a message";
                return {Shared::Message::State::error, "Unexpected error ecryptng the message"};
            }
        } else {
            task.then(this, [this, id] (QXmppE2eeExtension::MessageEncryptResult&& result) {
                if (std::holds_alternative<std::unique_ptr<QXmppMessage>>(result)) {
                    qDebug() << "Successfully encrypted a message";
                    const std::unique_ptr<QXmppMessage>& encrypted = std::get<std::unique_ptr<QXmppMessage>>(result);
                    encrypted->setBody(QString());
                    encrypted->setOutOfBandUrl(QString());
                    bool success = acc->client.sendPacket(*encrypted.get());
                    if (success) {
                        qDebug() << "Successfully sent an encrypted message";
                        if (!adjustPendingMessage(id, stateSent, false))
                            qDebug() << "Encrypted message has been successfully sent, but it couldn't be found to update the sate";
                    } else {
                        qDebug() << "Couldn't sent an encrypted message";
                        handlePendingMessageError(id, "Error sending successfully encrypted message");
                    }
                } else if (std::holds_alternative<QXmppError>(result)) {
                    qDebug() << "Couldn't encrypt a message";
                    const QXmppError& err = std::get<QXmppError>(result);
                    handlePendingMessageError(id, err.description);
                } else {
                    qDebug() << "Couldn't encrypt a message";
                    handlePendingMessageError(id, "Unexpected error ecryptng the message");
                }
            });
            return {Shared::Message::State::pending, ""};
        }
    } else
#endif
    {
        bool success = acc->client.sendPacket(msg);
        if (success)
            return {Shared::Message::State::sent, ""};
        else
            return {Shared::Message::State::error, "Error sending message, internal QXMPP error"};
    }
}

bool Core::MessageHandler::adjustPendingMessage(const QString& messageId, const QMap<QString, QVariant>& data, bool final) {
    std::optional<Shared::MessageInfo> info = getOriginalPendingMessageId(messageId, final);
    if (info) {
        RosterItem* ri = acc->rh->getRosterItem(info->jid);
        if (ri != nullptr)
            ri->changeMessage(info->messageId, data);

        emit acc->changeMessage(info->jid, info->messageId, data);
        return true;
    }

    return false;
}

QMap<QString, QVariant> Core::MessageHandler::getChanges(Shared::Message& data, const QDateTime& time, bool newMessage, const QString& originalId) const {
    QMap<QString, QVariant> changes;

    QString oob = data.getOutOfBandUrl();
    Shared::Message::State mstate = data.getState();
    changes.insert("state", static_cast<uint>(mstate));
    if (mstate == Shared::Message::State::error)
        changes.insert("errorText", data.getErrorText());

    if (oob.size() > 0)
        changes.insert("outOfBandUrl", oob);

    if (newMessage)
        data.setTime(time);

    if (originalId.size() > 0)
        changes.insert("body", data.getBody());

    changes.insert("stamp", time);

    //sometimes (when the image is pasted with ctrl+v)
    //I start sending message with one path, then copy it to downloads directory
    //so, the final path changes. Let's assume it changes always since it costs me close to nothing
    QString attachPath = data.getAttachPath();
    if (attachPath.size() > 0) {
        QString squawkified = Shared::squawkifyPath(attachPath);
        changes.insert("attachPath", squawkified);
        if (attachPath != squawkified)
            data.setAttachPath(squawkified);
    }

    return changes;
}

QXmppMessage Core::MessageHandler::createPacket(const Shared::Message& data, const QDateTime& time, const QString& originalId) const {
    QXmppMessage msg(QString(), data.getTo(), data.getBody(), data.getThread());
    QString id(data.getId());

    if (originalId.size() > 0)
        msg.setReplaceId(originalId);


#if (QXMPP_VERSION) >= QT_VERSION_CHECK(1, 3, 0)
    msg.setOriginId(id);
#endif
    msg.setId(id);
    msg.setType(static_cast<QXmppMessage::Type>(data.getType()));       //it is safe here, my type is compatible
    msg.setOutOfBandUrl(data.getOutOfBandUrl());
    msg.setReceiptRequested(true);
    msg.setStamp(time);

    return msg;
}

void Core::MessageHandler::prepareUpload(const Shared::Message& data, bool newMessage) {
    if (acc->state != Shared::ConnectionState::connected) {
        handleUploadError(data.getPenPalJid(), data.getId(), "Account is offline or reconnecting");
        qDebug() << "An attempt to send message with not connected account " << acc->name << ", skipping";
        return;
    }

    QString jid = data.getPenPalJid();
    QString id = data.getId();
    RosterItem* ri = acc->rh->getRosterItem(jid);
    if (ri == nullptr) {
        qDebug() << "An attempt to initialize upload in" << acc->name << "for pal" << jid << "but the object for this pal wasn't found, something went terrebly wrong, skipping send";
        return;
    }

    QString path = data.getAttachPath();
    QString url = acc->network->getFileRemoteUrl(path);
    if (url.size() != 0)
        return sendMessageWithLocalUploadedFile(data, url, newMessage);

    pendingStateMessages.insert(std::make_pair(id, jid));
    if (newMessage) {
        ri->appendMessageToArchive(data);
    } else {
        ri->changeMessage(id, statePending);
        emit acc->changeMessage(jid, id, statePending);
    }

    //this checks if the file is already uploading, and if so it subscribes to it's success,
    //So, I need to do stuff only if the network knows nothing of this file
    if (acc->network->checkAndAddToUploading(acc->getName(), jid, id, path))
        return;

    if (!acc->um->serviceFound()) {
        handleUploadError(jid, id, "Your server doesn't support file upload service, or it's prohibited for your account");
        qDebug() << "Requested upload slot in account" << acc->name << "for file" << path << "but upload manager didn't discover any upload services";
        return;
    }

    QFileInfo file(path);
    if (file.exists() && file.isReadable()) {
        pendingStateMessages.insert(std::make_pair(id, jid));
        uploadingSlotsQueue.emplace_back(file, id);
        if (uploadingSlotsQueue.size() == 1)
            acc->um->requestUploadSlot(file);
    } else {
        handleUploadError(jid, id, "Uploading file no longer exists or your system user has no permission to read it");
        qDebug() << "Requested upload slot in account" << acc->name << "for file" << path << "but the file doesn't exist or is not readable";
    }
}

void Core::MessageHandler::onUploadSlotReceived(const QXmppHttpUploadSlotIq& slot) {
    if (uploadingSlotsQueue.size() == 0) {
        qDebug() << "HTTP Upload manager of account" << acc->name << "reports about success requesting upload slot, but none was requested";
    } else {
        const std::pair<QFileInfo, QString>& pair = uploadingSlotsQueue.front();
        const QString& mId = pair.second;
        QString palJid = pendingStateMessages.at(mId);
        acc->network->uploadFile({acc->name, palJid, mId}, pair.first.path(), slot.putUrl(), slot.getUrl(), slot.putHeaders());
        
        uploadingSlotsQueue.pop_front();
        if (uploadingSlotsQueue.size() > 0)
            acc->um->requestUploadSlot(uploadingSlotsQueue.front().first);
    }
}

void Core::MessageHandler::onUploadSlotRequestFailed(const QXmppHttpUploadRequestIq& request) {
    QString err(request.error().text());
    if (uploadingSlotsQueue.size() == 0) {
        qDebug() << "HTTP Upload manager of account" << acc->name << "reports about an error requesting upload slot, but none was requested";
        qDebug() << err;
    } else {
        const std::pair<QFileInfo, QString>& pair = uploadingSlotsQueue.front();
        qDebug() << "Error requesting upload slot for file" << pair.first << "in account" << acc->name << ":" << err;
        handleUploadError(pendingStateMessages.at(pair.second), pair.second, err);
        
        uploadingSlotsQueue.pop_front();
        if (uploadingSlotsQueue.size() > 0)
            acc->um->requestUploadSlot(uploadingSlotsQueue.front().first);
    }
}

void Core::MessageHandler::onDownloadFileComplete(const std::list<Shared::MessageInfo>& msgs, const QString& path) {
    QMap<QString, QVariant> cData = {
        {"attachPath", path}
    };
    for (const Shared::MessageInfo& info : msgs) {
        if (info.account != acc->getName())
            continue;

        RosterItem* cnt = acc->rh->getRosterItem(info.jid);
        if (cnt != nullptr) {
            bool changed = cnt->changeMessage(info.messageId, cData);
            if (changed)
                emit acc->changeMessage(info.jid, info.messageId, cData);
        }
    }
}

void Core::MessageHandler::onLoadFileError(const std::list<Shared::MessageInfo>& msgs, const QString& text, bool up) {
    if (!up)
        return;

    for (const Shared::MessageInfo& info : msgs)
        if (info.account == acc->getName())
            handleUploadError(info.jid, info.messageId, text);
}

void Core::MessageHandler::handleUploadError(const QString& jid, const QString& messageId, const QString& errorText) {
    emit acc->uploadFileError(jid, messageId, "Error requesting slot to upload file: " + errorText);
    pendingStateMessages.erase(messageId);
    pendingCorrectionMessages.erase(messageId);
    requestChangeMessage(jid, messageId, {
        {"state", static_cast<uint>(Shared::Message::State::error)},
        {"errorText", errorText}
    });
}

void Core::MessageHandler::onUploadFileComplete(const std::list<Shared::MessageInfo>& msgs, const QString& url, const QString& path) {
    for (const Shared::MessageInfo& info : msgs) {
        if (info.account != acc->getName())
            continue;

        RosterItem* ri = acc->rh->getRosterItem(info.jid);
        if (ri != nullptr) {
            Shared::Message msg = ri->getMessage(info.messageId);
            msg.setAttachPath(path);
            sendMessageWithLocalUploadedFile(msg, url, false);
        } else {
            qDebug() << "A signal received about complete upload to" << acc->name << "for pal" << info.jid << "but the object for this pal wasn't found, something went terrebly wrong, skipping send";
        }
    }
}

void Core::MessageHandler::sendMessageWithLocalUploadedFile(Shared::Message msg, const QString& url, bool newMessage) {
    msg.setOutOfBandUrl(url);
    if (msg.getBody().size() == 0)      //not sure why, but most messengers do that
        msg.setBody(url);               //they duplicate oob in body, some of them wouldn't even show an attachment if you don't do that

    performSending(msg, pendingCorrectionMessages.at(msg.getId()), newMessage);
    //TODO removal/progress update
}

static const std::set<QString> allowedToChangeKeys({
    "attachPath",
    "outOfBandUrl",
    "state",
    "errorText"
});

void Core::MessageHandler::requestChangeMessage(const QString& jid, const QString& messageId, const QMap<QString, QVariant>& data) {
    RosterItem* cnt = acc->rh->getRosterItem(jid);
    if (cnt != nullptr) {
        bool allSupported = true;
        QString unsupportedString;
        for (QMap<QString, QVariant>::const_iterator itr = data.begin(); itr != data.end(); ++itr) {    //I need all this madness
            if (allowedToChangeKeys.count(itr.key()) != 1) {                                            //to not allow this method
                allSupported = false;                                                                   //to make a message to look like if it was edited
                unsupportedString = itr.key();                                                          //basically I needed to control who exaclty calls this method
                break;                                                                                  //because the underlying tech assumes that
            }                                                                                           //the change is initiated by user, not by system
        }
        if (allSupported) {
            cnt->changeMessage(messageId, data);
            emit acc->changeMessage(jid, messageId, data);
        } else {
            qDebug() << "A request to change message" << messageId << "of conversation" << jid << "with following data" << data;
            qDebug() << "only limited set of dataFields are supported yet here, and" << unsupportedString << "isn't one of them, skipping";
        }
    }
}

void Core::MessageHandler::resendMessage(const QString& jid, const QString& id) {
    RosterItem* cnt = acc->rh->getRosterItem(jid);
    if (cnt != nullptr) {
        try {
            Shared::Message msg = cnt->getMessage(id);
            if (msg.getState() == Shared::Message::State::error) {
                if (msg.getEdited()) {
                    QString originalId = msg.getId();
                    msg.generateRandomId();
                    sendMessage(msg, false, originalId);
                } else {
                    sendMessage(msg, false);
                }
            } else {
                qDebug() << "An attempt to resend a message to" << jid << "by account" << acc->getName() << ", but this message seems to have been normally sent, this method was made to retry sending failed to be sent messages, skipping";
            }
        } catch (const LMDBAL::NotFound& err) {
            qDebug() << "An attempt to resend a message to" << jid << "by account" << acc->getName() << ", but this message wasn't found in history, skipping";
        }
    } else {
        qDebug() << "An attempt to resend a message to" << jid << "by account" << acc->getName() << ", but this jid isn't present in account roster, skipping";
    }
}