// Copyright (C) 2019 ~ 2020 Uniontech Software Technology Co.,Ltd.
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include <DApplication>
#include <DTextEdit>

#include <QString>
#include <QDateTime>
#include <QPixmap>
#include <QTextDocumentFragment>
#include <QTextDocument>
#include <QKeyEvent>
#include <DSysInfo>

DWIDGET_USE_NAMESPACE
DCORE_USE_NAMESPACE

class VNoteBlock;

class Utils
{
public:
    Utils();
    //格式化时间
    static QString convertDateTime(const QDateTime &dateTime);
    //加载图标
    static QPixmap renderSVG(const QString &filePath, const QSize &size, DApplication *pApp);
    //加载图标
    static QPixmap loadSVG(const QString &fileName, bool fCommon = false);
    //编辑器内容高亮
    static int highTextEdit(QTextDocument *textDoc, const QString &searchKey,
                            const QColor &highColor, bool undo = false);
    //格式化录音时长
    static QString formatMillisecond(qint64 millisecond, qint64 minValue = 1);
    //内存数据同步编辑器显示内容
    static void documentToBlock(VNoteBlock *block, const QTextDocument *doc);
    //编辑器同步内存数据
    static void blockToDocument(const VNoteBlock *block, QTextDocument *doc);
    //设置编辑器字体颜色
    static void setDefaultColor(QTextDocument *srcDoc, const QColor &color);
    //设置标题栏tab焦点
    static void setTitleBarTabFocus(QKeyEvent *event);
    //图片转base64
    static bool pictureToBase64(QString imgPath, QString &base64);
    //判断是否龙芯平台
    static bool isLoongsonPlatform();
    //过滤文件名内容
    static QString filteredFileName(QString fileName, const QString &defaultName = "");
    //判断是否wayland
    static bool isWayland();
    //搜索结果标题处理
    static QString createRichText(const QString &title, const QString &key);
    //去除HTML标签，将富文本转换为纯文本
    static QString stripHtmlTags(const QString &htmlText);
    // Check if current in linglong environment
    static bool inLinglongEnv();

    // UOS edition detection via DBus OsBuild (fallback from provided reference)
    static DSysInfo::UosEdition uosEditionType();
    static DSysInfo::UosEdition parseOsBuildType(const QString &osBuild);
    static bool checkOsBuildValid(const QString &str);
    static bool isCommunityEdition();
    // 将音频绝对路径转换为相对路径（AppData/voicenote/... -> voicenote/...）
    static QString makeVoiceRelative(const QString &voicePath);
    // 将音频相对路径转换为绝对路径（voicenote/... -> AppData/voicenote/...），若已为绝对/URL则原样返回
    static QString makeVoiceAbsolute(const QString &voicePath);
};
