#include "documentstylelistmodel.h"
#include <QDebug>

DocumentStyleListModel *DocumentStyleListModel::mInstance = nullptr;

DocumentStyleListModel *DocumentStyleListModel::getInstance()
{
    if (mInstance == nullptr) {
        mInstance = new DocumentStyleListModel;
    }
    return mInstance;
}

DocumentStyleListModel::DocumentStyleListModel(QObject *parent)
    : QAbstractListModel(parent)
    , m_templateManager(DocTemplateManager::getInstance())
{
    // 连接信号
    connect(m_templateManager, &DocTemplateManager::templateAdded,
            this, &DocumentStyleListModel::onTemplateAdded);
    connect(m_templateManager, &DocTemplateManager::templateRemoved,
            this, &DocumentStyleListModel::onTemplateRemoved);
    connect(m_templateManager, &DocTemplateManager::templateUpdated,
            this, &DocumentStyleListModel::onTemplateUpdated);

    // 加载初始数据
    loadTemplates();
}

DocumentStyleListModel::~DocumentStyleListModel()
{
}

int DocumentStyleListModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return m_templates.size();
}

QVariant DocumentStyleListModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.row() < 0 || index.row() >= m_templates.size()) {
        return QVariant();
    }

    const DocTemplate &templateData = m_templates.at(index.row());

    switch (role) {
    case TemplateUuid:
        return templateData.uuid;
    case TemplateName:
        return templateData.templateName;
    case TemplateOutline:
        return templateData.templateOutline;
    case TemplateTone:
        return templateData.templateTone;
    case TemplateCount:
        return templateData.templateCount;
    case TemplatePrompt:
        return templateData.templatePrompt;
    case TemplateTip:
        return templateData.templateTip;
    case TemplateProperty:
        return templateData.templateProperty;
    case IsCustom:
        return templateData.isCustom;
    case IsReference:
        return templateData.isReference;
    case IsOutline:
        return templateData.isOutline;
    default:
        return QVariant();
    }
}

QHash<int, QByteArray> DocumentStyleListModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[TemplateUuid] = "templateUuid";
    roles[TemplateName] = "templateName";
    roles[TemplateOutline] = "templateOutline";
    roles[TemplateTone] = "templateTone";
    roles[TemplateCount] = "templateCount";
    roles[TemplatePrompt] = "templatePrompt";
    roles[TemplateTip] = "templateTip";
    roles[TemplateProperty] = "templateProperty";
    roles[IsCustom] = "isCustom";
    roles[IsReference] = "isReference";
    roles[IsOutline] = "isOutline";

    return roles;
}

bool DocumentStyleListModel::addTemplate(const DocTemplate &info)
{
    return m_templateManager->addTemplate(info);
}

bool DocumentStyleListModel::removeTemplate(const QString &uuid)
{
    return m_templateManager->removeTemplate(uuid);
}

bool DocumentStyleListModel::updateTemplate(const QString& uuid, const DocTemplate& newData)
{
    return m_templateManager->updateTemplate(uuid, newData);
}

QVariantMap DocumentStyleListModel::getTemplate(const QString &uuid) const
{
    DocTemplate templateData = m_templateManager->getTemplate(uuid);
    if (templateData.uuid.isEmpty()) {
        return QVariantMap();
    }

    QVariantMap result;
    result["uuid"] = templateData.uuid;
    result["name"] = templateData.templateName;
    result["outline"] = templateData.templateOutline;
    result["tone"] = templateData.templateTone;
    result["count"] = templateData.templateCount;
    result["prompt"] = templateData.templatePrompt;
    result["tip"] = templateData.templateTip;
    result["property"] = templateData.templateProperty;
    result["isCustom"] = templateData.isCustom;
    result["isReference"] = templateData.isReference;
    result["isOutline"] = templateData.isOutline;

    return result;
}

QVariantMap DocumentStyleListModel::getTemplateByIndex(int index) const
{
    if (index < 0 || index >= m_templates.size()) {
        return QVariantMap();
    }

    const DocTemplate &templateData = m_templates.at(index);
    QVariantMap result;
    result["uuid"] = templateData.uuid;
    result["name"] = templateData.templateName;
    result["outline"] = templateData.templateOutline;
    result["tone"] = templateData.templateTone;
    result["count"] = templateData.templateCount;
    result["prompt"] = templateData.templatePrompt;
    result["tip"] = templateData.templateTip;
    result["property"] = templateData.templateProperty;
    result["isCustom"] = templateData.isCustom;
    result["isReference"] = templateData.isReference;
    result["isOutline"] = templateData.isOutline;

    return result;
}

int DocumentStyleListModel::templateCount() const
{
    return m_templates.size();
}

bool DocumentStyleListModel::isCustomTemplate(const QString& uuid) const
{
    int index = findTemplateIndex(uuid);
    if (index != -1) {
        return isTemplateCustom(m_templates[index]);
    }
    return false;
}

void DocumentStyleListModel::onTemplateAdded(const DocTemplate &newTemplate)
{
    beginInsertRows(QModelIndex(), m_templates.size(), m_templates.size());
    m_templates.append(newTemplate);
    endInsertRows();

    emit templateAdded(newTemplate);
    qDebug() << "Template added:" << newTemplate.templateName;
}

void DocumentStyleListModel::onTemplateRemoved(const QString &uuid)
{
    int index = findTemplateIndex(uuid);
    if (index != -1) {
        beginRemoveRows(QModelIndex(), index, index);
        m_templates.removeAt(index);
        endRemoveRows();

        emit templateRemoved(uuid);
        qDebug() << "Template removed:" << uuid;
    }
}

void DocumentStyleListModel::onTemplateUpdated(const DocTemplate &updatedTemplate)
{
    int index = findTemplateIndex(updatedTemplate.uuid);
    if (index != -1) {
        m_templates[index] = updatedTemplate;
        QModelIndex modelIndex = createIndex(index, 0);
        emit dataChanged(modelIndex, modelIndex);

        emit templateUpdated(updatedTemplate);
        qDebug() << "Template updated:" << updatedTemplate.templateName;
    }
}

void DocumentStyleListModel::loadTemplates()
{
    beginResetModel();

    // 清空当前模板
    m_templates.clear();

    // 从管理器获取所有模板
    QList<DocTemplate> allTemplates = m_templateManager->getAllTemplates();

    // 将 QList 转换为 QVector
    m_templates = QVector<DocTemplate>::fromList(allTemplates);

    endResetModel();

}

int DocumentStyleListModel::findTemplateIndex(const QString &uuid) const
{
    for (int i = 0; i < m_templates.size(); ++i) {
        if (m_templates[i].uuid == uuid) {
            return i;
        }
    }
    return -1;
}

bool DocumentStyleListModel::isTemplateCustom(const DocTemplate& templateData) const
{
    return templateData.isCustom;
}
