#include "BCodeTerminalForm.h"
#include "ui_BCodeTerminalForm.h"
#include "DeviceHubWindow.h"

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

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

void BCodeTerminalForm::on_bctButt_clicked()
{
    // 获取设备对象
    int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex;
    BCodeTerminal * device = (BCodeTerminal *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07").at(devIndex);

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

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

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

    // 当前显示的设备编号
    if (frameData->frameType == "0701")
    {
        BCodeTerminalStatusDto * statusFrameDto = (BCodeTerminalStatusDto *) frameData;
        ui->btDevStatus->setText(statusFrameDto->devStatus == "1" ? "正常" : "异常");
        ui->btBDCInStatus->setText(statusFrameDto->bdcInStatus == "1" ? "有效" : statusFrameDto->bdcInStatus == "2" ? "异常" : "无效");
        ui->btBACInStatus->setText(statusFrameDto->bacInStatus == "1" ? "有效" : statusFrameDto->bacInStatus == "2" ? "异常" : "无效");
        ui->btFiveInStatus->setText(statusFrameDto->fiveInStatus == "1" ? "有效" : statusFrameDto->fiveInStatus == "2" ? "异常" : "无效");
        ui->btRef->setText(statusFrameDto->ref == "0" ? "BDC" : statusFrameDto->ref == "1" ? "BAC" : "5M");
        ui->label_ts->setText(statusFrameDto->timestamp);
        QString trackStatusStr;
        if (statusFrameDto->track == "0") trackStatusStr = "自由";
        else if (statusFrameDto->track == "1") trackStatusStr = "跟踪";
        else if (statusFrameDto->track == "2") trackStatusStr = "锁定";
        else if (statusFrameDto->track == "3") trackStatusStr = "守时";
        else if (statusFrameDto->track == "4") trackStatusStr = "预热";
        ui->btTrack->setText(trackStatusStr);
        ui->btBacRatio->setText(QString("%1").arg(statusFrameDto->bacRatio));
        ui->btBacRange->setText(QString("%1").arg(statusFrameDto->bacRange));

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

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

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

    }
}

void BCodeTerminalForm::displayDeviceCommandOnForm(QJsonObject command)
{
    QString deviceId = command.value("deviceId").toString();
    QList<DeviceBase *> typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07");
    for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++)
    {
        if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "07")
        {
            ((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")
    {
        ui->btRefSet->setText(command.value("params").toString());
        ui->btRefSetRaw->setText(command.value("rawCommand").toString());
    } else if (commandType == "2")
    {
        ui->btBacRatioSet->setText(command.value("params").toString());
        ui->btBacRatioSetRaw->setText(command.value("rawCommand").toString());
    }
}


void BCodeTerminalForm::on_bctCmdRepButt_clicked()
{
    // 获取设备对象
    int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex;
    BCodeTerminal * device = (BCodeTerminal *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07").at(devIndex);

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