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

WritingToneListModel *WritingToneListModel::mInstance = nullptr;

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

WritingToneListModel::WritingToneListModel(QObject *parent)
    : QAbstractListModel(parent)
    , m_toneManager(ToneStyleManager::getInstance())
{
    // 连接信号
    connect(m_toneManager, &ToneStyleManager::toneStyleAdded,
            this, &WritingToneListModel::onToneStyleAdded);
    connect(m_toneManager, &ToneStyleManager::toneStyleRemoved,
            this, &WritingToneListModel::onToneStyleRemoved);
    connect(m_toneManager, &ToneStyleManager::toneStyleUpdated,
            this, &WritingToneListModel::onToneStyleUpdated);

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

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

QVariant WritingToneListModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.row() < 0 || index.row() >= m_tones.size()) {
        KyInfo() << "Invalid index";
        return QVariant();
    }

    const ToneStyle &tone = m_tones.at(index.row());

    switch (role) {
    case ToneUuid:
        return tone.uuid;
    case ToneName:
        return tone.toneName;
    case ToneOriginal:
        return tone.toneOriginal;
    case ToneTarget:
        return tone.toneTarget;
    case ToneInstruction:
        return tone.toneInstruction;
    case IsCustom:
        return tone.isCustom;
    case FileURL:
        return tone.fileURL;
    default:
        return QVariant();
    }
}

QHash<int, QByteArray> WritingToneListModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[ToneUuid] = "toneUuid";
    roles[ToneName] = "toneName";
    roles[ToneOriginal] = "toneOriginal";
    roles[ToneTarget] = "toneTarget";
    roles[ToneInstruction] = "toneInstruction";
    roles[IsCustom] = "isCustom";
    roles[FileURL] = "fileURL";
    return roles;
}

bool WritingToneListModel::addTone(const ToneStyle &info)
{
    return m_toneManager->addToneStyle(info);
}

bool WritingToneListModel::removeTone(const QString &uuid)
{
    return m_toneManager->removeToneStyle(uuid);
}

bool WritingToneListModel::updateTone(const QString& uuid, const ToneStyle& newData)
{
    return m_toneManager->updateToneStyle(uuid, newData);
}

QVariantMap WritingToneListModel::getTone(const QString &uuid) const
{
    ToneStyle toneData = m_toneManager->getToneStyle(uuid);
    if (toneData.uuid.isEmpty()) {
        return QVariantMap();
    }

    QVariantMap result;
    result["uuid"] = toneData.uuid;
    result["name"] = toneData.toneName;
    result["original"] = toneData.toneOriginal;
    result["target"] = toneData.toneTarget;
    result["instruction"] = toneData.toneInstruction;
    result["isCustom"] = toneData.isCustom;
    result["fileURL"] = toneData.fileURL;

    return result;
}

QVariantMap WritingToneListModel::getToneByIndex(int index) const
{
    if (index < 0 || index >= m_tones.size()) {
        return QVariantMap();
    }

    const ToneStyle &toneData = m_tones.at(index);
    QVariantMap result;
    result["uuid"] = toneData.uuid;
    result["name"] = toneData.toneName;
    result["original"] = toneData.toneOriginal;
    result["target"] = toneData.toneTarget;
    result["instruction"] = toneData.toneInstruction;
    result["isCustom"] = toneData.isCustom;
    result["fileURL"] = toneData.fileURL;

    return result;
}

int WritingToneListModel::toneCount() const
{
    return m_tones.size();
}

bool WritingToneListModel::isCustomTone(const QString& uuid) const
{
    int index = findToneIndex(uuid);
    if (index != -1) {
        return isToneCustom(m_tones[index]);
    }
    return false;
}

void WritingToneListModel::onToneStyleAdded(const ToneStyle &newToneStyle)
{
    beginInsertRows(QModelIndex(), m_tones.size(), m_tones.size());
    m_tones.append(newToneStyle);
    endInsertRows();

    emit toneAdded(newToneStyle);
    qDebug() << "Tone added:" << newToneStyle.toneName;
}

void WritingToneListModel::onToneStyleRemoved(const QString &uuid)
{
    int index = findToneIndex(uuid);
    if (index != -1) {
        beginRemoveRows(QModelIndex(), index, index);
        m_tones.removeAt(index);
        endRemoveRows();

        emit toneRemoved(uuid);
        qDebug() << "Tone removed:" << uuid;
    }
}

void WritingToneListModel::onToneStyleUpdated(const ToneStyle &updatedToneStyle)
{
    int index = findToneIndex(updatedToneStyle.uuid);
    if (index != -1) {
        m_tones[index] = updatedToneStyle;
        QModelIndex modelIndex = createIndex(index, 0);
        emit dataChanged(modelIndex, modelIndex);

        emit toneUpdated(updatedToneStyle);
        qDebug() << "Tone updated:" << updatedToneStyle.toneName;
    }
}

void WritingToneListModel::loadTones()
{
    beginResetModel();

    // 清空当前语气风格
    m_tones.clear();

    // 从管理器获取所有语气风格
    QList<ToneStyle> allTones = m_toneManager->getAllToneStyles();

    // 将 QList 转换为 QVector
    m_tones = QVector<ToneStyle>::fromList(allTones);

    endResetModel();
}

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

bool WritingToneListModel::isToneCustom(const ToneStyle& toneStyle) const
{
    return toneStyle.isCustom;
}
