#include "bianbu-accounts-plugin.h"
#include "ui/accounts_controller.h"
#include <QIcon>
#include <QTranslator>
#include <QLocale>

AccountsPlugin::AccountsPlugin() = default;
AccountsPlugin::~AccountsPlugin() = default;

QString AccountsPlugin::pluginI18nName() const
{
    return QObject::tr("Accounts");
}

int AccountsPlugin::pluginTypes()
{
    return ACCOUNT;
}

QWidget* AccountsPlugin::pluginUi()
{
    if (!m_widget) {
        // Use the new controller (UI layer), keeping business logic in services/widgets
        auto *controller = new AccountsController();
        m_widget = controller;
    }
    return m_widget;
}

const QString AccountsPlugin::name() const
{
    return QStringLiteral("accounts");
}

QIcon AccountsPlugin::icon() const
{
    return QIcon::fromTheme("avatar-default");
}

bool AccountsPlugin::isEnabled() const
{
    return true;
}

QStringList AccountsPlugin::keywords() const
{
    // lupdate requires direct tr() calls
    tr("Accounts"); tr("User"); tr("Password"); tr("Login"); tr("Profile");
    return keywordsFromSources({
        "Accounts", "User", "Password", "Login", "Profile"
    }, "AccountsPlugin");
}

QTranslator* AccountsPlugin::translator() const
{
    if (m_translator) {
        return m_translator;
    }
    
    // Use bianbu-accounts instead of default bianbu-control-center-accounts
    QString locale = QLocale::system().name();
    QString translationFile = QStringLiteral("/usr/share/locale/%1/LC_MESSAGES/bianbu-accounts.qm").arg(locale);
    
    m_translator = new QTranslator();
    if (m_translator->load(translationFile)) {
        return m_translator;
    }
    
    delete m_translator;
    m_translator = nullptr;
    return nullptr;
}

void AccountsPlugin::pluginLeave()
{
    // No special action needed on leave for now
}
