#include "buriedpointdata.h"

BuriedPointData::BuriedPointData(QObject *parent)
    :QObject(parent)
{
    qDebug() << __FUNCTION__ << __LINE__;
    m_data_reprot_timer = new QTimer();
    m_data_reprot_timer->setInterval(500);
    connect(m_data_reprot_timer,&QTimer::timeout,this,[=]
    {
        run_timer_flag = false;
    });

    connect(this,&BuriedPointData::writeInData,this,&BuriedPointData::writeInDataSlot);
}

#ifdef EXISTS_KYSDK_DIAGNOSTICS
bool BuriedPointData::buriedSettings(QString settingsName ,QString action)
{
    qDebug() << __FUNCTION__ << __LINE__;
    char appName[] = "ukui-bluetooth";
    QByteArray ba = settingsName.toLatin1();
    char *messageType =ba.data();
    KBuriedPoint pt[1];
    pt[0].key = "action";
    std::string str = action.toStdString();
    pt[0].value = str.c_str();

    //if (kdk_buried_point(appName,messageType,pt,1))
    {
        qDebug() << __FUNCTION__ << "buried point failed!" << __LINE__;
        return false;
    }
    return true;
}
#endif

void BuriedPointData::writeInDataSlot(QString settingsName ,QString action)
{
    qDebug() << __FUNCTION__ << settingsName << action << __LINE__;
    run_timer_flag = true;

    if (!m_data_reprot_timer->isActive())
    {
        qInfo() << __FUNCTION__ << "m_data_reprot_timer start!" << __LINE__;
        m_data_reprot_timer->start();
    }

    while (1)
    {
        if (!run_timer_flag)
        {
            qInfo() << __FUNCTION__ << "m_data_reprot_timer start!" << __LINE__;
            break;
        }
        qInfo() << __FUNCTION__ << "write data end" << __LINE__;

#ifdef EXISTS_KYSDK_DIAGNOSTICS
        if (buriedSettings(settingsName,action))
        {
            run_timer_flag = false;
            break;
        }
#else
        run_timer_flag = false;
        break;
#endif
    }

    if (m_data_reprot_timer->isActive())
    {
        m_data_reprot_timer->stop();
    }
    qInfo() << __FUNCTION__ << "write data end" << __LINE__;

}
