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

#include <QWidget>
#include <QDialog>
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#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 "cloudmodelconfigwidget.h"
#include "kaglobal.h"

#include "libukcc/widgets/SettingWidget/addbutton.h"
#include "libukcc/widgets/SettingWidget/settinggroup.h"

#include <kysdk/applications/kswitchbutton.h>
#include <kysdk/applications/kpasswordedit.h>
#include <kysdk/applications/kpasswordedit.h>
#include <kysdk/applications/kborderlessbutton.h>
#include <aiconfig/modelconfig.h>
#include <polkit-qt6-1/polkitqt1-authority.h>
#include <ukuistylehelper/ukuistylehelper.h>

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

class ModelConfigDialog : public QDialog
{
    Q_OBJECT
public:
    explicit ModelConfigDialog(PublicCloudModel model, QWidget *parent = nullptr);
    explicit ModelConfigDialog(QWidget *parent = nullptr);
    ~ModelConfigDialog();

    // 添加模型-初始化模型UI
    void initUIData(AiCapability capability, const DeployType deployType);
    // 编辑模型-加载模型数据
    void loadModelData(PublicCloudModel model);
    void moveToCenter();

protected:
    void keyPressEvent(QKeyEvent* event) override;

signals:

public slots:

private slots:
    void onPasswordEditChanged(KPasswordEdit *passwordEdit);
    void onSwitchContinuousChanged(bool isVisible);
    void onSwitchRealTimeChanged(bool isVisible);
    void updateLayout(int index);
    void changeTheme();

private:
    // 布局
    void initLayout();
    // 信号槽
    void initConnect();
    // 根据模型需填内容改变尺寸
    void adjustDialogSize();
    // 根据所填内容更新按钮状态
    void updateButtonState();
    // 检查输入框中是否存在空格
    bool checkIsHasSpaces();
    // 添加model到map
    void addModelToMap(AiCapability capability, const PublicCloudModel &model);

    void accept() override;


    bool isEdit = false;
    AiCapability m_capability;
    PublicCloudModel m_model;

    kdk::KLabel *labelModelName = nullptr;                // 模型类型
    QLabel *labelAPPID = nullptr;
    QLabel *labelApiKey = nullptr;
    kdk::KLabel *labelSecretKey = nullptr;
    QLabel *labelErrorInfo = nullptr;

    QComboBox *comboboxModelName = nullptr;
    kdk::KPasswordEdit  *passwordAPPID = nullptr;
    kdk::KPasswordEdit  *passwordEditApi = nullptr;
    kdk::KPasswordEdit *passwordEditSecret = nullptr;

    // 流式
    QFrame *lineSeparator1 = nullptr;
    QLabel *labelSpeechContinuous = nullptr;
    QLabel *labelAPPIDContinuous = nullptr;
    QLabel *labelApiKeyContinuous = nullptr;
    kdk::KLabel *labelSecretKeyContinuous = nullptr;
    kdk::KSwitchButton *switchContinuous = nullptr;         // 开启：必填
    kdk::KPasswordEdit  *pwdAppIdContinuous = nullptr;
    kdk::KPasswordEdit  *pwdEditApiContinuous = nullptr;
    kdk::KPasswordEdit *pwdEditSecretContinuous = nullptr;
    QWidget *continuousWidget = nullptr;
    // 实时
    QFrame *lineSeparator2 = nullptr;
    QLabel *labelSpeechRealTime = nullptr;
    QLabel *labelAPPIDRealTime = nullptr;
    QLabel *labelApiKeyRealTime = nullptr;
    kdk::KSwitchButton *switchRealTime = nullptr;           // 开启：必填
    kdk::KPasswordEdit  *pwdAppIdRealTime = nullptr;
    kdk::KPasswordEdit  *pwdEditApiRealTime = nullptr;
    QWidget *realTimeWidget = nullptr;

    kdk::KBorderlessButton *btnHelpLink= nullptr;
    QPushButton *buttonCancel = nullptr;
    QPushButton *buttonAccept = nullptr;

    QWidget *contentWidget = nullptr;

    bool isContinuousSelected = true;
    bool isRealTimeSelected = true;

    QMap<QString, QVector<AiCapability>> mapAllModel;
};


#endif // MODELCONFIGDIALOG_H
