/*
   SPDX-FileCopyrightText: 2025-2026 Laurent Montel <montel@kde.org>

   SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "textautogeneratemenutextmanager.h"

#include <KConfigGroup>
#include <QRegularExpression>
using namespace Qt::Literals::StringLiterals;
using namespace TextAutoGenerateText;
TextAutoGenerateMenuTextManager::TextAutoGenerateMenuTextManager(QObject *parent)
    : QObject{parent}
{
}

TextAutoGenerateMenuTextManager::~TextAutoGenerateMenuTextManager() = default;

QList<TextAutoGenerateMenuTextInfo> TextAutoGenerateMenuTextManager::textInfos() const
{
    return mTextInfos;
}

void TextAutoGenerateMenuTextManager::setTextInfos(const QList<TextAutoGenerateMenuTextInfo> &newTextInfos)
{
    if (mTextInfos != newTextInfos) {
        mTextInfos = newTextInfos;
        Q_EMIT textInfoChanged();
    }
}

void TextAutoGenerateMenuTextManager::load()
{
    QList<TextAutoGenerateMenuTextInfo> infos;
    KSharedConfig::Ptr config;
    const QStringList keyGroups = keyRecorderList(config);

    for (const QString &groupName : keyGroups) {
        const KConfigGroup group = config->group(groupName);
        TextAutoGenerateMenuTextInfo info;
        info.setRequestText(group.readEntry(u"RequestedText"_s));
        info.setEnabled(group.readEntry(u"Enabled"_s, true));
        info.setOrder(group.readEntry(QStringLiteral("Order"), 0));
        infos.append(std::move(info));
    }
    std::sort(infos.begin(), infos.end(), [&](const auto &firstItem, const auto &secondItem) {
        const int firstOrder = firstItem.order();
        const int secondOrder = secondItem.order();
        if (firstOrder == secondOrder) {
            return firstItem.requestText() < secondItem.requestText();
        }
        return firstOrder < secondOrder;
    });
    setTextInfos(infos);
}

void TextAutoGenerateMenuTextManager::save()
{
    KSharedConfig::Ptr config;
    const QStringList filterGroups = keyRecorderList(config);

    for (const QString &group : filterGroups) {
        config->deleteGroup(group);
    }
    for (int i = 0, total = mTextInfos.count(); i < total; ++i) {
        const QString groupName = u"AskIA #%1"_s.arg(i);
        KConfigGroup group = config->group(groupName);
        const TextAutoGenerateMenuTextInfo &info = mTextInfos.at(i);
        group.writeEntry(u"RequestedText"_s, info.requestText());
        group.writeEntry(u"Enabled"_s, info.enabled());
        group.writeEntry(u"Order"_s, info.order());
    }
    config->sync();
}

QStringList TextAutoGenerateMenuTextManager::keyRecorderList(KSharedConfig::Ptr &config) const
{
    config = KSharedConfig::openConfig();
    const static QRegularExpression keyGroupRegularExpression(u"AskIA #\\d+"_s);
    const QStringList keyGroups = config->groupList().filter(keyGroupRegularExpression);
    return keyGroups;
}

#include "moc_textautogeneratemenutextmanager.cpp"
