#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")) {
            return false;
        } else {
            if(doc["deviceId"].IsInt())
                deviceId = doc["deviceId"].GetInt();
            else if(doc["deviceId"].IsString())
                deviceId = std::stoi(doc["deviceId"].GetString());
        }

        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")){
            return false;
        }
        else{
            if(object["deviceId"].IsInt()) deviceId = object["deviceId"].GetInt();
            else if(object["deviceId"].IsString()) deviceId = std::atoi(object["deviceId"].GetString());
        }
        if(end == object.FindMember("commandCode") || !object["commandCode"].IsString()){
            return false;
        }
        else{
            commandCode = object["commandCode"].GetString();
        }
        return true;
    }
};