#include <QApplication>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QStatusBar>
#include <QMenuBar>
#include <QAction>
#include <QMessageBox>
#include <QTimer>
#include <QDebug>
#include "screenwall_widget.h"

class ScreenWallMainWindow : public QMainWindow
{

public:
    explicit ScreenWallMainWindow(QWidget *parent = nullptr)
        : QMainWindow(parent)
        , m_screenWall(nullptr)
        , m_startButton(nullptr)
        , m_stopButton(nullptr)
        , m_refreshButton(nullptr)
        , m_statusLabel(nullptr)
    {
        setupUI();
        setupConnections();
        setupMenus();
        
        setWindowTitle("屏幕墙 - 实时窗口预览系统");
        setMinimumSize(1000, 700);
        resize(1400, 1000);
    }

private slots:
    void startScreenWall()
    {
        if (m_screenWall) {
            m_screenWall->startScreenWall();
            m_startButton->setEnabled(false);
            m_stopButton->setEnabled(true);
            m_refreshButton->setEnabled(true);
            m_statusLabel->setText("状态: 运行中");
            statusBar()->showMessage("屏幕墙已启动", 3000);
        }
    }
    
    void stopScreenWall()
    {
        if (m_screenWall) {
            m_screenWall->stopScreenWall();
            m_startButton->setEnabled(true);
            m_stopButton->setEnabled(false);
            m_refreshButton->setEnabled(false);
            m_statusLabel->setText("状态: 已停止");
            statusBar()->showMessage("屏幕墙已停止", 3000);
        }
    }
    
    void refreshWindows()
    {
        if (m_screenWall) {
            m_screenWall->refreshWindowList();
            statusBar()->showMessage("窗口列表已刷新", 2000);
        }
    }
    
    void onWindowSelected(HWND hwnd, const QString& title)
    {
        statusBar()->showMessage(QString("已选择窗口: %1").arg(title), 5000);
        qDebug() << "Window selected:" << title << "HWND:" << hwnd;
    }
    
    void onDesktopSelected()
    {
        statusBar()->showMessage("已选择桌面预览", 3000);
        qDebug() << "Desktop selected";
    }
    
    void showAbout()
    {
        QMessageBox::about(this, "关于屏幕墙",
            "<h3>屏幕墙 v1.0</h3>"
            "<p>实时窗口预览系统</p>"
            "<p><b>功能特性:</b></p>"
            "<ul>"
            "<li>实时桌面预览</li>"
            "<li>窗口图标列表</li>"
            "<li>实时窗口内容预览</li>"
            "<li>最小化窗口支持</li>"
            "<li>性能优化</li>"
            "</ul>"
            "<p>基于 Qt + AvRecorder 开发</p>"
        );
    }
    
    void showSettings()
    {
        // 这里可以添加设置对话框
        QMessageBox::information(this, "设置", "设置功能开发中...");
    }

private:
    void setupUI()
    {
        // 创建中央部件
        auto* centralWidget = new QWidget;
        setCentralWidget(centralWidget);
        
        auto* mainLayout = new QVBoxLayout(centralWidget);
        mainLayout->setContentsMargins(5, 5, 5, 5);
        mainLayout->setSpacing(5);
        
        // 控制面板
        auto* controlPanel = createControlPanel();
        mainLayout->addWidget(controlPanel);
        
        // 屏幕墙组件
        m_screenWall = new ScreenWallWidget;
        mainLayout->addWidget(m_screenWall, 1);
        
        // 状态栏
        m_statusLabel = new QLabel("状态: 已停止");
        statusBar()->addWidget(m_statusLabel);
        statusBar()->showMessage("就绪");
    }
    
