#pragma once

#include <iostream>

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

class SendBird
{
private:
public:
    string birdImageBase64;
    int birdNum;
    string birdType;
    int degree;
    string deviceUuid;
    string heightRange;
    string remark;
    int repeStatus;
    string warningTime;
    string attr4;
    string attr5;
    SendBird(){};
    ~SendBird(){};
    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("birdImageBase64");
        writer.String(birdImageBase64.c_str());

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

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

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

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

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

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

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

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

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

        writer.Key("attr5");
        writer.String(attr5.c_str());
        
        writer.EndObject();
    }
    bool jsonToObject(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("birdImageBase64") || !doc["birdImageBase64"].IsString()) {
            return false;
        }else{
            birdImageBase64 = doc["birdImageBase64"].GetString();
        }

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

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

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

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

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

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