#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QScrollArea>
#include "space-save-dialog.h"
#include "kysdk/applications/kswitchbutton.h"
#include "kysdk/applications/ktag.h"
#include "flowlayout.h"
#include "idm-dbus-manager.h"
#include "idm-actions-manager.h"
#include <kmessagebox.h>
#include <QApplication>
#include <QMessageBox>
SpaceSaveDialog::SpaceSaveDialog(const QStringList &paths, const QString &conditions, const QString &keyWord, QWidget *parent)
    : m_paths(paths), m_conditions(conditions), m_keyWord(keyWord), kdk::KDialog(parent)
{
    setAttribute(Qt::WA_DeleteOnClose);
    setWindowFlag(Qt::Dialog);
    setWindowIcon(QIcon(":/icons/kylin-data-manager.svg"));
    setWindowTitle(tr("Save as space"));
    setBackgroundRole(QPalette::Base);
    setAutoFillBackground(true);
    setModal(true);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(24, 12, 24, 24);
    mainLayout->setSpacing(0);
    QLabel *nameLabel = new QLabel(tr("Space Name"));
    QLineEdit *nameEdit = new QLineEdit;
    nameEdit->setFixedSize(328, 36);
    nameEdit->setPlaceholderText(tr("Please input space name"));
    connect(nameEdit, &QLineEdit::textChanged, [=](const QString &currentText) {
        if (currentText.toUtf8().size() > 255) {
            QString truncatedText = truncateToByteLimit(currentText, 255);
            nameEdit->blockSignals(true);
            nameEdit->setText(truncatedText);
            nameEdit->blockSignals(false);
        }
    });
    QHBoxLayout *dynamicLayout = new QHBoxLayout;
    //Dynamically Update Space Files
    QLabel *dynamicLabel = new QLabel(tr("Dynamically Update Space Files"));
    kdk::KSwitchButton *switchBtn = new kdk::KSwitchButton;
    switchBtn->setChecked(true);
    dynamicLayout->addWidget(dynamicLabel);
    dynamicLayout->addStretch();
    dynamicLayout->addWidget(switchBtn);
    switchBtn->setDefault(false);

    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->setSpacing(0);
    QPushButton *cancelBtn = new QPushButton(tr("Cancel"));
    cancelBtn->setFixedSize(96, 36);
    QPushButton *okBtn = new QPushButton(tr("Ok"));
    okBtn->setFixedSize(96, 36);
    okBtn->setDefault(true);  // 设置为默认按钮
    okBtn->setAutoDefault(true);  // 确保自动成为默认按钮
    buttonLayout->addStretch();
    buttonLayout->addWidget(cancelBtn);
    buttonLayout->addSpacing(8);
    buttonLayout->addWidget(okBtn);

    mainLayout->addWidget(nameLabel);
    mainLayout->addSpacing(8);
    mainLayout->addWidget(nameEdit);
    mainLayout->addSpacing(8);
    mainLayout->addLayout(dynamicLayout);
    mainLayout->addSpacing(24);
    QWidget *widget = QApplication::topLevelAt(QCursor::pos());
//    QStringList labels = Peony::IDMDBusManager::getInstance()->getIntelligentLabel(m_keyWord);
//    if (!labels.isEmpty()) {
//        QHBoxLayout *tagLayout = new QHBoxLayout;
//        tagLayout->setSpacing(0);
//        QLabel *tagLabel = new QLabel(tr("Tag Recommendations"));
//        QLabel *questionLabel = new QLabel;
//        questionLabel->setFixedSize(QSize(16, 16));
//        questionLabel->setPixmap(QIcon::fromTheme("dialog-question-symbolic").pixmap(16, 16));
//        questionLabel->setToolTip(tr("Based on the file tags in the system and in combination with the current search topic, the recommended file tags generated can be used for quick and intelligent filtering."));
//        tagLayout->addWidget(tagLabel);
//        tagLayout->addSpacing(4);
//        tagLayout->addWidget(questionLabel);
//        tagLayout->addStretch();

//        QWidget *tagWidget = new QWidget;
//        FlowLayout *tagWidgetLayout = new FlowLayout;
//        tagWidget->setLayout(tagWidgetLayout);
//        for (int i = 0; i < labels.count(); ++i) {
//            kdk::KTag *tag = new kdk::KTag;
//            tag->setText(labels.at(i));
//            tag->setTagStyle(kdk::TagStyle::BaseBoderTag);
//            tagWidgetLayout->addWidget(tag);
//        }
//        QScrollArea *scrollArea = new QScrollArea(this);
//        scrollArea->setFrameShape(QFrame::NoFrame);
//        scrollArea->setWidgetResizable(true);
//        scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
//        scrollArea->setWidget(tagWidget);

//        mainLayout->addLayout(tagLayout);
//        mainLayout->addWidget(scrollArea);
//        setFixedSize(376, 334);
//    } else {
        setFixedSize(QSize(376, 246));
//    }

    bool isCreatedFromSearch = keyWord.isEmpty() ? false : true;

    mainLayout->addLayout(buttonLayout);
    mainWidget()->setLayout(mainLayout);
    connect(cancelBtn, &QPushButton::clicked, this, &SpaceSaveDialog::close);
    connect(okBtn, &QPushButton::clicked, this, [=](){
        bool isDymaic = switchBtn->isChecked();
        bool success = Peony::IDMDBusManager::getInstance()->onSpaceCreateRequestWithFiles(nameEdit->text(), m_conditions, isCreatedFromSearch, m_paths);
        qDebug() << "============" << success << m_conditions;
        close();
        if (success) {
            Peony::IDMDBusManager::getInstance()->updateSpacestatus(nameEdit->text(), isDymaic);
        }else{
            QMessageBox::warning(widget,"", tr("Failed to create space %1,%1 is existed").arg(nameEdit->text()), QMessageBox::Ok);
        }
    });
}

SpaceSaveDialog::~SpaceSaveDialog()
{

}
