// 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/>.

#ifndef CORE_TRUSTHANDLER_H
#define CORE_TRUSTHANDLER_H

#include <shared/enums.h>
#include <shared/trustsummary.h>

#include <QXmppTrustStorage.h>
#include <cache.h>

namespace Core {
class Account;

class TrustHandler : public QObject, public QXmppTrustStorage {
    Q_OBJECT
public:
    TrustHandler(Account* account);
    ~TrustHandler();

signals:
    void trustLevelsChanged(const QString& jid, const Shared::TrustSummary& summary) const;

public:
    typedef QMultiHash<QString, QByteArray> MultySB;
    typedef QHash<QString, MultySB> HashSM;
    typedef const QList<QString>& CLSR;
    typedef const QList<QByteArray>& CLBAR;
    typedef const QString& CSR;
    typedef QXmpp::TrustLevel TL;
    typedef QHash<QString, QHash<QByteArray, TL>> HSHBTL;

    typedef std::map<QByteArray, Shared::TrustLevel> Keys;
    typedef LMDBAL::Cache<QString, Keys> KeyCache;

    virtual QXmppTask<void> resetAll(CSR encryption) override;
    virtual QXmppTask<TL> trustLevel(CSR encryption, CSR keyOwnerJid, const QByteArray& keyId) override;
    virtual QXmppTask<HashSM> setTrustLevel(CSR encryption, CLSR keyOwnerJids, TL oldTrustLevel, TL newTrustLevel) override;
    virtual QXmppTask<HashSM> setTrustLevel(CSR encryption, const MultySB& keyIds, TL trustLevel) override;
    virtual QXmppTask<bool> hasKey(CSR encryption, CSR keyOwnerJid, QXmpp::TrustLevels trustLevels) override;
    virtual QXmppTask<HSHBTL> keys(CSR encryption, CLSR keyOwnerJids, QXmpp::TrustLevels trustLevels) override;
    virtual QXmppTask<QHash<TL, MultySB>> keys(CSR encryption, QXmpp::TrustLevels trustLevels) override;
    virtual QXmppTask<void> removeKeys(CSR encryption) override;
    virtual QXmppTask<void> removeKeys(CSR encryption, CSR keyOwnerJid) override;
    virtual QXmppTask<void> removeKeys(CSR encryption, CLBAR keyIds) override;
    virtual QXmppTask<void> addKeys(CSR encryption, CSR keyOwnerJid, CLBAR keyIds, TL trustLevel) override;
    virtual QXmppTask<QByteArray> ownKey(CSR encryption) override;
    virtual QXmppTask<void> resetOwnKey(CSR encryption) override;
    virtual QXmppTask<void> setOwnKey(CSR encryption, const QByteArray& keyId) override;
    virtual QXmppTask<QXmpp::TrustSecurityPolicy> securityPolicy(CSR encryption) override;
    virtual QXmppTask<void> resetSecurityPolicy(CSR encryption) override;
    virtual QXmppTask<void> setSecurityPolicy(const QString& encryption, QXmpp::TrustSecurityPolicy securityPolicy) override;

    static TL convert(Shared::TrustLevel level);
    static Shared::TrustLevel convert(TL level);

    Keys getKeys(Shared::EncryptionProtocol protocol, const QString& jid) const;
    Shared::TrustSummary getSummary(const QString& jid) const;

private:
    KeyCache* createNewCache(const QString& encryption);
    KeyCache* getCache(const QString& encryption);

private:
    Account* acc;
    LMDBAL::Base db;
    QFile protocols;
    LMDBAL::Cache<QString, uint8_t>* securityPolicies;
    LMDBAL::Cache<QString, QByteArray>* ownKeys;
    std::map<QString, KeyCache*> keysByProtocol;
};

}

#endif // CORE_TRUSTHANDLER_H