#include "dialogcamera.h"
#include "ui_dialogcamera.h"
#include <QMessageBox>
#include "VrSimpleLog.h"

DialogCamera::DialogCamera(IVrEyeDevice* pDevice, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogCamera),
    m_pDevice(pDevice)
{
    ui->setupUi(this);
    
    // 检查设备是否有效
    if (!m_pDevice) {
        setWindowTitle("相机参数配置 - 设备错误");
        // 禁用所有控件
        setEnabled(false);
        return;
    } else {
        setWindowTitle("相机参数配置");
    }
    
    // 初始化界面数据
    InitCameraParameters();
}

DialogCamera::~DialogCamera()
{
    delete ui;
}

void DialogCamera::on_btn_camer_ok_clicked()
{
    if (!m_pDevice) {
        QMessageBox::warning(this, "错误", "设备未初始化");
        return;
    }
    
    // 应用参数配置
    if (ApplyCameraParameters()) {
        QMessageBox::information(this, "成功", "相机参数配置成功！");
        accept(); // 关闭对话框并返回Accepted
    } else {
        QMessageBox::warning(this, "失败", "相机参数配置失败，请检查设备连接！");
    }
}

void DialogCamera::on_btn_camer_cancel_clicked()
{
    // 直接关闭窗口，不保存任何更改
    reject();
}

void DialogCamera::InitCameraParameters()
{
    if (!m_pDevice) return;
    
    try {
        // 获取曝光时间
        unsigned int exposeTime = 0;
        if (m_pDevice->GetEyeExpose(exposeTime) == 0) {
            // 假设UI中有一个名为spinBox_expose的控件
            ui->lineEdit_export->setText(QString::number(exposeTime));
            LOG_DEBUG("Current expose time: %u\n", exposeTime);
        }
        
        // 获取增益
        unsigned int gain = 0;
        if (m_pDevice->GetEyeGain(gain) == 0) {
            // 假设UI中有一个名为spinBox_gain的控件
            ui->lineEdit_gain->setText(QString::number(gain));
            LOG_DEBUG("Current gain: %u\n", gain);
        }
        
        // 获取帧率
        int frame = 0;
        if (m_pDevice->GetFrame(frame) == 0) {
            // 假设UI中有一个名为spinBox_frame的控件
            ui->lineEdit_frame->setText(QString::number(frame));
            LOG_DEBUG("Current frame rate: %d\n", frame);
        }
        
        // 获取摆动速度
        float swingSpeed = 0.0f;
        if (m_pDevice->GetSwingSpeed(swingSpeed) == 0) {
            // 假设UI中有一个名为doubleSpinBox_swing_speed的控件
            ui->lineEdit_swing_speed->setText(QString::number(swingSpeed));
            LOG_DEBUG("Swing speed: %.3f\n", swingSpeed);
        }
        
        // 获取工作角度
        float minAngle = 0.0f, maxAngle = 0.0f;
        if (m_pDevice->GetSwingAngle(minAngle, maxAngle) == 0) {
            // 假设UI中有角度相关的控件
            ui->lineEdit_swing_start->setText(QString::number(minAngle));
            ui->lineEdit_swing_stop->setText(QString::number(maxAngle));
            LOG_DEBUG("Swing angle range: %.3f to %.3f\n", minAngle, maxAngle);
        }
        
    } catch (...) {
        QMessageBox::warning(this, "错误", "读取相机参数时发生异常");
    }
}

bool DialogCamera::ApplyCameraParameters()
{
    if (!m_pDevice) return false;
    
    try {
        bool success = true;
        
        // 设置曝光时间
        unsigned int exposeTime = ui->lineEdit_export->text().toUInt();
        if (m_pDevice->SetEyeExpose(exposeTime) != 0) {
            LOG_WARNING("Failed to set expose time: %u\n", exposeTime);
            success = false;
        } else {
            LOG_INFO("Set expose time: %u\n", exposeTime);
        }
        
        // 设置增益
        unsigned int gain = ui->lineEdit_gain->text().toUInt();
        if (m_pDevice->SetEyeGain(gain) != 0) {
            LOG_WARNING("Failed to set gain: %u\n", gain);
            success = false;
        } else {
            LOG_INFO("Set gain: %u\n", gain);
        }
        
        // 设置帧率
        int frame = ui->lineEdit_frame->text().toInt();
        if (m_pDevice->SetFrame(frame) != 0) {
            LOG_WARNING("Failed to set frame rate: %d\n", frame);
            success = false;
        } else {
            LOG_INFO("Set frame rate: %d\n", frame);
        }
        
        // 设置摆动速度
        float swingSpeed = ui->lineEdit_swing_speed->text().toFloat();
        if (m_pDevice->SetSwingSpeed(swingSpeed) != 0) {
            LOG_WARNING("Failed to set swing speed: %.3f\n", swingSpeed);
            success = false;
        } else {
            LOG_INFO("Set swing speed: %.3f\n", swingSpeed);
        }
        
        // 设置工作角度
        float minAngle = ui->lineEdit_swing_start->text().toFloat();
        float maxAngle = ui->lineEdit_swing_stop->text().toFloat();
        if (m_pDevice->SetSwingAngle(minAngle, maxAngle) != 0) {
            LOG_WARNING("Failed to set swing angle: %.3f to %.3f\n", minAngle, maxAngle);
            success = false;
        } else {
            LOG_INFO("Set swing angle: %.3f to %.3f\n", minAngle, maxAngle);
        }
        
        return success;
        
    } catch (...) {
        QMessageBox::warning(this, "错误", "应用相机参数时发生异常");
        return false;
    }
}

