#ifndef WINDOWPREVIEWWIDGET_H
#define WINDOWPREVIEWWIDGET_H

#include <QListView>

class QStandardItemModel;

class WindowPreviewWidget : public QListView
{
    Q_OBJECT
public:
    explicit WindowPreviewWidget(QWidget *parent = nullptr);
    ~WindowPreviewWidget();

signals:
    void windowDoubleClicked(quintptr hwnd, const QString &title); // 添加双击信号

protected:
    void mousePressEvent(QMouseEvent *event) override;
    void mouseDoubleClickEvent(QMouseEvent *event) override; // 添加双击事件处理
    void mouseMoveEvent(QMouseEvent *event) override;
    void leaveEvent(QEvent *event) override;
    void enterEvent(QEvent *event) override;
    void focusInEvent(QFocusEvent *event) override;
    void focusOutEvent(QFocusEvent *event) override;

private:
    QStandardItemModel *model;
    QTimer *updateTimer;
    QHash<HWND, QPixmap *> thumbnailCache;
    HWND lastActiveWindow = nullptr;
    QSize thumbnailSize = QSize(120, 120); // 预览大小
    QModelIndex hoveredIndex;              // 当前鼠标悬停的索引
    QModelIndex focusedIndex;              // 当前获得焦点的索引

    void updateThumbnails();
    void addWindowThumbnail(HWND hwnd, HWND activeWindow);
    QPixmap *captureWindow(HWND hwnd);
    void addDesktopThumbnail(HWND activeWindow);
    QIcon getWindowIcon(HWND hwnd);
    bool needFullUpdate() const;
    std::wstring getWindowTextStd(HWND hwnd);
};

/// demo
// #include "qapplication.h"
// #include "widgets/windowpreviewwidget.h"

// #include <QBoxLayout>
// #include <QLabel>
// #include <QWidget>
// int main(int argc, char *argv[])
// {
//     QApplication app(argc, argv);

//     QWidget mainWidget;
//     QVBoxLayout *layout = new QVBoxLayout(&mainWidget);
//     layout->setContentsMargins(0, 0, 0, 0);

//     // 设置半透明背景
//     mainWidget.setAttribute(Qt::WA_TranslucentBackground);
//     mainWidget.setStyleSheet("background: rgba(0, 0, 0, 150);");

//     WindowPreviewWidget *previewWidget = new WindowPreviewWidget;
//     layout->addWidget(previewWidget);

//     // 添加信息标签
//     QLabel *infoLabel = new QLabel("Double-click a window to see its handle");
//     infoLabel->setAlignment(Qt::AlignCenter);
//     infoLabel->setStyleSheet("color: white; font-weight: bold; background: rgba(0, 0, 0, 100);");
//     layout->addWidget(infoLabel);

//     // 连接双击信号
//     QObject::connect(previewWidget,
//                      &WindowPreviewWidget::windowDoubleClicked,
//                      [infoLabel](quintptr hwnd, const QString &title) {
//                          QString info = QString("Double-clicked window: HWND=0x%1, Title=\"%2\"")
//                          .arg(hwnd, 0, 16) // 以十六进制显示句柄
//                              .arg(title);
//                          infoLabel->setText(info);
//                      });

//     mainWidget.setWindowTitle("Window Preview");
//     mainWidget.resize(900, 700);
//     mainWidget.show();

//     return app.exec();
// }

#endif // WINDOWPREVIEWWIDGET_H
