#include "SignalGeneratorForm.h"
#include "ui_SignalGeneratorForm.h"
#include "DeviceHubWindow.h"

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

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

void SignalGeneratorForm::on_sigGenButt_clicked()
{
    // 获取设备对象
    int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex;
    SignalGenerator * device = (SignalGenerator *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("04").at(devIndex);

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

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

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

    // 当前显示的设备编号
    if (frameData->frameType == "0401")
    {
        SignalGeneratorInterfaceDto * interFrameDto = (SignalGeneratorInterfaceDto *) frameData;
        ui->sgKeyControl->setText(interFrameDto->keyControl == "1" ? "锁定" : "解锁");
        ui->sgTimeType->setText(interFrameDto->timeType == "1" ? "儒略日" : "年月日");
    } else if (frameData->frameType == "0402")
    {
        SignalGeneratorStatusDto * statusFrameDto = (SignalGeneratorStatusDto *) frameData;
        ui->sgDevStatus->setText(statusFrameDto->devStatus == "1" ? "正常" : "异常");
        ui->sgLeapSec->setText(statusFrameDto->leapSec == "2" ? "负闰秒" : (statusFrameDto->leapSec == "1" ? "正闰秒" : "无闰秒"));
        ui->sgLeapTimestamp->setText(statusFrameDto->leapTimestamp);
        ui->sgFreqSignalStatus->setText(statusFrameDto->devStatus == "1" ? "有效" : "无效");
        ui->sgFreqSignalType->setText(statusFrameDto->freqSignalType == "1" ? "5MHz" : "10MHz");
        ui->sgOppsSignalStatus->setText(statusFrameDto->oppsSignalStatus == "1" ? "有效" : "无效");
        ui->sgPhaseDiff->setText(QString("%1 ns").arg(statusFrameDto->phaseDiff));
        ui->sgPulseWidth->setText(QString("%1 ns").arg(statusFrameDto->pulseWidth));
        ui->sgPhaseShiftAcc->setText(QString("%1 ps").arg(statusFrameDto->phaseShiftAcc));
        ui->sgBacRatio->setText(QString("%1").arg(statusFrameDto->bacRatio));
        ui->sgBacRange->setText(QString("%1").arg(statusFrameDto->bacRange));
        ui->label_ts->setText(statusFrameDto->timestamp);
    } else if (frameData->frameType == "0403")
    {
        SignalGeneratorMJDTimeDto * mjdFrameDto = (SignalGeneratorMJDTimeDto *) frameData;
        ui->sgMJDTime->setText(mjdFrameDto->mjdTime);
        ui->sgMJDDate->setText(mjdFrameDto->mjdDay);
        ui->sgMJDLeapSec->setText(mjdFrameDto->leapSec == "2" ? "负闰秒" : (mjdFrameDto->leapSec == "1" ? "正闰秒" : "无闰秒"));
        ui->sgMJDValid->setText(mjdFrameDto->valid == "01" ? "有效" : "无效");
    } else if (frameData->frameType == "0404")
    {
        SignalGeneratorZDATimeDto * zdaFrameDto = (SignalGeneratorZDATimeDto *) frameData;
        ui->sgZDATime->setText(zdaFrameDto->zdaTime);
        ui->sgZDADate->setText(zdaFrameDto->zdaYear + "-" + zdaFrameDto->zdaMon + "-" + zdaFrameDto->zdaDay);
        ui->sgZDALeapSec->setText(zdaFrameDto->leapSec == "2" ? "负闰秒" : (zdaFrameDto->leapSec == "1" ? "正闰秒" : "无闰秒"));
        ui->sgZDAValid->setText(zdaFrameDto->valid == "01" ? "有效" : "无效");
    }
}

void SignalGeneratorForm::displayDeviceCommandOnForm(QJsonObject command)
{
    QString deviceId = command.value("deviceId").toString();
    QList<DeviceBase *> typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("04");
    for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++)
    {
        if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "04")
        {
            ((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,01")
    {
        ui->sgLeapSecondSet->setText(command.value("params").toString());
        ui->sgLeapSecondSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,02")
    {
        ui->sgSingleSynchSet->setText(command.value("params").toString());
        ui->sgSingleSynchSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,03")
    {
        ui->sgDateSet->setText(command.value("params").toString());
        ui->sgDateSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,04")
    {
        ui->sgSecondWidthSet->setText(command.value("params").toString());
        ui->sgSecondWidthSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,05")
    {
        ui->sgBacRatioSet->setText(command.value("params").toString());
        ui->sgBacRatioSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,06")
    {
        ui->sgBacRangeSet->setText(command.value("params").toString());
        ui->sgBacRangeSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,07")
    {
        ui->sgOppsPhaseShiftSet->setText(command.value("params").toString());
        ui->sgOppsPhaseShiftSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,08")
    {
        ui->sgLeapTimestampSet->setText(command.value("params").toString());
        ui->sgLeapTimestampSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,09")
    {
        ui->sgMJDDateSet->setText(command.value("params").toString());
        ui->sgMJDDateSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,10")
    {
        ui->sgTimeSet->setText(command.value("params").toString());
        ui->sgTimeSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLC,01")
    {
        ui->sgKeyControlSet->setText(command.value("params").toString());
        ui->sgKeyControlSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLC,02")
    {
        ui->sgTimeTypeSet->setText(command.value("params").toString());
        ui->sgTimeTypeSetRaw->setText(command.value("rawCommand").toString());
    }
}
