/*
 * Copyright (C) 2020, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include "aiservicedialog.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPixmap>
#include <QApplication>
#include <QProcess>
#include <ukui-log4qt.h>

#include "windowhelper.h"

AiServiceDialog::AiServiceDialog(QWidget *parent) : QDialog(parent)
{
#ifdef UKUI_WINDOWHELPER_SUPPORT
    UkuiWindowHelper *ukuiWindowHelper = new UkuiWindowHelper(this);
    ukuiWindowHelper->removeTitleBar();
#else
    kdk::UkuiStyleHelper::self()->removeHeader(this);
#endif

    initLayout();
    initConnect();

}

AiServiceDialog::~AiServiceDialog()
{

}

void AiServiceDialog::initLayout()
{
    this->setWindowTitle(QString(" "));
    this->setFixedSize(452, 182);
    this->setStyleSheet("QDialog { background-color: white; }");

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(24, 10, 24, 24);
    mainLayout->setSpacing(8);

    QHBoxLayout *TitleLayout = new QHBoxLayout(this);
    QHBoxLayout *contentLayout = new QHBoxLayout(this);
    contentLayout->setContentsMargins(20, 0, 0, 0);


    iconLabel = new QLabel(this);
    iconLabel->setPixmap(QPixmap(":/icon/icon/hint.svg").scaled(16, 16));
    iconLabel->setContentsMargins(28, 12, 0, 144);
    titleLabel = new QLabel(tr("<b>AI service unavailable</b>"));
    titleLabel->setFixedSize(380, 20);  

    TitleLayout->addWidget(iconLabel);
    TitleLayout->addWidget(titleLabel);

    mainLayout->addLayout(TitleLayout);

    detailLabel = new QLabel(tr("The AI subsystem is not detected. The AI service of the application cannot be used. Please go to System Settings > AI Module Management page to install it."));
    detailLabel->setWordWrap(true);
    detailLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    contentLayout->addWidget(detailLabel);
    mainLayout->addLayout(contentLayout);

    QHBoxLayout *buttonLayout = new QHBoxLayout(this);
    cancelButton = new QPushButton(tr("Cancel"));
    cancelButton->setAccessibleName("kylin-aiassistant_QQPushButton_cancelButton");
    cancelButton->setFixedSize(94, 36);

    goButton = new QPushButton(tr("go now"));
    goButton->setAccessibleName("kylin-aiassistant_QPushButton_okButton");

    goButton->setProperty("isImportant", true);
    goButton->setDefault(true);

    buttonLayout->addStretch();
    buttonLayout->addWidget(cancelButton);
    buttonLayout->addWidget(goButton);
    mainLayout->addStretch();
    mainLayout->addLayout(buttonLayout);
}

void AiServiceDialog::initConnect()
{
    connect(cancelButton, &QPushButton::clicked, this, &AiServiceDialog::reject);
    connect(goButton, &QPushButton::clicked, this, &AiServiceDialog::openUkuiControlCenter);
}

void AiServiceDialog::openUkuiControlCenter()
{
    QProcess process;
    QString command = "ukui-control-center";
    QStringList arguments;
    arguments << "-m" << "aisubsystem";
    process.startDetached(command, arguments);
    process.waitForFinished(-1);
    // 检查进程是否正常退出
    if (process.exitStatus() == QProcess::NormalExit) {
        KyInfo() << "ukui-control-center successfully!";
    } else {
        KyInfo() << "Failed  to open ukui-control-center";
    }
    this->close();
}
