#include "capture_source_widget.h"
#include "qdebug.h"

#include <capturer/finder.h>

CaptureSourceWidget::CaptureSourceWidget(QWidget* parent)
    : QWidget(parent)
    , m_captureComboBox(nullptr)
    , m_updateListBtn(nullptr)
    , m_captureGroup(nullptr)
    , m_layout(nullptr)
{
    setupUi();
    updateCaptureList();
}

void CaptureSourceWidget::setupUi()
{
    // 创建控件
    m_captureComboBox = new QComboBox(this);
    m_updateListBtn = new QPushButton("刷新窗口列表", this);
    m_captureGroup = new QGroupBox("捕获源", this);
    
    // 设置ComboBox属性
    m_captureComboBox->setMinimumWidth(20);
    
    // 创建布局
    m_layout = new QHBoxLayout();
    m_layout->addWidget(m_captureComboBox);
    m_layout->addWidget(m_updateListBtn);
    m_captureGroup->setLayout(m_layout);
    
    // 设置主布局
    QHBoxLayout* mainLayout = new QHBoxLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->addWidget(m_captureGroup);
    setLayout(mainLayout);
    
    // 连接信号槽
    connect(m_captureComboBox, &QComboBox::currentTextChanged, 
            this, &CaptureSourceWidget::onComboBoxTextChanged);
    connect(m_captureComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
            this, &CaptureSourceWidget::onComboBoxIndexChanged);
    connect(m_updateListBtn, &QPushButton::clicked,
            this, &CaptureSourceWidget::onRefreshButtonClicked);
}

int CaptureSourceWidget::getCurrentIndex() const
{
    return m_captureComboBox ? m_captureComboBox->currentIndex() : -1;
}

QVariant CaptureSourceWidget::getCurrentData() const
{
    if (!m_captureComboBox) {
        return QVariant();
    }
    
    QVariant data = m_captureComboBox->currentData();
    QString text = m_captureComboBox->currentText();
    
    // 如果是窗口类型，验证HWND有效性
    if (text.startsWith("窗口: ")) {
        qintptr ptrHwnd = data.value<qintptr>();
        HWND hwnd = reinterpret_cast<HWND>(ptrHwnd);
        
        if (!IsWindow(hwnd)) {
            qDebug() << "CaptureSourceWidget: 检测到无效窗口句柄" << Qt::hex << ptrHwnd 
                     << "窗口标题:" << text << "- 自动刷新窗口列表";
            
            // 自动刷新窗口列表
            const_cast<CaptureSourceWidget*>(this)->updateCaptureList();
            
            // 尝试重新获取数据
            return m_captureComboBox->currentData();
        }
    }
    
    return data;
}

QString CaptureSourceWidget::getCurrentText() const
{
    return m_captureComboBox ? m_captureComboBox->currentText() : QString();
}

void CaptureSourceWidget::setEnabled(bool enabled)
{
    QWidget::setEnabled(enabled);
    if (m_captureComboBox) {
        m_captureComboBox->setEnabled(enabled);
    }
    if (m_updateListBtn) {
        m_updateListBtn->setEnabled(enabled);
    }
}

void CaptureSourceWidget::updateCaptureList()
{
    if (!m_captureComboBox) {
        return;
    }
    
    m_captureComboBox->clear();
    
    // 添加屏幕列表
    auto&& monitorList = MonitorFinder::GetList(true);
    for (auto&& monitor : monitorList) {
        QString text = "屏幕: " + QString::fromStdWString(monitor.title);
        m_captureComboBox->addItem(text, QVariant::fromValue(qintptr(monitor.monitor)));
    }
    
    // 添加窗口列表
    auto&& windowList = WindowFinder::GetList(true);
    for (auto&& window : windowList) {
        QString text = "窗口: " + QString::fromStdWString(window.title);
        m_captureComboBox->addItem(text, QVariant::fromValue(qintptr(window.hwnd)));
    }
}

void CaptureSourceWidget::onComboBoxTextChanged(const QString& text)
{
    emit captureSourceTextChanged(text);
}

void CaptureSourceWidget::onComboBoxIndexChanged(int index)
{
    emit captureSourceIndexChanged(index);
}

void CaptureSourceWidget::onRefreshButtonClicked()
{
    updateCaptureList();
    emit refreshRequested();
}
