#include "FrequencyTuningForm.h"
#include "ui_FrequencyTuningForm.h"
#include "DeviceHubWindow.h"

FrequencyTuningForm::FrequencyTuningForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FrequencyTuningForm)
{
    ui->setupUi(this);
}

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

void FrequencyTuningForm::on_freqTunButt_clicked()
{
    // 获取设备对象
    int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex;
    FrequencyTuning * device = (FrequencyTuning *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("03").at(devIndex);

    // mock测试工作模式则产生一条mock数据
    if (SettingConfig::getInstance().WORK_MODE == "mock")
    {
        device->mockReceivData();
    }
}

void FrequencyTuningForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData)
{
    QString currentDevCode = ((DeviceHubWindow *)this->parent()->parent())->getDevSelect()->currentData().toJsonObject().find("deviceNo")->toString();

    // 如果不是当前设备的帧，直接返回
    if (frameData->devCode != currentDevCode)
    {
        return;
    }

    // 当前显示的设备编号
    if (frameData->frameType == "0301")
    {
        FrequencyTuningStatusFreqDto * freqFrameDto = (FrequencyTuningStatusFreqDto *) frameData;
        ui->ftDevStatus->setText(freqFrameDto->devStatus == "1" ? "正常" : "异常");
        ui->ftFreqAdjust->setText(QString::number(freqFrameDto->freqAdjust, 'g', 3));
        ui->ftFreqAdjustAcc->setText(QString::number(freqFrameDto->freqAdjustAcc, 'g', 3));
        ui->ftPhaseShift->setText(QString::number(freqFrameDto->phaseShift));
        ui->ftPhaseAdjust->setText(QString::number(freqFrameDto->phaseAdjust, 'f', 3));
        ui->ftPulseWidth->setText(QString::number(freqFrameDto->pulseWidth));
        ui->label_ts->setText(freqFrameDto->timestamp);
    } else if (frameData->frameType == "0302")
    {
        FrequencyTuningStatusLockDto * lockFrameDto = (FrequencyTuningStatusLockDto *) frameData;
        ui->ftLockStatus->setText(lockFrameDto->lockStatus == "1" ? "锁定" : "失锁");
    }
}

void FrequencyTuningForm::displayDeviceCommandOnForm(QJsonObject command)
{
    QString deviceId = command.value("deviceId").toString();
    QList<DeviceBase *> typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("03");
    for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++)
    {
        if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "03")
        {
            ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->setCurrentIndex(i);
        }
    }
    for (int i = 0; i < typeDevList.size(); i++)
    {
        if (typeDevList.at(i)->getDeviceId() == command.value("deviceId").toString())
        {
            ((DeviceHubWindow *)this->parent()->parent())->getDevSelect()->setCurrentIndex(i);
        }
    }

    QString commandType = command.value("command").toString();
    if (commandType == "GLF,20")
    {
        ui->ftSetFreqTurn->setText(command.value("params").toString());
        ui->ftSetFreqTurnRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,21")
    {
        ui->ftSetPhaseTunn->setText(command.value("params").toString());
        ui->ftSetPhaseTunnRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLP,01")
    {
        ui->ftSetPhaseShift->setText(command.value("params").toString());
        ui->ftSetPhaseShiftRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLP,02")
    {
        ui->ftSetSynch->setText(command.value("params").toString());
        ui->ftSetSynchRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLP,03")
    {
        ui->ftSetPulseWidth->setText(command.value("params").toString());
        ui->ftSetPulseWidthRaw->setText(command.value("rawCommand").toString());
    }
}
