#include "msgtemplate.h"
#include <QMessageBox>
#include <QInputDialog>
#include <QDebug>
#include <QWindow>

MsgTemplate::MsgTemplate(QObject *parent)
    : QObject{parent}
{

}

void MsgTemplate::setParent(QObject *parent)
{
    m_parent = qobject_cast<QQuickWindow*>(parent);
}

// 显示添加文章类型对话框
void MsgTemplate::onShowAddArticleTypeDialog()
{
    qDebug() << "onShowAddArticleTypeDialog slot called!";
    WritingAddItemDialog addDialog;

    if (m_parent) {
        addDialog.winId();
        if (addDialog.windowHandle()) {
            addDialog.windowHandle()->setTransientParent(m_parent);
        }
    }

    // 连接添加信号
    connect(&addDialog, &WritingAddItemDialog::sigDocItemAdd,
            this, [](const DocTemplate &info) {
        DocumentStyleListModel::getInstance()->addTemplate(info);
    });

    addDialog.exec();
}

// 显示编辑文章类型对话框
void MsgTemplate::onShowEditArticleTypeDialog(const QString &uuid)
{
    QVariantMap templateData = DocumentStyleListModel::getInstance()->getTemplate(uuid);
    if (!templateData.isEmpty()) {
        WritingEditItemDialog editDialog;

        if (m_parent) {
            editDialog.winId();
            if (editDialog.windowHandle()) {
                editDialog.windowHandle()->setTransientParent(m_parent);
            }
        }

        editDialog.setEditData(templateData);

        connect(&editDialog, &WritingEditItemDialog::sigDocItemUpdated,
                this, [](const QVariantMap &updatedData) {
            // 更新模型数据
            DocTemplate updatedTemplate;
            updatedTemplate.uuid = updatedData["uuid"].toString();
            updatedTemplate.templateName = updatedData["name"].toString();
            updatedTemplate.templateOutline = updatedData["outline"].toString();
            updatedTemplate.templateTone = updatedData["tone"].toString();
            updatedTemplate.templateCount = updatedData["count"].toInt();
            updatedTemplate.templatePrompt = updatedData["prompt"].toString();
            updatedTemplate.templateProperty = updatedData["property"].toString();
            updatedTemplate.templateTip = updatedData["tip"].toString();
            updatedTemplate.isReference = updatedData["isReference"].toBool();
            updatedTemplate.isOutline = updatedData["isOutline"].toBool();
            updatedTemplate.isCustom = true;

            DocumentStyleListModel::getInstance()->updateTemplate(updatedTemplate.uuid, updatedTemplate);
        });

        editDialog.exec();
    }
}

void MsgTemplate::onArticleItemDeleteRequested(const QString &uuid)
{
    qDebug()<<"5656565656";
    QMessageBox msgBox;
    if (m_parent) {
        msgBox.winId();
        if (msgBox.windowHandle()) {
            msgBox.windowHandle()->setTransientParent(m_parent);
        }
    }

    msgBox.setWindowTitle(tr("Confirm"));
    msgBox.setText(tr("Once the article type is deleted, it cannot be restored. Are you sure you want to delete it?"));
    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    msgBox.setDefaultButton(QMessageBox::No);

    int result = msgBox.exec();

    if (result == QMessageBox::Yes) {
        DocumentStyleListModel::getInstance()->removeTemplate(uuid);
    }
}

// 显示添加语气风格对话框
void MsgTemplate::onShowAddToneStyleDialog()
{
    ToneAddItemDialog addDialog;

    if (m_parent) {
        addDialog.winId();
        if (addDialog.windowHandle()) {
            addDialog.windowHandle()->setTransientParent(m_parent);
        }
    }

    connect(&addDialog, &ToneAddItemDialog::sigToneItemAdd,
            this, [](const ToneStyle &info) {
        WritingToneListModel::getInstance()->addTone(info);
    });

    addDialog.exec();
}

// 显示编辑语气风格对话框
void MsgTemplate::onShowEditToneStyleDialog(const QString &uuid)
{
    QVariantMap toneData = WritingToneListModel::getInstance()->getTone(uuid);
    if (!toneData.isEmpty()) {
        ToneEditItemDialog editDialog;

        if (m_parent) {
            editDialog.winId();
            if (editDialog.windowHandle()) {
                editDialog.windowHandle()->setTransientParent(m_parent);
            }
        }

        editDialog.setEditData(toneData);

        connect(&editDialog, &ToneEditItemDialog::sigToneItemUpdated,
                this, [](const QVariantMap &updatedData) {
            ToneStyle updatedTone;
            updatedTone.uuid = updatedData["uuid"].toString();
            updatedTone.toneName = updatedData["name"].toString();
            updatedTone.toneOriginal = updatedData["original"].toString();
            updatedTone.toneTarget = updatedData["target"].toString();
            updatedTone.toneInstruction = updatedData["instruction"].toString();
            updatedTone.isCustom = true;

            WritingToneListModel::getInstance()->updateTone(updatedTone.uuid, updatedTone);
        });

        editDialog.exec();
    }
}

void MsgTemplate::onToneItemDeleteRequested(const QString &uuid)
{
    QMessageBox msgBox;
    if (m_parent) {
        msgBox.winId();
        if (msgBox.windowHandle()) {
            msgBox.windowHandle()->setTransientParent(m_parent);
        }
    }

    msgBox.setWindowTitle(tr("Confirm"));
    msgBox.setText(tr("Once the tone style is deleted, it cannot be restored. Are you sure you want to delete it?"));
    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    msgBox.setDefaultButton(QMessageBox::No);

    int result = msgBox.exec();

    if (result == QMessageBox::Yes) {
        WritingToneListModel::getInstance()->removeTone(uuid);
    }
}

// 文章类型被选中
void MsgTemplate::onArticleTypeSelected(const QString &uuid)
{
    qDebug() << "Article type selected:" << uuid;
    // 发出信号通知QML处理跳转和选中模板的逻辑
    emit sigArticleTypeSelectedForWriting(uuid);
}
