#include "registrymanager.h"

#include <QDebug>
#include <QSettings>

RegistryManager *RegistryManager::s_instance = nullptr;

RegistryManager *RegistryManager::instance() {
    if (!s_instance) {
        s_instance = new RegistryManager();
    }
    return s_instance;
}

void RegistryManager::cleanup() {
    if (s_instance) {
        delete s_instance;
        s_instance = nullptr;
    }
}

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

RegistryManager::~RegistryManager() {}

bool RegistryManager::saveUserInfo(const QString &userId, const QString &username,
                                   const QString &password, const QString &identity,
                                   const QString &certName) {
    QSettings settings(organizationName, applicationName);
    settings.beginGroup(userId);
    settings.setValue("Username", username);
    settings.setValue("Password", password);
    settings.setValue("Identity", identity);
    settings.setValue("CertName", certName);
    qDebug() << "saved user info" << username << password << identity;
    settings.endGroup();

    if (settings.status() == QSettings::NoError) {
        return true;
    } else {
        return false;
    }
}

bool RegistryManager::readUserInfo(const QString &userId, QString &username, QString &password) {
    QSettings settings(organizationName, applicationName);
    settings.beginGroup(userId);
    if (settings.contains("Username") && settings.contains("Password")) {
        username = settings.value("Username").toString();
        password = settings.value("Password").toString();
        settings.endGroup();

        return true;
    } else {
        settings.endGroup();

        return false;
    }
}

QStringList RegistryManager::getAllUserIds() {
    QSettings settings(organizationName, applicationName);
    return settings.childGroups();
}

QStringList RegistryManager::getAllUsernames() {
    QStringList usernames;
    QMap<QString, QPair<QString, QString>> allUserInfos = getAllUserInfos();
    for (const auto &userInfo : allUserInfos) {
        usernames.append(userInfo.first);
    }
    return usernames;
}

QString RegistryManager::getPasswordByUsername(const QString &username) {
    QMap<QString, QPair<QString, QString>> allUserInfos = getAllUserInfos();
    for (const auto &userInfo : allUserInfos) {
        if (userInfo.first == username) {
            return userInfo.second;
        }
    }
    return "";  // 如果未找到匹配的用户名，返回空字符串
}

QMap<QString, QPair<QString, QString>> RegistryManager::getAllUserInfos() {
    QMap<QString, QPair<QString, QString>> allUserInfos;

    QStringList userIds = getAllUserIds();
    for (const QString &userId : userIds) {
        QString username, password;
        if (readUserInfo(userId, username, password)) {
            allUserInfos.insert(userId, qMakePair(username, password));
        }
    }

    return allUserInfos;
}

QMap<QString, QString> RegistryManager::getCurentLoginUser() {
    QMap<QString, QString> currentUserInfo;

    QSettings settings(organizationName, applicationName);
    settings.beginGroup(currentLoginUserId);
    settings.value("Username").toString();
    currentUserInfo["username"] = settings.value("Username").toString();
    currentUserInfo["certName"] = settings.value("CertName").toString();
    currentUserInfo["identity"] = settings.value("Identity").toString();

    settings.endGroup();

    return currentUserInfo;
}

void RegistryManager::setCurentLoginUserId(const QString &userId) { currentLoginUserId = userId; }