// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef MODELSCOPEITEM_H
#define MODELSCOPEITEM_H

#include "uosai_global.h"
#include <QProcess>
#include <QTimer>

#include <DWidget>
#include <DBackgroundGroup>
#include <DLabel>
#include <DSuggestButton>
#include <DSpinner>

namespace uos_ai {

class Downloader;
class ModelScopeItem : public DTK_WIDGET_NAMESPACE::DWidget
{
    Q_OBJECT
public:
    struct ItemInfo
    {
        ItemInfo() {}
        QString appName;
        QString modelName;
        QString baseUrl;
        QStringList modelFileList;

        static ItemInfo deepseek_r1_1_5B();
    };
public:
    explicit ModelScopeItem(const ItemInfo &info, DTK_WIDGET_NAMESPACE::DWidget *parent = nullptr);
    void stopDownload();
private:
    void initUI();
    void initConnect();
    void changeInstallStatus();
    void adjustSummaryLabelWidth();

public:
    void setText(const QString &theme, const QString &summary, double sizeGb = 0.0);
    void checkInstallStatus();
    bool getInstallStatus();

private slots:
    void onInstall();
    void onUninstall();
    void onCheckFile();
    void onCheckFileFailed();
    void onDownloadFinished();
    void onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);

protected:
    void resizeEvent(QResizeEvent *event) override;
    static void runCheckFile(ModelScopeItem *self, const QString &dirPath, const QStringList &fileList);

private:
    DTK_WIDGET_NAMESPACE::DLabel *m_pLabelTheme = nullptr;
    DTK_WIDGET_NAMESPACE::DLabel *m_pLabelSize = nullptr;
    DTK_WIDGET_NAMESPACE::DLabel *m_pLabelSummary = nullptr;
    DTK_WIDGET_NAMESPACE::DSuggestButton *m_pBtnInstall = nullptr;
    DTK_WIDGET_NAMESPACE::DPushButton *m_pBtnUninstall = nullptr;
    DTK_WIDGET_NAMESPACE::DPushButton *m_pBtnStop = nullptr;
    DTK_WIDGET_NAMESPACE::DSpinner *m_pSpinner = nullptr;

    bool m_isInstall = false;
    double m_lastProgress = 0.0;
    QString m_installPath;
    QSharedPointer<Downloader> m_downloader;
    double m_sizeGb = 0.0;
    qint64 m_startTimeMs = 0;
    QTimer *m_timer = nullptr;

    ItemInfo m_info;
};

}

#endif // MODELSCOPEITEM_H
