#include "NtpServerForm.h"
#include "ui_NtpServerForm.h"
#include "DeviceHubWindow.h"

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

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

void NtpServerForm::on_ntpServButt_clicked()
{
    // 获取设备对象
    int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex;
    NtpServer * device = (NtpServer *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("11").at(devIndex);

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

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

    if (frameData->frameType == NTP_SERVER_STATUS_FRAME_TYPE)
    {
        ui->label_ts->setText(frameData->timestamp);
        ui->ntpDevStatus->setText(((NtpServerStatusDto *) frameData)->devStatus);
        ui->ntpSystime->setText(((NtpServerStatusDto *) frameData)->sysTime);
        ui->ntpDevStatusJson->setText(QJsonDocument(frameData->toJSON()).toJson(QJsonDocument::Compact));
    }
}
void NtpServerForm::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")
    {

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

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

    }
}
