#pragma once

#include <iostream>

#include <rapidjson/document.h>
#include <rapidjson/rapidjson.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
using namespace std;

class SendDevice
{
private:
public:
    /* data */
    SendDevice(){};
    ~SendDevice(){};
    int deviceId;
    string commandCode;
    int status;
    int workStatus;
    int powerStatus;
    int batteryStatus;
    int num;
    int oxyPressure;
    int gasPressure;
    string remark;
    string attr1;
    string attr2;
    string attr3;
    string msgTime;
    void objectToJson(std::string& str){
        rapidjson::StringBuffer strBuf;
        rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
        this->objectToJson(writer);
        str = strBuf.GetString();
    }
    void objectToJson(rapidjson::Writer<rapidjson::StringBuffer>& writer){
        writer.StartObject();

        writer.Key("deviceId");
        writer.Int(deviceId);

        writer.Key("commandCode");
        writer.String(commandCode.c_str());

        writer.Key("status");
        writer.Int(status);

        writer.Key("workStatus");
        writer.Int(workStatus);

        writer.Key("powerStatus");
        writer.Int(powerStatus);

        writer.Key("batteryStatus");
        writer.Int(batteryStatus);

        writer.Key("num");
        writer.Int(num);
        
        writer.Key("oxyPressure");
        writer.Int(oxyPressure);

        writer.Key("gasPressure");
        writer.Int(gasPressure);

        writer.Key("gasPressure");
        writer.Int(gasPressure);

        writer.Key("remark");
        writer.String(remark.c_str());

        writer.Key("attr1");
        writer.String(attr1.c_str());

        writer.Key("attr2");
        writer.String(attr2.c_str());

        writer.Key("attr3");
        writer.String(attr3.c_str());

        writer.Key("msgTime");
        writer.String(msgTime.c_str());

        writer.EndObject();
    }
    
    bool jsonToObject(const std::string& json){
        rapidjson::Document doc;
        if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
            return false;
        }
        // get members
        const auto end = doc.MemberEnd();

        // json_type
        if(end == doc.FindMember("deviceId") || !doc["deviceId"].IsInt()) {
            return false;
        }else{
            deviceId = doc["deviceId"].GetInt();
        }

        if(end == doc.FindMember("commandCode") || !doc["commandCode"].IsString()) {
            return false;
        }else{
            commandCode = doc["commandCode"].GetString();
        }

        if(end == doc.FindMember("status") || !doc["status"].IsInt()) {
            return false;
        }else{
            status = doc["status"].GetInt();
        }

        if(end == doc.FindMember("workStatus") || !doc["workStatus"].IsInt()) {
            return false;
        }else{
            workStatus = doc["workStatus"].GetInt();
        }

        if(end == doc.FindMember("powerStatus") || !doc["powerStatus"].IsInt()) {
            return false;
        }else{
            powerStatus = doc["powerStatus"].GetInt();
        }

        if(end == doc.FindMember("batteryStatus") || !doc["batteryStatus"].IsInt()) {
            return false;
        }else{
            batteryStatus = doc["batteryStatus"].GetInt();
        }

        if(end == doc.FindMember("num") || !doc["num"].IsInt()) {
            return false;
        }else{
            num = doc["num"].GetInt();
        }

        if(end == doc.FindMember("oxyPressure") || !doc["oxyPressure"].IsInt()) {
            return false;
        }else{
            oxyPressure = doc["oxyPressure"].GetInt();
        }

        if(end == doc.FindMember("gasPressure") || !doc["gasPressure"].IsInt()) {
            return false;
        }else{
            gasPressure = doc["gasPressure"].GetInt();
        }
        
        if(end == doc.FindMember("remark") || !doc["remark"].IsString()) {
            return false;
        }else{
            remark = doc["remark"].GetString();
        }
        if(end == doc.FindMember("attr1") || !doc["attr1"].IsString()) {
            return false;
        }else{
            attr1 = doc["attr1"].GetString();
        }
        if(end == doc.FindMember("attr2") || !doc["attr2"].IsString()) {
            return false;
        }else{
            attr2 = doc["attr2"].GetString();
        }

        if(end == doc.FindMember("attr3") || !doc["attr3"].IsString()) {
            return false;
        }else{
            attr3 = doc["attr3"].GetString();
        }

        if(end == doc.FindMember("msgTime") || !doc["msgTime"].IsString()) {
            return false;
        }else{
            msgTime = doc["msgTime"].GetString();
        }
        return true;
    }
};

