#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 <aisubsystemplugininterface.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, KylinAiSubSystemPluginInterface
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.kylinaisubsystem.PluginInterface")
    Q_INTERFACES(KylinAiSubSystemPluginInterface)

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