    QWidget* createControlPanel()
    {
        auto* panel = new QWidget;
        panel->setMaximumHeight(60);
        panel->setStyleSheet(
            "QWidget { "
            "  background-color: #34495e; "
            "  border-radius: 5px; "
            "}"
            "QPushButton { "
            "  background-color: #3498db; "
            "  color: white; "
            "  border: none; "
            "  padding: 8px 16px; "
            "  border-radius: 4px; "
            "  font-weight: bold; "
            "}"
            "QPushButton:hover { "
            "  background-color: #2980b9; "
            "}"
            "QPushButton:pressed { "
            "  background-color: #21618c; "
            "}"
            "QPushButton:disabled { "
            "  background-color: #7f8c8d; "
            "}"
        );
        
        auto* layout = new QHBoxLayout(panel);
        layout->setContentsMargins(10, 10, 10, 10);
        layout->setSpacing(10);
        
        // 标题
        auto* titleLabel = new QLabel("屏幕墙控制面板");
        titleLabel->setStyleSheet("color: white; font-size: 14px; font-weight: bold;");
        layout->addWidget(titleLabel);
        
        layout->addStretch();
        
        // 控制按钮
        m_startButton = new QPushButton("启动");
        m_startButton->setIcon(QApplication::style()->standardIcon(QStyle::SP_MediaPlay));
        layout->addWidget(m_startButton);
        
        m_stopButton = new QPushButton("停止");
        m_stopButton->setIcon(QApplication::style()->standardIcon(QStyle::SP_MediaStop));
        m_stopButton->setEnabled(false);
        layout->addWidget(m_stopButton);
        
        m_refreshButton = new QPushButton("刷新");
        m_refreshButton->setIcon(QApplication::style()->standardIcon(QStyle::SP_BrowserReload));
        m_refreshButton->setEnabled(false);
        layout->addWidget(m_refreshButton);
        
        return panel;
    }
    
    void setupConnections()
    {
        // 控制按钮
        connect(m_startButton, &QPushButton::clicked, this, &ScreenWallMainWindow::startScreenWall);
        connect(m_stopButton, &QPushButton::clicked, this, &ScreenWallMainWindow::stopScreenWall);
        connect(m_refreshButton, &QPushButton::clicked, this, &ScreenWallMainWindow::refreshWindows);
        
        // 屏幕墙事件
        connect(m_screenWall, &ScreenWallWidget::windowSelected,
                this, &ScreenWallMainWindow::onWindowSelected);
        connect(m_screenWall, &ScreenWallWidget::desktopSelected,
                this, &ScreenWallMainWindow::onDesktopSelected);
    }
    
    void setupMenus()
    {
        // 文件菜单
        auto* fileMenu = menuBar()->addMenu("文件(&F)");
        
        auto* exitAction = new QAction("退出(&X)", this);
        exitAction->setShortcut(QKeySequence::Quit);
        exitAction->setIcon(QApplication::style()->standardIcon(QStyle::SP_DialogCloseButton));
        connect(exitAction, &QAction::triggered, this, &QWidget::close);
        fileMenu->addAction(exitAction);
        
        // 视图菜单
        auto* viewMenu = menuBar()->addMenu("视图(&V)");
        
        auto* refreshAction = new QAction("刷新窗口列表(&R)", this);
        refreshAction->setShortcut(QKeySequence::Refresh);
        refreshAction->setIcon(QApplication::style()->standardIcon(QStyle::SP_BrowserReload));
        connect(refreshAction, &QAction::triggered, this, &ScreenWallMainWindow::refreshWindows);
        viewMenu->addAction(refreshAction);
        
        // 工具菜单
        auto* toolsMenu = menuBar()->addMenu("工具(&T)");
        
        auto* settingsAction = new QAction("设置(&S)", this);
        settingsAction->setIcon(QApplication::style()->standardIcon(QStyle::SP_ComputerIcon));
        connect(settingsAction, &QAction::triggered, this, &ScreenWallMainWindow::showSettings);
        toolsMenu->addAction(settingsAction);
        
        // 帮助菜单
        auto* helpMenu = menuBar()->addMenu("帮助(&H)");
        
        auto* aboutAction = new QAction("关于(&A)", this);
        aboutAction->setIcon(QApplication::style()->standardIcon(QStyle::SP_MessageBoxInformation));
        connect(aboutAction, &QAction::triggered, this, &ScreenWallMainWindow::showAbout);
        helpMenu->addAction(aboutAction);
    }

protected:
    void closeEvent(QCloseEvent* event) override
    {
        if (m_screenWall) {
            m_screenWall->stopScreenWall();
        }
        QMainWindow::closeEvent(event);
    }

private:
    ScreenWallWidget* m_screenWall;
    QPushButton* m_startButton;
    QPushButton* m_stopButton;
    QPushButton* m_refreshButton;
    QLabel* m_statusLabel;
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    
    // 设置应用程序信息
    app.setApplicationName("ScreenWall");
    app.setApplicationVersion("1.0");
    app.setOrganizationName("ScreenWall Team");
    
    // 设置应用程序样式
    app.setStyle("Fusion");
    
    // 创建并显示主窗口
    ScreenWallMainWindow window;
    window.show();
    
    qDebug() << "ScreenWall application started";
    
    return app.exec();
}