#include "Deviceinfo.h"
#include <QSettings>
#include <QString>
#include <QFile>
#include <QDir>
#include <QDebug>
#include <QTextCodec>

#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>

#define CONFIG_PATH  "C:/Profile"
#define CONFIG_FULL_PATH  "C:/Profile/deviceinfo.ini"
#define CONFIG_NAME  "deviceinfo.ini"

DeviceInfo::DeviceInfo()
{
    genDefault();
    load();
}

bool DeviceInfo::update_testinfo(const QString& jsonStr )
{
    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonStr.toUtf8());
    QJsonObject jsonObj = jsonDoc.object();


    //if(jsonObj.contains("target") && jsonObj.value("target").isDouble()){
    //    m_testinfo.m_target = jsonObj.value("target").toDouble();
    //}

    if(jsonObj.contains("interval") && jsonObj.value("interval").isDouble()){
        m_testinfo.m_interval = jsonObj.value("interval").toInt();
    }

    if(jsonObj.contains("threshold") && jsonObj.value("threshold").isDouble()){
        m_testinfo.m_threshold = jsonObj.value("threshold").toDouble();
    }

    if(jsonObj.contains("begin") && jsonObj.value("begin").isDouble()){
        m_testinfo.m_begin = jsonObj.value("begin").toDouble();
        qDebug() << "m_testinfo.m_begin = " << m_testinfo.m_begin;
    }

    if(jsonObj.contains("end") && jsonObj.value("end").isDouble()){
        m_testinfo.m_end = jsonObj.value("end").toDouble();
    }

    if(jsonObj.contains("delta") && jsonObj.value("delta").isDouble()){
        m_testinfo.m_delta = jsonObj.value("delta").toDouble();
    }

    if(jsonObj.contains("passed") && jsonObj.value("passed").isDouble()){
        m_testinfo.m_passed = jsonObj.value("passed").toInt();
    }


    return true;
}

void DeviceInfo::genDefault()
{
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");

    m_model = "JL-VRST-2000";
    m_id = "JL0000000";
    m_name = codec->toUnicode("汽车罐车油气回收系统智能检测仪");
    m_ex_level = "Exd IIBT4 Gb";
    m_manufacture = codec->toUnicode("杭州嘉隆物联网科技有限公司");
    m_date = "2000-00-00";

    m_testinfo.m_target = 6.0;
    m_testinfo.m_interval = 300;
    m_testinfo.m_threshold = 0.5;
    m_testinfo.m_begin = 0.0;
    m_testinfo.m_end = 0.0;
    m_testinfo.m_delta = 0.0;
    m_testinfo.m_passed = 1;

}

void DeviceInfo::load()
{
    QFileInfo fi(CONFIG_FULL_PATH);
    if(fi.isFile()){

        QSettings *configIniRead = new QSettings(CONFIG_FULL_PATH, QSettings::IniFormat);
        configIniRead->setIniCodec("UTF-8");

        m_name = configIniRead->value("Device/Name").toString();
        m_model = configIniRead->value("Device/Model").toString();
        m_id = configIniRead->value("Device/ID").toString();
        m_ex_level = configIniRead->value("Device/ExFlag").toString();
        m_date = configIniRead->value("Device/Date").toString();
        m_manufacture = configIniRead->value("Device/Manufacture").toString();

        m_testinfo.m_target = configIniRead->value("SelfTest/Target").toDouble();
        m_testinfo.m_interval = configIniRead->value("SelfTest/Interval").toInt();
        m_testinfo.m_threshold = configIniRead->value("SelfTest/Threshold").toDouble();

        if(m_testinfo.m_target > 50.0){
           m_testinfo.m_target = 50.0;
        }else if(m_testinfo.m_target < -20.0){
           m_testinfo.m_target = -20.0;
        }

        if(m_testinfo.m_interval > 10*60){
            m_testinfo.m_interval = 10*60;
        }else if(m_testinfo.m_interval < 10){
           m_testinfo.m_interval = 10;
        }

        //if(m_testinfo.m_threshold > m_testinfo.m_target){
        //    m_testinfo.m_threshold = m_testinfo.m_target;
        //}
        qDebug() << "manufacture:" << m_manufacture;

        delete configIniRead;

    }else{

        qDebug() << "CONFIG is not exists";

        QDir dir(CONFIG_PATH);
        if(false == dir.exists()){
            qDebug() << "CONFIG_PATH is not exists, will mk it";
            dir.mkdir(CONFIG_PATH);
        }

        QSettings *configIniWrite = new QSettings(CONFIG_FULL_PATH, QSettings::IniFormat);
        configIniWrite->setIniCodec("UTF-8");

        configIniWrite->setValue("Device/Name", m_name);
        configIniWrite->setValue("Device/Model", m_model);
        configIniWrite->setValue("Device/ID", m_id);
        configIniWrite->setValue("Device/ExFlag", m_ex_level);
        configIniWrite->setValue("Device/Date", m_date);
        configIniWrite->setValue("Device/Manufacture", m_manufacture);

        configIniWrite->setValue("SelfTest/Target", m_testinfo.m_target);
        configIniWrite->setValue("SelfTest/Interval", m_testinfo.m_interval);
        configIniWrite->setValue("SelfTest/Threshold", m_testinfo.m_threshold);

        delete configIniWrite;

    }

}
