#include "TimeSwitcherForm.h"
#include "ui_TimeSwitcherForm.h"
#include "DeviceHubWindow.h"

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

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

void TimeSwitcherForm::on_tmSwiButt_clicked()
{
    // 获取设备对象
    int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex;
    TimeSwitcher * device = (TimeSwitcher *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("05").at(devIndex);

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

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

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

    // 当前显示的设备编号
    if (frameData->frameType == "0501")
    {
        TimeSwitcherStatusDto * statusFrameDto = (TimeSwitcherStatusDto *) frameData;
        ui->tsDevStatus->setText(statusFrameDto->devStatus == "1" ? "正常" : "异常");
        ui->tsRefSelectType->setText(statusFrameDto->refSelectType == "1" ? "手动" : "自动");
        ui->tsRefTmSrc->setText(QString("时间源%1").arg(statusFrameDto->refTmSrc));
        ui->tsOppsPhaseAccOut->setText(QString("%1 ps").arg(statusFrameDto->oppsPhaseAccOut));
        ui->tsBacRatio->setText(QString("%1").arg(statusFrameDto->bacRatio));
        ui->tsBacRange->setText(QString("%1 V").arg(statusFrameDto->bacRange));
        ui->label_ts->setText(statusFrameDto->timestamp);

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

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

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

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

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

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

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

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

void TimeSwitcherForm::displayDeviceCommandOnForm(QJsonObject command)
{
    QString deviceId = command.value("deviceId").toString();
    QList<DeviceBase *> typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("05");
    for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++)
    {
        if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "05")
        {
            ((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->tsRefSelectTypeSet->setText(command.value("params").toString());
        ui->tsRefSelectTypeSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,02")
    {
        ui->tsBacRatioSet->setText(command.value("params").toString());
        ui->tsBacRatioSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,03")
    {
        ui->tsBacRangeSet->setText(command.value("params").toString());
        ui->tsBacRangeSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,04")
    {
        ui->tsTmSrcSet->setText(command.value("params").toString());
        ui->tsTmSrcSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,05")
    {
        ui->tsOppsPhaseSet->setText(command.value("params").toString());
        ui->tsOppsPhaseSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLF,06")
    {
        ui->tsDelayCompSet->setText(command.value("params").toString());
        ui->tsDelayCompSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "GLC,01")
    {
        ui->tsControlSet->setText(command.value("params").toString());
        ui->tsControlSetRaw->setText(command.value("rawCommand").toString());
    }
}
