#ifndef PRIVATEMODELCONFIGWIDGET_H
#define PRIVATEMODELCONFIGWIDGET_H

#include <QWidget>
#include <QDialog>
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QTextEdit>
#include <QPushButton>
#include <QIcon>
#include <QComboBox>
#include <QScrollArea>
#include <QScrollBar>
#include <QMessageBox>
#include <QDesktopServices>
#include <QJsonObject>
#include <QCoreApplication>
#include <QJsonDocument>
#include <QJsonArray>
#include <QFile>
#include <QDir>
#include <klabel.h>
#include <QApplication>
#include "privatemodelconfigdialog.h"

#include "privatemodelitemwidget.h"
#include "kaglobal.h"
#include "libukcc/widgets/SettingWidget/addbutton.h"
#include "libukcc/widgets/SettingWidget/settinggroup.h"
#include "ukui-log4qt.h"

#include <kysdk/applications/kswitchbutton.h>
#include <kysdk/applications/kpasswordedit.h>
#include <kysdk/applications/kborderlessbutton.h>
#include <aiconfig/modelconfig.h>

using namespace std;
using namespace kdk;
using namespace kyai::config::model;

class PrivateModelConfigWidget : public QDialog
{
    Q_OBJECT
    friend class PrivateModelConfigDialog;
public:
    explicit PrivateModelConfigWidget(QWidget *parent =nullptr);
    void initLayout();
    void initModelTitleLayout();
    void initModelLayout();
    void showCentered();
    ModelStatus getModelStatus(const QString &modelName);
signals:
    void sigSetSelectPrivateModel(AiCapability capability, QString modelName);
public slots:
    void onEditModelItem(AiCapability capability, QString modelName);
    void onDeleteModelItem(AiCapability capability, QString modelName);
protected:
    bool eventFilter(QObject *watched, QEvent *event) override;
private slots:
    void nlpAddModelItem();
    void visionAddModelItem();
    void speechAddModelItem();
private:
    // 从sdk加载数据
    void loadModelConfig();
    // 信号槽
    void initConnect();
    // 添加模型
    void addModelItem(PrivateModel model);
    // 加载模型
    void loadModelItem(PrivateModel model, QString curSelectedName);
    // 删除模型
    void deleteModelItem(AiCapability capability, QString modelName);
    // 切换选中模型
    void switchModelItem(AiCapability capability, QString modelName);

    // 设置选中模型
    bool setSelectPrivateModelConfig(PrivateModel model);
    // 设置选中模型名称
    bool setSelectPrivateModelName(AiCapability capability, DeployType deployType, QString modelName);
    // 编辑选中模型
    // 返回值表示是否需要关闭当前正在编辑的窗口
    bool editSelectPrivateModel(PrivateModel model, bool isContinuousSelected,
                                bool isRealTimeSelected, bool isValueChanged, QDialog *parentDialog);
    // 点击模型
    void clickPrivateModel(AiCapability capability, QString modelName);

    //恢复模型key默认值
    bool clearModelMultiAuths(AiCapability capability,QString modelName);

    //清除自定义模型
    bool clearCustomModel(AiCapability capability, QString modelName);


    //添加自定义模型
    bool addPrivateModel(PrivateModel model);
    // 更新自定义模型
    bool updatePrivateModel(PrivateModel model);
    // 检查该模型是否已选择
    bool checkModelSelection(AiCapability capability, QString modelName);
    // 授权弹窗
    bool polkit();
    // 获取ModelItemMap
    QMap<QString, PrivateModelItemWidget*>& getModelItemMap(AiCapability capability);
private:

    QVBoxLayout *m_mainVLayout;
    QHBoxLayout *m_titleHLayout;
    QLabel *m_titleLabel;
    QVBoxLayout *m_vLeftLayout = nullptr;
    QLabel *m_iconLabel = nullptr;
    QVBoxLayout *m_privateHLayout = nullptr;
    QLabel *m_privateLabel = nullptr;
    kdk::KLabel *m_descripLabel = nullptr;

    QVBoxLayout *m_baseVLayout = nullptr;
    QScrollArea *m_scrollArea = nullptr;
    QWidget *m_scrollWidget = nullptr;
    QVBoxLayout *m_scrollVLayout = nullptr;

    //NLP大语言模型
    QWidget *m_nlpWidget = nullptr;
    QVBoxLayout *m_nlpDescVLayout = nullptr;
    QHBoxLayout *m_nlpHLayout = nullptr;
    QVBoxLayout *m_nlpVLayout = nullptr;
    QLabel * m_nlpLabel = nullptr;
    kdk::KLabel * m_nlpDescLabel = nullptr;
    KBorderlessButton *m_nlpAddButton = nullptr;

    //确定按钮
    QWidget *m_confirmWidget = nullptr;
    QPushButton *m_confirmPrivateButton = nullptr;
    QHBoxLayout *m_confirmPrivateButtoLayout = nullptr;

public:
    QMap<QString, PrivateModel> m_nlpPrivateModelMap;
    QMap<QString, PrivateModel> m_visionPrivateModelMap;
    QMap<QString, PrivateModel> m_speechPrivateModelMap;

    QMap<QString, PrivateModelItemWidget*> m_nlpPrivateModelItemMap;
    QMap<QString, PrivateModelItemWidget*> m_visionPrivateModelItemMap;
    QMap<QString, PrivateModelItemWidget*> m_speechPrivateModelItemMap;

    QList<PrivateModelItemWidget*> m_nlpPrivateModelItemList;
    QList<PrivateModelItemWidget*> m_visionPrivateModelItemList;
    QList<PrivateModelItemWidget*> m_speechPrivateModelItemList;

    QMap<QString, UkccFrame*> m_nlpPrivateModelMapUI;
    QMap<QString, UkccFrame*> m_visionPrivateModelMapUI;
    QMap<QString, UkccFrame*> m_speechPrivateModelMapUI;

    SettingGroup *m_nlpPrivateModelGroup = nullptr;
    SettingGroup *m_visionPrivateModelGroup = nullptr;
    SettingGroup *m_speechPrivateModelGroup = nullptr;

    QString nlpSelectedPrivateModel;
    QString visionSelectedPrivateModel;
    QString speechSelectedPrivateModel;
    bool continuousSelected = false;
    bool realtimeSelected = false;

};

#endif // PRIVATEMODELCONFIGWIDGET_H
