#include "InteractiveData.h"
#include "InteractiveHttp.h"
#include "../httpclient/HttpClient.h"

InteractiveData* InteractiveData::pInstance = nullptr;

InteractiveData::InteractiveData()
{
    clearToken();
}

void InteractiveData::clearToken()
{
    m_token.clear();
    InteractiveHttp::strToken.clear();
    HttpClient::clearToken();
}

InteractiveData *InteractiveData::instance()
{
    if (!pInstance) {
        pInstance = new InteractiveData;
    }
    return pInstance;
}

InteractiveData *InteractiveData::create(QQmlEngine *, QJSEngine *)
{
    return instance();
}

bool InteractiveData::isHavePower(const QString &operation)
{
    // 如果角色包含超级管理员
    if (m_roles.contains(QString("admin")))
    {
        return true;
    }
    // 如果是其他用户，判断是否有执行该操作的权利
    else if (m_permissions.contains(operation))
    {
        return true;
    }
    // 如果没有权利执行该操作
    else
    {
        return false;
    }
}

QSet<QString> InteractiveData::roles()
{
    return m_roles;
}

void InteractiveData::setRoles(const QSet<QString> &roles)
{
    m_roles = roles;
    emit rolesChanged();
}

QSet<QString> InteractiveData::permissions()
{
    return m_permissions;
}

void InteractiveData::setPermissions(const QSet<QString> &permissions)
{
    m_permissions = permissions;
    emit permissionsChanged();
}

void InteractiveData::notifyRequestTimedOut()
{
    emit requestTimedOut();
}

InteractiveData::~InteractiveData()
{
}

