/*
 * 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 AISUBSYSTEM_H
#define AISUBSYSTEM_H

#include <QObject>
#include <QVBoxLayout>
#include <QFrame>
#include <QListWidget>
#include <QListWidgetItem>
#include <QMessageBox>
#include <QTranslator>

#include <kysdk/applications/kbackgroundgroup.h>

#include "modelwidget.h"
#include "modelcallpriority.h"
#include "cloudmodelconfigwidget.h"
#include "localmodelconfigwidget.h"
#include "privatemodelconfigwidget.h"
#include "appversionerrorwarningwidget.h"

struct PriorityItem {
    int priority;
    DeployType deployType;
    ModelWidget *modelWidget;
};

class TestAiSubSystem : public QObject
{
    Q_OBJECT

public:
    explicit TestAiSubSystem(QObject *parent = nullptr);
    //通过这个接口返回模型配置UI
    QWidget* createModelConfigWidget();
    QWidget* createComputePowerSharingWidget();
    static CloudModelConfigWidget* getCloudModelConfigPage();
    static LocalModelConfigWidget* getLocalModelConfigPage();
    static PrivateModelConfigWidget* getPrivateModelConfigPage();

private:
    void initConnect();
    void initModelGroup();

    void saveModelPriority();
    void updateModelPriority(const QMap<DeployType, int> &priorityMap);

    void sortItemsByPriority();

    void moveItemUp(DeployType deployType);
    void updateUIOrder();

    DeployType getDeployTypeByFrame(QFrame *frame);
    bool hasLocalModel(); // 判断是否需要隐藏本地模型

public slots:
    void onModelPriorityChanged(DeployType topPriorityModelType);

private:
    QTranslator *m_translator = nullptr;

    QLabel *m_modelConfigLabel = nullptr;

    QWidget *m_msgWidget;
    AppVersionErrorWarningWidget *m_appTipsWidget = nullptr;

    ModelWidget *m_publicCloudModel = nullptr;
    ModelWidget *m_ondeviceModel = nullptr;
    ModelWidget *m_privateModel = nullptr;

    QFrame *m_tipsFrame = nullptr;

    QFrame *m_modelCallPriorityFrame = nullptr;
    ModelCallPriority *m_modelCallPriorityWidget = nullptr;

    KBackgroundGroup *m_modelGroup = nullptr;

    QWidget *m_mainWidget = nullptr;
    QVBoxLayout *m_mainVLayout = nullptr;

    QMap<DeployType, int> m_priorityMap;

    QList<PriorityItem> m_modelPriorityList;
};

#endif // AISUBSYSTEM_H
