#ifndef MASKOVERLAY_H
#define MASKOVERLAY_H

#include <QMap>
#include <QObject>
#include <QPointer>
#include <QProgressBar>
#include <QTimer>
#include <QWidget>
#include <functional>

class MaskOverlay : public QObject
{
    Q_OBJECT
public:
    enum FindMode { ActiveWindow, MainWindow, ParentWindow };

    struct OverlayContext
    {
        QWidget* maskWidget = nullptr;
        QProgressBar* progressBar = nullptr;
        QTimer* timeoutTimer = nullptr;
        std::function<void()> timeoutCallback;
    };

    static MaskOverlay* instance();

    void setGlobalStyle(const QString& styleSheet);
    void setProgressBarStyle(const QString& styleSheet);
    void setProgressValue(QWidget* target, int value); // 新增方法

    void show(QWidget* target = nullptr,
              int timeoutMs = 3000,
              FindMode findMode = ActiveWindow,
              std::function<void()> timeoutCallback = nullptr);

    void hide(QWidget* target = nullptr);
    void setTimeout(QWidget* target, int milliseconds);

protected:
    bool eventFilter(QObject* watched, QEvent* event) override;

private:
    explicit MaskOverlay(QObject* parent = nullptr);
    QWidget* findBestTarget(QWidget* hintTarget, FindMode mode);
    void setupOverlay(QWidget* target);

    QMap<QWidget*, OverlayContext> overlays_;
    QPointer<QWidget> lastShownTarget_;
    QString globalStyle_;
    QString progressStyle_;

    static MaskOverlay* instance_;
};

#endif // LOADINGMASK_H
