#include "FreqSwitcherForm.h"
#include "ui_FreqSwitcherForm.h"
#include "DeviceHubWindow.h"

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

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

void FreqSwitcherForm::on_freqSwiButt_clicked()
{
    // 获取设备对象
    int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex;
    FreqSwitcher * device = (FreqSwitcher *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("06").at(devIndex);

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

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

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

    // 当前显示的设备编号
    if (frameData->frameType == "0601")
    {
        FreqSwitcherStatusDto * statusFrameDto = (FreqSwitcherStatusDto *) frameData;
        ui->label_ts->setText(statusFrameDto->timestamp);
        ui->fsDevStatus->setText(statusFrameDto->devStatus == "1" ? "正常" : "异常");
        ui->fsLockPhaseType->setText(statusFrameDto->lockPhaseType == "1" ? "自由运行" : "跟踪");
        QString lockPhaseStatusStr;
        if (statusFrameDto->digitalLockPhaseStatus == "1") lockPhaseStatusStr = "驯服";
        else if (statusFrameDto->digitalLockPhaseStatus == "2") lockPhaseStatusStr = "保持";
        else if (statusFrameDto->digitalLockPhaseStatus == "3") lockPhaseStatusStr = "预热";
        else if (statusFrameDto->digitalLockPhaseStatus == "4") lockPhaseStatusStr = "锁定";
        ui->fsLockPhaseStatus->setText(lockPhaseStatusStr);
        ui->fsRefSelectType->setText(statusFrameDto->refSelectType == "2" ? "手动" : "自动");
        ui->fsFreqAccOut->setText(QString("%1 pHz").arg(statusFrameDto->freqAdjustAcc));
        ui->fsOppsPhaseAcc->setText(QString("%1 fs").arg(statusFrameDto->phaseAdjustAcc * 10));
        ui->fsRefFreqSrc->setText(statusFrameDto->refFreqSrc.toInt() < 5 ? QString("参考%1").arg(statusFrameDto->refFreqSrc.toInt() + 1) : "融合");

        qDebug() << statusFrameDto->toJSON();

        QJsonObject dataObj = statusFrameDto->toJSON().find("data")->toObject();
        QJsonDocument doc;

        doc.setArray(dataObj.find("freqRefStatusArray")->toArray());
        ui->fsFreqRefStatus->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData()));

        doc.setArray(dataObj.find("localFreqDiffArray")->toArray());
        ui->fsFreqDiff->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData()));

        doc.setArray(dataObj.find("freqRefWeightArray")->toArray());
        ui->fsFreqRefWeight->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData()));

        doc.setArray(dataObj.find("inputFreqTypeArray")->toArray());
        ui->fsInputFreqType->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData()));

        doc.setArray(dataObj.find("outTenStatusArray")->toArray());
        ui->fsOutTenStatus->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData()));

        doc.setArray(dataObj.find("outFiveStatusArray")->toArray());
        ui->fsOutFiveStatus->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData()));

    } else if (frameData->frameType == "0602")
    {
        FreqSwitcherInterfaceDto * interFrameDto = (FreqSwitcherInterfaceDto *) frameData;
        ui->fsCtrlStatus->setText(interFrameDto->ctrlStatus == "1" ? "程控" : "本控");
    }
}

void FreqSwitcherForm::displayDeviceCommandOnForm(QJsonObject command)
{
    QString deviceId = command.value("deviceId").toString();
    QList<DeviceBase *> typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("06");
    for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++)
    {
        if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "06")
        {
            ((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,05")
    {
        ui->fsLockPhaseTypeSet->setText(command.value("params").toString());
        ui->fsLockPhaseTypeSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,09")
    {
        ui->fsRefSelectTypeSet->setText(command.value("params").toString());
        ui->fsRefSelectTypeSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,12")
    {
        ui->fsFreqSrcSet->setText(command.value("params").toString());
        ui->fsFreqSrcSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,17")
    {
        ui->fsFreqRefWeightSet->setText(command.value("params").toString());
        ui->fsFreqRefWeightSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,20")
    {
        ui->fsFreqTunSet->setText(command.value("params").toString());
        ui->fsFreqTunSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,21")
    {
        ui->fsPhaseTunSet->setText(command.value("params").toString());
        ui->fsPhaseTunSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLC,01")
    {
        ui->fsControlSet->setText(command.value("params").toString());
        ui->fsControlSetRaw->setText(command.value("rawCommand").toString());
    }
}
