#pragma once

#include <iostream>
using namespace std;

class RecDeviceCommand
{
private:
    /* data */
    int deviceId;
	/**
	 * 操作驱鸟设备的指令类型
	 */
	std::string commandCode;
public:
    RecDeviceCommand(){};
    ~RecDeviceCommand(){};
    void setDeviceId(int DeviceId){
        deviceId = DeviceId;
    }   

    void setCommandCode(std::string CommandCode) {
        commandCode = CommandCode;
    }

    int getDeviceId(){
        return deviceId;
    }

    std::string getCommandCode(){
        return commandCode;
    }

    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("deviceId");
        writer.String(commandCode.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();
        }
        return true;
    }

    bool jsonToObject(const rapidjson::Value& object){
        const auto end = object.MemberEnd();

        if(end == object.FindMember("deviceId") || !object["deviceId"].IsInt()){
            return false;
        }
        else{
            deviceId = object["deviceId"].GetInt();
        }
        if(end == object.FindMember("commandCode") || !object["commandCode"].IsString()){
            return false;
        }
        else{
            commandCode = object["commandCode"].GetString();
        }
        return true;
    }
};
