/*
 * Copyright 2025 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/>.
 */

#ifndef UTIL_H
#define UTIL_H

#include <kysdk/applications/klabel.h>
#include <kysdk/kysdk-system/libkysysinfo.h>

#include <QDebug>
#include <QFile>
#include <QMessageBox>
#include <QObject>
#include <QPushButton>
#include <QString>

enum class SystemVersion { Version_2403, Version_2503, Version_V11 };

static SystemVersion curSystemVersion() {
    QString version = kdk_system_get_version_detaile().os_version;
    qInfo() << "System version: " << version;

    if (version == "2403") {
        return SystemVersion::Version_2403;
    } else if (version == "2503") {
        return SystemVersion::Version_2503;
    } else {
        return SystemVersion::Version_V11;
    }
}

static void setKLabelFontColor(kdk::KLabel *klabel, QColor color) {
#ifdef SYSTEM_VERSION_2403
    QPalette palette;
    palette.setColor(QPalette::WindowText, color);
    klabel->setPalette(palette);
#else
    klabel->setFontColor(color);
#endif
}

static void showErrorMessage(const QString &message, QWidget *parent) {
    QMessageBox msgBox(parent);
    msgBox.setIcon(QMessageBox::Question);
    msgBox.setText(message);

    QPushButton *cancelButton = msgBox.addButton(QObject::tr("Cancel"), QMessageBox::RejectRole);
    QPushButton *sureButton = msgBox.addButton(QObject::tr("Ok"), QMessageBox::AcceptRole);
    msgBox.setDefaultButton(sureButton);
    msgBox.exec();
}

static bool showModelInUseMessage(QWidget* parent) {
    QMessageBox retryMsgBox(parent);
    retryMsgBox.setText(QObject::tr("Sorry, the model is currently executing another task. Please try again after it is completed.."));
    retryMsgBox.setIcon(QMessageBox::Critical);

    QPushButton *cancelButton = retryMsgBox.addButton(QObject::tr("Cancel"), QMessageBox::RejectRole);
    QPushButton *retryButton = retryMsgBox.addButton(QObject::tr("Retry"), QMessageBox::AcceptRole);
    retryMsgBox.setDefaultButton(retryButton);
    retryMsgBox.exec();

    if ((QPushButton *)retryMsgBox.clickedButton() == retryButton) {
        return true;
    }
    return false;
}

static bool showConfirmMessage(QWidget* parent) {
    QMessageBox confirmMsgBox(parent);
    confirmMsgBox.setText(QObject::tr("Deleting this model may cause apps that rely on this model to not function properly, are you sure you want to remove it?"));
    confirmMsgBox.setIcon(QMessageBox::Question);

    QPushButton *cancelButton = confirmMsgBox.addButton(QObject::tr("Cancel"), QMessageBox::RejectRole);
    QPushButton *sureButton = confirmMsgBox.addButton(QObject::tr("Delete"), QMessageBox::AcceptRole);
    confirmMsgBox.setDefaultButton(sureButton);
    confirmMsgBox.exec();

    if ((QPushButton *)confirmMsgBox.clickedButton() == sureButton) {
        return true;
    }
    return false;
}
#endif  // UTIL_H
