#ifndef LOCALMODELCONFIGWIDGET_H
#define LOCALMODELCONFIGWIDGET_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 <QJsonDocument>
#include <QJsonArray>
#include <QFile>
#include <QDir>
#include <QFileInfo>
#include <klabel.h>
#include <QApplication>
#include <QProcess>

#include "localmodelitem.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 <kysdk/applications/kmessagebox.h>
#include <aiconfig/modelconfig.h>
#include <polkit-qt5-1/polkitqt1-authority.h>

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

class LocalModelConfigWidget : public QDialog
{
    Q_OBJECT
public:
    explicit  LocalModelConfigWidget(QWidget *parent = nullptr);
    void initLayout();
    void initModelTitleLayout();
    void initModellayout();
    void showCentered();
    ModelStatus getModelStatus(QString modelName);

signals:
    void sigSetSelectLocalModel(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;
    void closeEvent(QCloseEvent *event) override;
private:
    // 加载nlp模型
    void loadNlpModelConfig();
    // 加载除nlp以外的其他模型
    void loadOtherModelConfig();
    // 从sdk加载数据
    void loadModelConfig();
    // 信号槽
    void initConnect();

    void openSoftwareStore();
    // 添加模型
    void addModelItem(PublicLocalModel model);
    // 加载模型
    void loadModelItem(PublicLocalModel model,QString curSelectedName);
    // 删除模型
    void deleteModelItem(AiCapability capability, QString modelName);
    // 切换选中模型
    void switchModelItem(AiCapability capability, QString modelName);
    // 设置选中模型名称
    bool setSelectLocalModelName(AiCapability capability, DeployType deployType, QString modelName);
    // 编辑选中模型
    void editSelectLocalModel(PublicLocalModel model, bool isContinuousSelected, bool isRealTimeSelected, bool isValueChanged);
    // 点击模型
    void clickLocalModel(AiCapability capability, QString modelName);
    // 恢复模型key默认值
    bool clearModelMultiAuths(AiCapability capability, QString modelName);
    // 更新ModelMap数据
    void updateModelMap(PublicLocalModel editModel, AiCapability capability, QString modelName);
    // 检查该模型是否已选择
    bool checkModelSelection(AiCapability capability, QString modelName);
    // 检查该模型是否已安装
    bool checkModelInstalled(AiCapability capability, QString modelName);
    // 模型未安装弹窗
    void showNotInstallMessage(const QString &modelName);
    // 报错弹窗
    void showErrorMessage(const QString &message, QWidget *parent);
    // 重试弹窗
    bool showModelInUseMessage(QWidget* parent);
    // 确认弹窗
    bool showConfirmMessage(QWidget* parent);

    // 授权弹窗
    bool polkit();
    // 获取ModelItemMap
    QMap<QString, LocalModelItem*>& getModelItemMap(AiCapability capability);
    // 检查当前本地模型是否安装
    bool checkCurrentLocalModelInstall(const QString &filePath);

private:
    QVBoxLayout *m_mainVLayout;
    QHBoxLayout *m_titleHLayout;
    QLabel *m_titleLabel;
    QVBoxLayout *m_vLeftLayout = nullptr;
    QLabel *m_iconLabel = nullptr;
    QVBoxLayout *m_localHLayout = nullptr;
    QLabel *m_localLabel = 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;

    KBorderlessButton *m_installButton = nullptr;
    KBorderlessButton *m_updateOrUninstallBtn = nullptr;
    QHBoxLayout *m_nlpLabelHLayout = nullptr;
    QLabel * m_nlpLabel = nullptr;
    QPushButton *m_tipButton = nullptr;
    kdk::KLabel * m_nlpDescLabel = nullptr;

    QHBoxLayout *m_nlpFrameHLayout = nullptr;
    QLabel *m_nlpFrameLabelText = nullptr;
    QLabel *m_nlpFrameSelectedIcon = nullptr;

    // 系统内置模型 图像识别、向量化、语音处理等其他系统运行所需的模型
    QVBoxLayout *m_otherVLayout = nullptr;
    QVBoxLayout *m_otherDescVLayout = nullptr;
    QLabel *m_otherLabel = nullptr;
    kdk::KLabel *m_otherDescLabel = nullptr;
    QLabel *m_otherFrameLabelText = nullptr;

    // VISION视觉模型
    QWidget *m_visionWidget = nullptr;
    QVBoxLayout *m_visionDescVLayout = nullptr;
    QHBoxLayout *m_visionHLayout = nullptr;
    QVBoxLayout *m_visionVLayout = nullptr;
    QLabel *m_visionLabel = nullptr;
    kdk::KLabel *m_visionDescrLabel = nullptr;

    // SPEECH语音模型
    QWidget *m_speechWidget = nullptr;
    QVBoxLayout *m_speechDescVlayout =nullptr;
    QHBoxLayout *m_speechHLayout = nullptr;
    QVBoxLayout *m_speechVLayout = nullptr;
    QLabel * m_speechLabel = nullptr;
    kdk::KLabel * m_speechDescLabel = nullptr;

    //确定按钮
    QPushButton *m_confirmLocalButton = nullptr;
    QHBoxLayout *m_confirmLocalButtoLayout = nullptr;

public:
    QMap<QString, PublicLocalModel> m_nlpLocalModelMap;
    QMap<QString, PublicLocalModel> m_visionLocalModelMap;
    QMap<QString, PublicLocalModel> m_speechLocalModelMap;

    QMap<QString, LocalModelItem*> m_nlpLocalModelItemMap;
    QMap<QString, LocalModelItem*> m_visionLocalModelItemMap;
    QMap<QString, LocalModelItem*> m_speechLocalModelItemMap;

    QMap<QString, UkccFrame*> m_nlpLocalModelMapUI;
    QMap<QString, UkccFrame*> m_visionLocalModelMapUI;
    QMap<QString, UkccFrame*> m_speechLocalModelMapUI;

    SettingGroup *m_nlpLocalModelGroup = nullptr;
    SettingGroup *m_visionLocalModelGroup = nullptr;
    SettingGroup *m_speechLocalModelGroup = nullptr;
    SettingGroup *m_otherModelGroup = nullptr;

    QString nlpSelectedLocalModel;
    QString visionSelectedLocalModel;
    QString speechSelectedLocalModel;
    bool continuousSelected = false;
    bool realtimeSelected = false;
};

#endif // LOCALMODELCONFIGWIDGET_H
