#include "WindInverstorDialog.h"
#include "WindInverseFunction.h"
WindInverstorDialog::WindInverstorDialog(QWidget *parent)
    : QMainWindow(parent)
{
    setupUI();
    connectSlots();
    setWindowTitle(u8"风速反演工具");
    setMinimumSize(800, 400);
}

WindInverstorDialog::~WindInverstorDialog()
{}

void WindInverstorDialog::browseWindDirFile()
{
    QString file = QFileDialog::getOpenFileName(this, u8"选择风速方向文件",
        windDirLineEdit->text(),
        "Binary files (*.bin);;All files (*.*)");
    if (!file.isEmpty()) {
        windDirLineEdit->setText(file);
    }
}
void WindInverstorDialog::setupUI()
{
    // 创建中央部件和主布局
    QWidget* centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);

    QVBoxLayout* mainLayout = new QVBoxLayout(centralWidget);

    // 创建文件输入组
    QGroupBox* fileGroup = new QGroupBox(u8"文件路径设置", this);
    QVBoxLayout* fileLayout = new QVBoxLayout(fileGroup);

    // 风速方向文件选择
    QHBoxLayout* windDirLayout = new QHBoxLayout();
    QLabel* windDirLabel = new QLabel(u8"风速方向文件:", this);
    windDirLineEdit = new QLineEdit(this);
    windDirLineEdit->setText("D:\\Programs\\SpacetyLabelAIPlante\\Manual-Labeling-Tool\\WindSpeedModel\\TestData\\SAR_EC_20240801\\total_precipitation2024080110_resample.bin");
    windDirLineEdit->setMinimumWidth(400);
    browseWindDirBtn = new QPushButton(u8"浏览...", this);
    windDirLayout->addWidget(windDirLabel);
    windDirLayout->addWidget(windDirLineEdit);
    windDirLayout->addWidget(browseWindDirBtn);

    // 入射角文件选择
    QHBoxLayout* incAngleLayout = new QHBoxLayout();
    QLabel* incAngleLabel = new QLabel(u8"入射角文件:", this);
    incAngleLineEdit = new QLineEdit(this);
    incAngleLineEdit->setText("D:\\Programs\\SpacetyLabelAIPlante\\Manual-Labeling-Tool\\WindSpeedModel\\TestData\\SAR_EC_20240801\\4254-DB-IncidenceAngle-GEO.bin");
    incAngleLineEdit->setMinimumWidth(400);
    browseIncAngleBtn = new QPushButton(u8"浏览...", this);
    incAngleLayout->addWidget(incAngleLabel);
    incAngleLayout->addWidget(incAngleLineEdit);
    incAngleLayout->addWidget(browseIncAngleBtn);

    // SAR sigma0文件选择
    QHBoxLayout* sarSigmaLayout = new QHBoxLayout();
    QLabel* sarSigmaLabel = new QLabel(u8"SAR sigma0文件:", this);
    sarSigmaLineEdit = new QLineEdit(this);
    sarSigmaLineEdit->setText("D:\\Programs\\SpacetyLabelAIPlante\\Manual-Labeling-Tool\\WindSpeedModel\\TestData\\SAR_EC_20240801\\4254-DB-GEO.bin");
    sarSigmaLineEdit->setMinimumWidth(400);
    browseSarSigmaBtn = new QPushButton(u8"浏览...", this);
    sarSigmaLayout->addWidget(sarSigmaLabel);
    sarSigmaLayout->addWidget(sarSigmaLineEdit);
    sarSigmaLayout->addWidget(browseSarSigmaBtn);

    // 输出文件选择
    QHBoxLayout* outputLayout = new QHBoxLayout();
    QLabel* outputLabel = new QLabel(u8"输出风速文件:", this);
    outputLineEdit = new QLineEdit(this);
    outputLineEdit->setText("D:\\Programs\\SpacetyLabelAIPlante\\Manual-Labeling-Tool\\WindSpeedModel\\TestData\\InverserWindSpeed.bin");
    outputLineEdit->setMinimumWidth(400);
    browseOutputBtn = new QPushButton(u8"浏览...", this);
    outputLayout->addWidget(outputLabel);
    outputLayout->addWidget(outputLineEdit);
    outputLayout->addWidget(browseOutputBtn);

    // 添加所有文件路径控件到文件组
    fileLayout->addLayout(windDirLayout);
    fileLayout->addLayout(incAngleLayout);
    fileLayout->addLayout(sarSigmaLayout);
    fileLayout->addLayout(outputLayout);

    // 创建按钮组
    QHBoxLayout* buttonLayout = new QHBoxLayout();
    startBtn = new QPushButton(u8"开始反演", this);
    cancelBtn = new QPushButton(u8"取消", this);

    // 设置按钮样式
    startBtn->setStyleSheet("QPushButton { background-color: #4CAF50; color: white; font-weight: bold; padding: 8px 16px; }");
    cancelBtn->setStyleSheet("QPushButton { background-color: #f44336; color: white; font-weight: bold; padding: 8px 16px; }");

    buttonLayout->addStretch();
    buttonLayout->addWidget(startBtn);
    buttonLayout->addWidget(cancelBtn);

    // 添加到主布局
    mainLayout->addWidget(fileGroup);
    mainLayout->addStretch();
    mainLayout->addLayout(buttonLayout);
}
void WindInverstorDialog::browseIncAngleFile()
{
    QString file = QFileDialog::getOpenFileName(this, u8"选择入射角文件",
        incAngleLineEdit->text(),
        "Binary files (*.bin);;All files (*.*)");
    if (!file.isEmpty()) {
        incAngleLineEdit->setText(file);
    }
}

