/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-02-15 15:26:55
 * @LastEditors: lishengyin
 * @LastEditTime: 2022-02-16 10:22:27
 */

#ifndef __PALYERSTATUSE_H_
#define __PALYERSTATUSE_H_

#include <iostream>
#include <rapidjson/document.h>
#include <rapidjson/rapidjson.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
#include <vector>
#include "PlayerDeviceList.h"

class PlayerInquire
{
private:
    std::string URL = "/qxsbr360xs.do";
public:
    std::string cid;
    std::string ctype;
    std::string cname;
    std::string dtype;
    std::string did;
    
public:
    PlayerInquire(){};
    ~PlayerInquire(){};

    void setURL(std::string url){
        this->URL = url;
    }

    std::string getUrl(){
        return this->URL;
    }

    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("cid");
        writer.String(cid.c_str());

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

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

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

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

        writer.EndObject();
    }
};

class PlayerStatus
{
private:
    std::string cid;
    std::string cmsg;
    std::string code;
    vector<PlayerDeviceList> deviceList;
    
public:
    PlayerStatus() {};
    ~PlayerStatus() {};


    /**
     * @description: json转换为对象
     * @param {string&} json
     * @return {*}
     */    
    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();

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

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

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

        if(end == doc.FindMember("deviceList") || !doc["deviceList"].IsArray()){
            return false;
        }
        else{
            const rapidjson::Value& objs = doc["deviceList"];
            for(size_t i = 0; i < objs.Size(); i++){
                const rapidjson::Value& obj = objs[i];
                PlayerDeviceList playerDevice;
                if(!playerDevice.jsonToObject(obj)){
                    return false;
                }
                deviceList.push_back(playerDevice);
            }
        }
        
        return true;
    }
};



#endif