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

#include <QWidget>
#include <QGSettings>
#include <QMouseEvent>
#include <QMenu>
#include <QLabel>
#include <QVariant>
#include <QToolButton>
#include <QHBoxLayout>

#include "ktoolbutton.h"
#include "kaglobal.h"
#include <aiconfig/modelconfig.h>

#include "updatenotificationwidget.h"
#include "util.h"

#define THEME_QT_SCHEMA  "org.ukui.style"
#define TEMP_REBOOT_FILE "/tmp/temp_reboot_file.txt"
using namespace std;
using namespace kyai::config::model;

enum MessageTipType{
    InfoMessage =0x0,
    WarningMessage,
    PersistentMessage
};

class LocalModelItem :public QWidget
{
    Q_OBJECT
public:
    explicit LocalModelItem(PublicLocalModel model, SystemVersion curSystemVersion, QWidget *parent = nullptr);
    ~LocalModelItem();

    void showSelectedIcon(bool flag);
    bool isSelected();
    AiCapability getAICapability();
    QString getModelName();
    QString getModelDebName();
    bool getIsStartDownloading();
    void showMessageTips(MessageTipType type,const QString &msg,const int &duration);

private:
    void initModelDownloadLayout();
    void initLayout();
    void initMenu();
    void initInstallSuccessMsgBox();
    void initConnect();
signals:
    void sigEdit(AiCapability capability, QString modelName);
    void sigDelete(AiCapability capability, QString modelName);
public slots:
    void checkHasNewVersion();
    bool createRebootTempFile();
    bool checkHasRebootTempFile();
    void sldownModel();
    void startNetErrorTips();
    void slUpdateModelInstallStatus(int value,QString status ,QString details);
    void slUpdateModelInstallFinished(bool isSuccess,QStringList upgradeGroup,QString errorStr,QString errorDesc);
    void slUpdateCancelButton(bool isInstalling);
private:
    QHBoxLayout *m_mainHLayout;
    //模型名称
    QLabel *m_modelNameLabel;

    //模型面板
    QHBoxLayout *m_panelHLayout;
    //信息
    QHBoxLayout *m_messageHLayout;
    QWidget *m_messageWidget;
    QLabel *m_tipsLabel;
    QToolButton *m_moreButton = nullptr;  // 更多
    QToolButton *m_downButton = nullptr;  //下载
    KToolButton *m_loadButton = nullptr;  // 加载
    QMenu *m_menu;
    //下载进度条
    QHBoxLayout *m_progressHLayout;
    UpdateNotificationWidget *updateProgressBar =nullptr;
    //选择框
    QLabel *m_selectedIconLabel = nullptr;// 已选择状态
    //下载成功对话框
    QMessageBox *m_installSuccessMsgBox;

    //当前提示信息
    QString m_currentTipsMsg;
private:
    kyai::config::model::AiCapability m_capability;
    QString m_modelName;
    PublicLocalModel m_model;
    LocalModelInstallStatus m_modelStatus;
    bool isModelSelected;
    bool isDownLoading =false;
    bool isStartDownloading = false;
    QTimer *m_timer;

    QString m_currentInfoMessage = "";
    QString m_currentWarningMessage = "";

    QTimer* m_infoTimer = nullptr;
    QTimer* m_warningTimer = nullptr;

    SystemVersion m_curSystemVersion;
};

#endif // LOCALMODELITEM_H