void WindInverstorDialog::browseSarSigmaFile()
{
    QString file = QFileDialog::getOpenFileName(this, u8"选择SAR sigma0文件",
        sarSigmaLineEdit->text(),
        "Binary files (*.bin);;All files (*.*)");
    if (!file.isEmpty()) {
        sarSigmaLineEdit->setText(file);
    }
}

void WindInverstorDialog::browseOutputFile()
{
    QString file = QFileDialog::getSaveFileName(this, u8"选择输出文件",
        outputLineEdit->text(),
        "Binary files (*.bin);;All files (*.*)");
    if (!file.isEmpty()) {
        outputLineEdit->setText(file);
    }
}

void WindInverstorDialog::startInversion()
{
    // 验证文件路径
    if (windDirLineEdit->text().isEmpty() ||
        incAngleLineEdit->text().isEmpty() ||
        sarSigmaLineEdit->text().isEmpty() ||
        outputLineEdit->text().isEmpty()) {
        QMessageBox::warning(this, u8"警告", u8"请填写所有文件路径！");
        return;
    }


 

	QString in_wind_dir_path = windDirLineEdit->text();
	QString in_inc_angle_path = incAngleLineEdit->text();
	QString in_sar_sigma0_path = sarSigmaLineEdit->text();
	QString out_wind_speed_path = outputLineEdit->text();
    windSpeedInversionProcess(in_wind_dir_path,
                                in_inc_angle_path,
                                in_sar_sigma0_path,
                                out_wind_speed_path);
    // 这里添加风速反演的核心处理逻辑
    QMessageBox::information(this, u8"提示", u8"风速反演处理结束\n输入文件: " +
        windDirLineEdit->text() +
        u8"\n输出文件: " + outputLineEdit->text());

}

void WindInverstorDialog::closeApplication()
{
    QMessageBox::StandardButton reply;
    reply = QMessageBox::question(this, u8"确认退出", u8"确定要退出风速反演工具吗？",
        QMessageBox::Yes | QMessageBox::No);
    if (reply == QMessageBox::Yes) {
        close();
    }
}

void WindInverstorDialog::connectSlots()
{
    connect(browseWindDirBtn, &QPushButton::clicked, this, &WindInverstorDialog::browseWindDirFile);
    connect(browseIncAngleBtn, &QPushButton::clicked, this, &WindInverstorDialog::browseIncAngleFile);
    connect(browseSarSigmaBtn, &QPushButton::clicked, this, &WindInverstorDialog::browseSarSigmaFile);
    connect(browseOutputBtn, &QPushButton::clicked, this, &WindInverstorDialog::browseOutputFile);
    connect(startBtn, &QPushButton::clicked, this, &WindInverstorDialog::startInversion);
    connect(cancelBtn, &QPushButton::clicked, this, &WindInverstorDialog::closeApplication);
}

