#include "TimeReplicatorForm.h"
#include "ui_TimeReplicatorForm.h"
#include "DeviceHubWindow.h"

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

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

void TimeReplicatorForm::on_tmRepButt_clicked()
{
    // 获取设备对象
    int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex;
    TimeReplicator * device = (TimeReplicator *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09").at(devIndex);

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

void TimeReplicatorForm::on_tmRepCmdRepButt_clicked()
{
    // 获取设备对象
    int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex;
    TimeReplicator * device = (TimeReplicator *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09").at(devIndex);

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


void TimeReplicatorForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData)
{
    QString currentDevCode = ((DeviceHubWindow *)this->parent()->parent())->getDevSelect()->currentData().toJsonObject().find("deviceNo")->toString();
    if (currentDevCode != frameData->devCode)
    {
        return;
    }

    // 当前显示的设备编号
    if (frameData->frameType == "0901")
    {
        TimeReplicatorStatusDto * statusFrameDto = (TimeReplicatorStatusDto *) frameData;
        ui->trDevStatus->setText(statusFrameDto->devStatus == "1" ? "正常" : "异常");
        ui->trInCount->setText(QString("%1").arg(statusFrameDto->inCount));
        ui->label_ts->setText(statusFrameDto->timestamp);

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

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

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

void TimeReplicatorForm::displayDeviceCommandOnForm(QJsonObject command)
{
    QString deviceId = command.value("deviceId").toString();
    QList<DeviceBase *> typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09");
    for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++)
    {
        if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "09")
        {
            ((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 == "1")
    {

    } else if (commandType == "2")
    {

    }
}
